// Zuoye1.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #include "iostream" HINSTANCE g_hInst = NULL; int g_x1pos,g_y1pos,g_x2pos,g_y2pos; int g_nDreawType=0; //保存画笔颜色 COLORREF g_nPenColor=RGB(0,0,0); //保存画笔的样式 int g_nPenStyle=0; //保存画笔的宽度 int g_nPenWidth=0; //保存画刷的颜色 COLORREF g_BrushColor=RGB(0,0,0); //保存画刷的样式 int g_BrushStyle=-1; 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=GetRValue(nColor); int nBlue=GetRValue(nColor); char szText[260]={0}; sprintf(szText,"color=%08X,RED=%d,GREEN=%d,BLUE=%d",nColor, nRed,nGreen,nBlue); MessageBox(NULL,szText,"Color",MB_OK); } //绘制一条直线 void DrawLine(HDC hDC){ //开始从0 0 MoveToEx(hDC,0,0,NULL); //结束是到500 500 LineTo(hDC,500,500); MoveToEx(hDC,300,300,NULL); LineTo(hDC,0,0); } //绘制一条弧线 void DrawArc(HDC hDC){ MoveToEx(hDC,200,200,NULL); AngleArc(hDC,200,200,100,60,240); LineTo(hDC,200,200); Arc(hDC,400,400,500,500,500,400,400,400); } //绘制一条折线 void PolyLine(HDC hDC){ POINT ptPolyLine[7]={0}; ptPolyLine[0].x =100; ptPolyLine[0].y =100; ptPolyLine[1].x =200; ptPolyLine[1].y =200; ptPolyLine[2].x =100; ptPolyLine[2].y =200; ptPolyLine[3].x =100; ptPolyLine[3].y =200; ptPolyLine[4].x =300; ptPolyLine[4].y =300; ptPolyLine[4].x =400; ptPolyLine[4].y =300; ptPolyLine[4].x =400; ptPolyLine[4].y =400; //Polyline(hDC,ptPolyLine,7); 和一下一函数一致只不过在绘制折线前从屏幕当前的使用LineTo连接绘制直线到折线的第一个顶点 //PolyPolyline绘制多条直线 PolylineTo(hDC,ptPolyLine,3); } //矩形绘制 void DrawRect(HDC hDC){ Rectangle(hDC,100,100,200,200); //吧后面的角度给修改大一点和CSS3的圆角效果一样,在大一点可以成一个圆形 //我把他设置成了160,160 RoundRect(hDC,300,100,400,200,160,160); } //绘制一个椭圆 void DrawEllipse(HDC hDC){ Ellipse(hDC,100,100,200,200); Ellipse(hDC,300,100,500,200); } //绘制一个扇形 void DrawPie(HDC hDC){ Pie(hDC,100,100,500,400,500,100,100,200); } //绘制一个平行四边形 void DrawGon(HDC hDC){ POINT ptGon[4]={0}; ptGon[0].x=100; ptGon[0].y=100; ptGon[1].x=200; ptGon[1].y=100; ptGon[3].x=600; ptGon[3].y=300; ptGon[2].x=500; ptGon[2].y=300; Polygon(hDC,ptGon,4); } //曲线绘制 类似于PS钢笔工具 void DrawBizer(HDC hDC){ POINT ptPolyBezier[4]={0}; ptPolyBezier[0].x =100; ptPolyBezier[0].y =100; ptPolyBezier[1].x =150; ptPolyBezier[1].y =50; ptPolyBezier[2].x =300; ptPolyBezier[2].y =150; ptPolyBezier[3].x =300; ptPolyBezier[3].y =100; PolyBezier(hDC,ptPolyBezier,4); MoveToEx(hDC,ptPolyBezier[0].x,ptPolyBezier[0].y,NULL); LineTo(hDC,ptPolyBezier[1].x,ptPolyBezier[1].y); } void onPaint(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){ PAINTSTRUCT ps={0}; HDC hDC=BeginPaint(hWnd,&ps); //CreatePen 第一个画笔样式 默认PS_SOLID 实心线 //第二 线宽度 默认1个像素 第三 颜色 HPEN hPen=CreatePen(g_nPenStyle,g_nPenWidth,g_nPenColor); //吧画笔加载到HDC里面 返回以前用的画笔句柄 得保存好 HPEN hOldPen=(HPEN)SelectObject(hDC,hPen); HBRUSH hBrush=NULL; if( g_BrushStyle<0) { //创建一个画刷句柄 hBrush=CreateSolidBrush(g_BrushColor); }else{ //这个要比上面有很多的选择 hBrush=CreateHatchBrush(g_BrushStyle,g_BrushColor); } //设置画刷当前DC和上面画笔一样 HBRUSH hOldBrush=(HBRUSH) SelectObject(hDC,hBrush); switch(g_nDreawType){ //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: PolyLine(hDC); break; case ID_Bizer: DrawBizer(hDC); break; case ID_RECT: DrawRect(hDC); break; case ID_ELLIPSE: DrawEllipse(hDC); break; case ID_PIE: DrawPie(hDC); break; case ID_POLYGON: DrawGon(hDC); break; } //吧以前的画笔放进去 SelectObject(hDC,hOldPen); // 并销毁画笔 DeleteObject(hPen); //吧以前的画刷放进去 SelectObject(hDC,hOldBrush); // 并销毁画刷 DeleteObject(hBrush); EndPaint(hWnd,&ps); } void onCreate(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){ HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MENU1)); SetMenu(hWnd,hMenu); } void onCommand(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){ int nCmdID=LOWORD(wParam); switch(nCmdID){ case ID_GETPIXEL: case ID_LINE: case ID_ARC: case ID_POLYLINE: case ID_Bizer: case ID_RECT: case ID_ELLIPSE: case ID_PIE: case ID_POLYGON: g_nDreawType=nCmdID; InvalidateRect(hWnd,NULL,true); break; case ID_EXIT: PostQuitMessage(0); break; case ID_PENRED: g_nPenColor= RGB(255,0,0); InvalidateRect(hWnd,NULL,true); break; case ID_PS_SOLID: g_nPenStyle= 0; InvalidateRect(hWnd,NULL,true); break; case ID_PS_DASH: g_nPenStyle= 1; InvalidateRect(hWnd,NULL,true); break; case ID_PS_DOT: g_nPenStyle= 2; InvalidateRect(hWnd,NULL,true); break; case ID_PS_DASHDOT: g_nPenStyle= 3; InvalidateRect(hWnd,NULL,true); break; //画刷设置颜色 case ID_REDBRUSH: g_BrushColor=RGB(255,0,0); InvalidateRect(hWnd,NULL,true); break; case ID_BRUSH2e8b57: g_BrushColor=RGB(46,139,87); InvalidateRect(hWnd,NULL,true); break; case ID_PEN3: g_nPenWidth=3; InvalidateRect(hWnd,NULL,true); break; case ID_HS_CROSS: g_BrushStyle=1; InvalidateRect(hWnd,NULL,true); break; case ID_HS_DIAGCROSS: g_BrushStyle=2; InvalidateRect(hWnd,NULL,true); break; } } //主窗口的窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_DESTROY: PostQuitMessage( 0 ); KillTimer(hWnd,1002); break; case WM_COMMAND: onCommand(hWnd, nMsg, wParam, lParam); break; case WM_CREATE: onCreate(hWnd, nMsg, wParam, lParam); break; case WM_PAINT: onPaint(hWnd, nMsg, wParam, lParam); break; //传递到子窗体处理消息响应 //return DefFrameProc( hWnd,g_hMDIClient, nMsg, wParam, lParam ); //传递给系统 } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } //窗口注册函数 BOOL RegisterWnd( LPSTR pszClassName,int nBrush ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.style = CS_HREDRAW|CS_VREDRAW; wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.lpfnWndProc= WndProc; wce.hInstance = g_hInst; wce.hCursor = NULL; wce.hIcon = NULL; wce.hbrBackground = HBRUSH(nBrush); wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.hIconSm = NULL; ATOM nAtom = RegisterClassEx( &wce ); if( nAtom == 0 ) { return FALSE; } return TRUE; } //创建主窗口 HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MainWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } //消息循环 void Message() { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { DispatchMessage( &msg ); } } //显示窗口 void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. RegisterWnd("luoqiang",COLOR_BACKGROUND); DisplayWnd(CreateWnd("luoqiang")); Message(); return 0; }