标签:
// 7Windows_paint.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "7Windows_paint.h"
#include "resource.h"
#include <iostream>
HINSTANCE g_hInst = NULL;
HANDLE g_hStdout = NULL; //控制台
int g_DrawType = 0;
COLORREF g_nPenColor = RGB(0,0,0); //画笔颜色
int g_nPenStyle = PS_SOLID; //画笔样式
int g_nPenWdith = 1; //画笔宽度
COLORREF g_nBrushColor = RGB(0, 0, 0); //画刷
int g_nBrushStyle = 0xFFFFFFFF; //画刷样式
CHAR szText[256] = { 0 }; //备用字符串
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
void DrawPixel(HDC hDC)
{
COLORREF nColor = RGB(255,0,0);
SetPixel(hDC, 100, 100, nColor);
}
void GetPixelColor(HDC hDC)
{
COLORREF nColor = GetPixel(hDC, 100, 100);
int nRed = GetRValue(nColor);
int nGreen = GetGValue(nColor);
int nBlue = GetBValue(nColor);
sprintf_s(szText, 256, "nRed=%d,nGreen=%d,nBlue=%d", nRed, nGreen, nBlue);
//PrintLog(szText);
MessageBox(NULL, szText, "color", MB_OK);
}
//DrawLine
void DrawLine(HDC hDC)
{
MoveToEx(hDC, 0, 0, NULL); //当前点
LineTo(hDC, 500, 500);
MoveToEx(hDC, 500, 0, NULL);
LineTo(hDC, 0, 500);
}
//DrawArc弧
void DrawArc(HDC hDC)
{
SetArcDirection(hDC, AD_CLOCKWISE);//AD_COUNTERCLOCKWISE //顺时针
Arc(hDC, 400, 200, 500, 300, 500, 200, 400, 200); //顺时针和逆时针画出来的东西不同
SetArcDirection(hDC, AD_COUNTERCLOCKWISE);//AD_CLOCKWISE //逆时针
Arc(hDC, 500, 200, 600, 300, 600, 200, 500, 400); //顺时针和逆时针画出来的东西不同
MoveToEx(hDC, 200, 200, NULL);
AngleArc(hDC, 200, 200, 100, 90, 270);
LineTo(hDC, 200, 200);
}
//DrawPolyLine
//折线
void DrawPolyLine(HDC hDC)
{
POINT ptPolyLine[7] = { 0 }; //三角形需要4个点
ptPolyLine[0].x = 100;
ptPolyLine[0].y = 100;
ptPolyLine[1].x = 200;
ptPolyLine[1].y = 100;
ptPolyLine[2].x = 200;
ptPolyLine[2].y = 200;
ptPolyLine[3].x = 300;
ptPolyLine[3].y = 200;
ptPolyLine[4].x = 300;
ptPolyLine[4].y = 300;
ptPolyLine[5].x = 400;
ptPolyLine[5].y = 300;
ptPolyLine[6].x = 400;
ptPolyLine[6].y = 400;
//两点绘制一直线
Polyline(hDC, ptPolyLine, 7); //绘制折线
//PolylineTo(hDC, ptPolyLine, 4);
/*
分成两组,这两组之间没有任何关联
*/
DWORD nGroup[2] = { 3, 4 };
//PolyPolyline(hDC, ptPolyLine, nGroup, 2);
}
//DrawBizer曲线
void DrawBizer(HDC hDC)
{
POINT ptBizer[7] = { 0 };
ptBizer[0].x = 100; //端点
ptBizer[0].y = 100;
ptBizer[1].x = 150; //控制点
ptBizer[1].y = 50;
ptBizer[2].x = 300; //控制点
ptBizer[2].y = 150;
ptBizer[3].x = 300; //端点
ptBizer[3].y = 100;
ptBizer[4].x = 300; //控制点
ptBizer[4].y = 400;
ptBizer[5].x = 400; //控制点
ptBizer[5].y = 200;
ptBizer[6].x = 500; //端点
ptBizer[6].y = 300;
PolyBezier(hDC, ptBizer, 7);
//为了看的更清楚,把切线也给划出来
MoveToEx(hDC, ptBizer[0].x, ptBizer[0].y,NULL);
LineTo(hDC, ptBizer[1].x, ptBizer[1].y);
MoveToEx(hDC, ptBizer[2].x, ptBizer[2].y, NULL);
LineTo(hDC, ptBizer[3].x, ptBizer[3].y);
}
//DrawPolyDraw多样式线
void DrawPolyDraw(HDC hDC)
{
POINT ptDraw[4] = { 0 };
ptDraw[0].x = 100;
ptDraw[0].y = 100;
ptDraw[1].x = 200;
ptDraw[1].y = 100;
ptDraw[2].x = 200;
ptDraw[2].y = 200;
ptDraw[3].x = 300;
ptDraw[3].y = 200;
BYTE ptType[4] = { 0 }; //PolyDraw第三个参数
ptType[0] = PT_MOVETO;
ptType[1] = PT_LINETO;
ptType[2] = PT_MOVETO;
ptType[3] = PT_LINETO;
PolyDraw(hDC, ptDraw, ptType, 4);
}
//Rect 矩形
void DrawRect(HDC hDC)
{
//矩形
Rectangle(hDC, 100, 100, 200, 200);
//带圆角矩形
RoundRect(hDC, 300, 100, 400, 200, 10, 10);
}
//DrawEllipse圆和椭圆
void DrawEllipse(HDC hDC)
{
//圆
Ellipse(hDC, 100, 100, 200, 200);
//椭圆
Ellipse(hDC, 300, 100, 500, 200);
}
//DrawPie饼图
void DrawPie(HDC hDC)
{
Pie(hDC, 100, 100, 500, 400, 500, 100, 100, 100);
}
//Chord弦
void DrawChord(HDC hDC)
{
Chord(hDC, 100, 100, 500, 400, 500, 100, 100, 100);
}
//DrawPolygon多边形
void DrawPolygon(HDC hDC)
{
POINT ptPolygon[7] = { 0 };
ptPolygon[0].x = 100;
ptPolygon[0].y = 100;
ptPolygon[1].x = 200;
ptPolygon[1].y = 100;
ptPolygon[2].x = 200;
ptPolygon[2].y = 200;
ptPolygon[3].x = 300;
ptPolygon[3].y = 200;
ptPolygon[4].x = 300;
ptPolygon[4].y = 300;
ptPolygon[5].x = 400;
ptPolygon[5].y = 300;
ptPolygon[6].x = 400;
ptPolygon[6].y = 400;
Polygon(hDC, ptPolygon, 7);
}
void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps = { 0 };
HDC hDC = BeginPaint(hWnd,&ps);
//画笔使用
//创建画笔
HPEN hPen = CreatePen(g_nPenStyle, g_nPenWdith, g_nPenColor);
//设置画笔到当前DC
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
//画刷使用*/
//创建画刷
/*以下一句为默认画刷*/
//HBRUSH hBrush = CreateSolidBrush(g_nBrushColor);
//创建画刷样式时改写
HBRUSH hBrush = NULL;
if (g_nBrushStyle == 0xFFFFFFFF)
{
hBrush = CreateSolidBrush(g_nBrushColor);
}
else
{
hBrush = CreateHatchBrush(g_nBrushStyle, g_nBrushColor);
}
//设置画刷到当前DC
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
//绘制图形
//在绘制图形里面根据类型绘制图形
switch (g_DrawType)
{
case ID_SETPIXEL:
DrawPixel(hDC);
break;
case ID_GETPIXEL:
GetPixelColor(hDC);
break;
case ID_LINE:
DrawLine(hDC);
break;
case ID_ARC:
DrawArc(hDC);
break;
case ID_POLYLINE:
DrawPolyLine(hDC);
break;
case ID_DRAWBIZER:
DrawBizer(hDC);
break;
case ID_POLYDRAW:
DrawPolyDraw(hDC);
break;
//矩形
case ID_RECT:
DrawRect(hDC);
break;
case ID_ELLIPSE:
DrawEllipse(hDC);
break;
case ID_PIE:
DrawPie(hDC);
break;
case ID_CHORD:
DrawChord(hDC);
break;
case ID_POLYGON:
DrawPolygon(hDC);
break;
}
//画笔使用2
//取出画笔
SelectObject(hDC, hOldPen);
//销毁画笔
DeleteObject(hPen);
//画刷使用2
//取出画刷
SelectObject(hDC, hOldBrush);
//销毁画刷
DeleteObject(hBrush);
EndPaint(hWnd,&ps);
}
//OnCommand
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nCmdID = LOWORD(wParam);
switch (nCmdID)
{
case ID_SETPIXEL: //Get 和 Set都是同样的代码,所以这样简写
case ID_GETPIXEL:
case ID_LINE: //线
case ID_ARC: //弧
case ID_POLYLINE: //折线
case ID_DRAWBIZER: //BIZER曲线
case ID_POLYDRAW: //多样式线
//矩形
case ID_RECT:
case ID_ELLIPSE:
case ID_PIE:
case ID_CHORD:
case ID_POLYGON:
g_DrawType = nCmdID; //保存绘制图形类型
//刷新窗口
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_REDPEN: //红色画笔
//把这个笔保存到全局变量中
g_nPenColor = RGB(255, 0, 0); //画笔颜色
//刷新窗口
InvalidateRect(hWnd, NULL, TRUE);
break;
case PS_PSDASH: //画笔的样式为虚线
g_nPenStyle = PS_DASH;
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_PEN1: //画笔宽度为1
g_nPenWdith = 1;
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_PEN5: //画笔宽度为5
g_nPenWdith = 5;
InvalidateRect(hWnd, NULL, TRUE);
break;
//画刷颜色
case ID_REDBRUSH:
g_nBrushColor = RGB(255, 0, 0);
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_GREENBRUSH:
g_nBrushColor = RGB(0, 255, 0);
InvalidateRect(hWnd, NULL, TRUE);
break;
//画刷样式
case ID_SOLIDBRUSH:
g_nBrushStyle = 0xFFFFFFFF;
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_HSDIAGCROSS:
g_nBrushStyle = HS_DIAGCROSS;
InvalidateRect(hWnd, NULL, TRUE);
break;
case ID_EXIT:
PostQuitMessage(0);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_PAINT:
OnPaint(hWnd, nMsg, wParam, lParam);
break;
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbClsExtra = 0;
wce.cbSize = sizeof(wce);
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_VREDRAW | CS_HREDRAW;
ATOM aTom = RegisterClassEx(&wce);
if (aTom)
{
return TRUE;
}
else
{
return FALSE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
CHAR szTextWindow[256] = { 0 };
LoadString(g_hInst, IDS_WNDID, szTextWindow, 256);
//添加菜单
HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowEx(0, pszClassName, szTextWindow, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, hMenu, g_hInst, NULL);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
//AllocConsole();
RegisterWnd("9999");
HWND hWnd = CreateWnd("9999");
ShowWnd(hWnd);
Msg();
return 0;
}
标签:
原文地址:http://www.cnblogs.com/nfking/p/5573164.html