Win32编程---实现点,线,面的绘制

源自:http://blog.csdn.net/chenqiai0/article/details/8045123

Win32编程---实现点,线,面的绘制


  1. #include <windows.h>  
  2. #include   <string>     
  3. using   namespace   std;  
  4.   
  5. //----------函数声明---------------  
  6. void init(HWND hWnd,HINSTANCE hInstance);  
  7. void pix(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps);//画点,也就是像素输出  
  8. void rect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps);//----画矩形----  
  9. void fillrect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//填充矩形  
  10. void linerect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//线框矩形  
  11. void triangle(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,POINT Pt[3],PAINTSTRUCT &Ps);//三角形  
  12. void ellipse(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps);//椭圆  
  13. void DrawContent(HWND hWnd);  
  14.   
  15. //-----回调函数-----------------  
  16. LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);  
  17.   
  18. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  19.                    LPSTR lpCmdLine, int nCmdShow)  
  20. {  
  21.       
  22.     WNDCLASSEX  WndCls;  
  23.   
  24.     MSG         Msg;  
  25.     WndCls.cbSize        = sizeof(WndCls);  
  26.     WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;  
  27.     WndCls.lpfnWndProc   = WindProcedure;  
  28.     WndCls.cbClsExtra    = 0;  
  29.     WndCls.cbWndExtra    = 0;  
  30.     WndCls.hInstance     = hInstance;  
  31.     WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);  
  32.     WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);  
  33.     WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
  34.     WndCls.lpszMenuName  = NULL;  
  35.     WndCls.lpszClassName = TEXT("MAIN");  
  36.     WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);  
  37.     RegisterClassEx(&WndCls);  
  38.   
  39.    HWND  hWnd=CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,  
  40.                    TEXT("MAIN"),TEXT( "陈琦"),  
  41.                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,  
  42.                    CW_USEDEFAULT, CW_USEDEFAULT, 820, 820,  
  43.                    NULL, NULL, hInstance, NULL);  
  44.     init(hWnd,hInstance);  
  45.     UpdateWindow(hWnd);  
  46.     while( GetMessage(&Msg, NULL, 0, 0) )  
  47.     {  
  48.         TranslateMessage(&Msg);  
  49.         DispatchMessage( &Msg);  
  50.     }  
  51.   
  52.     return static_cast<int>(Msg.wParam);  
  53. }  
  54.        
  55. void init(HWND hWnd,HINSTANCE hInstance)  
  56. {   
  57.     CreateWindow(  TEXT("BUTTON"),  TEXT("画点"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 0, 150,80, hWnd,NULL, hInstance, NULL);  
  58.     CreateWindow(   TEXT("BUTTON"),  TEXT("矩形"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 90, 150,80, hWnd,NULL, hInstance, NULL);    
  59.     CreateWindow(   TEXT("BUTTON"),  TEXT("填充矩形"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 180, 150,80, hWnd,NULL, hInstance, NULL);  
  60.     CreateWindow(   TEXT("BUTTON"),  TEXT("线框矩形"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 270, 150,80, hWnd,NULL, hInstance, NULL);  
  61.     CreateWindow(   TEXT("BUTTON"),  TEXT("三角形"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 360, 150,80, hWnd,NULL, hInstance, NULL);  
  62.     CreateWindow(   TEXT("BUTTON"),  TEXT("椭圆"),  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 450, 150,80, hWnd,NULL, hInstance, NULL);  
  63. }  
  64.   
  65. LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam)  
  66. {  
  67.       
  68.     HDC         hDC;  
  69.     PAINTSTRUCT Ps;  
  70.     HBRUSH      NewBrush;  
  71.     RECT    r;  
  72.     POINT Pt[3];  
  73.   
  74.     switch(Msg)  
  75.     {  
  76.     case WM_COMMAND:  
  77.     HWND h;  
  78.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("画点"));  
  79.     if(DWORD(lParam)==int(h))  
  80.     {  
  81.         pix(hWnd,hDC,NewBrush,Ps);  
  82.         break;  
  83.     }  
  84.   
  85.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("矩形"));  
  86.     if(DWORD(lParam)==int(h))  
  87.     {  
  88.         rect(hWnd,hDC,NewBrush,Ps);  
  89.         break;  
  90.     }  
  91.   
  92.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("填充矩形"));  
  93.     if(DWORD(lParam)==int(h))  
  94.     {  
  95.         fillrect(hWnd,hDC,NewBrush,r,Ps);  
  96.         break;  
  97.     }  
  98.   
  99.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("线框矩形"));  
  100.     if(DWORD(lParam)==int(h))  
  101.     {  
  102.         linerect(hWnd,hDC,NewBrush,r,Ps);  
  103.         break;  
  104.     }  
  105.   
  106.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("三角形"));  
  107.     if(DWORD(lParam)==int(h))  
  108.     {  
  109.         triangle(hWnd,hDC,NewBrush,Pt,Ps);  
  110.         break;  
  111.     }  
  112.   
  113.     h=FindWindowEx(hWnd ,NULL,TEXT("BUTTON"),TEXT("椭圆"));  
  114.     if(DWORD(lParam)==int(h))  
  115.     {  
  116.         ellipse(hWnd,hDC,NewBrush,r,Ps);  
  117.         break;  
  118.     }     
  119.     case WM_PAINT:  
  120.         DrawContent(hWnd);//为什么加了这一句后开始按钮就会显示,而不加的话按钮需要重绘以后显示  
  121.          break;  
  122.   
  123.     case WM_DESTROY:  
  124.         PostQuitMessage(WM_QUIT);  
  125.         break;  
  126.     default:  
  127.         return DefWindowProc(hWnd, Msg, wParam, lParam);  
  128.     }  
  129.     return 0;  
  130. }  
  131.   
  132.   
  133.   
  134. //画点  
  135. void pix(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps)  
  136. {  
  137.     //hDC = BeginPaint(hWnd, &Ps);//用BeginPaint的话消息一定要WM_PAINT响应,但是WM_PAINT里面只有DrawContent(hWnd);  
  138.                                   //所以用getDC  
  139.     hDC=GetDC(hWnd);  
  140.     NewBrush=CreateSolidBrush(RGB(255,0,0));  
  141.     //SelectObject(hDC, NewBrush);  
  142.     for(int x=500;x<600;x++)//由于一个点太小了,所以这里花了100个点构成线段  
  143.         SetPixel(hDC,x,50,RGB(255,0,0));  
  144.     //DeleteObject(NewBrush);  
  145.     //EndPaint(hWnd, &Ps);  
  146.     ReleaseDC(hWnd,hDC);  
  147. }  
  148. //矩形  
  149. void rect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,PAINTSTRUCT &Ps)  
  150. {  
  151.     //hDC = BeginPaint(hWnd, &Ps);  
  152.     hDC=GetDC(hWnd);  
  153.     NewBrush=CreateSolidBrush(RGB(255,0,0));  
  154.     SelectObject(hDC, NewBrush);  
  155.     Rectangle(hDC,400,400,500,500);  
  156.     DeleteObject(NewBrush);  
  157.     ReleaseDC(hWnd,hDC);  
  158.     //EndPaint(hWnd, &Ps);  
  159. }  
  160.   
  161. //填充矩形  
  162. void fillrect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)  
  163. {  
  164.     //hDC = BeginPaint(hWnd, &Ps);  
  165.     hDC=GetDC(hWnd);  
  166.     NewBrush = CreateSolidBrush(RGB(25, 25, 5));  
  167.     SelectObject(hDC, NewBrush);  
  168.     SetRect (&r, 200, 200,250, 250);  
  169.     FillRect(hDC, &r, NewBrush);  
  170.     DeleteObject(NewBrush);  
  171.     //EndPaint(hWnd, &Ps);  
  172.     ReleaseDC(hWnd,hDC);  
  173. }  
  174.   
  175. //线框矩形  
  176. void linerect(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)  
  177. {  
  178.     //hDC = BeginPaint(hWnd, &Ps);  
  179.     hDC=GetDC(hWnd);  
  180.     NewBrush = CreateSolidBrush(RGB(25, 25, 5));  
  181.     SelectObject(hDC, NewBrush);  
  182.     SetRect (&r, 250, 250,400, 400);  
  183.     FrameRect(hDC, &r, NewBrush);  
  184.     DeleteObject(NewBrush);  
  185.     DeleteObject(NewBrush);  
  186.     //EndPaint(hWnd, &Ps);  
  187.     ReleaseDC(hWnd,hDC);  
  188. }  
  189. //三角形  
  190. void triangle(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,POINT Pt[3],PAINTSTRUCT &Ps)  
  191. {  
  192.     //hDC = BeginPaint(hWnd, &Ps);  
  193.     hDC=GetDC(hWnd);  
  194.     NewBrush = CreateSolidBrush(RGB(50, 50, 50));   
  195.     SelectObject(hDC, NewBrush);      
  196.     Pt[0].x = 425; Pt[0].y = 40;  
  197.     Pt[1].x = 395; Pt[1].y = 70;  
  198.     Pt[2].x = 455; Pt[2].y = 70;  
  199.     Polygon(hDC, Pt, 3);   
  200.     DeleteObject(NewBrush);  
  201.   
  202.     NewBrush = CreateSolidBrush(RGB(0, 255, 0));   
  203.     SelectObject(hDC, NewBrush);  
  204.     Pt[0].x = 365; Pt[0].y = 110;  
  205.     Pt[1].x = 395; Pt[1].y = 80;  
  206.     Pt[2].x = 395; Pt[2].y = 140;  
  207.     Polygon(hDC, Pt, 3);   
  208.     DeleteObject(NewBrush);  
  209.   
  210.     NewBrush = CreateSolidBrush(RGB(255, 0, 0));   
  211.     SelectObject(hDC, NewBrush);  
  212.     Pt[0].x = 485; Pt[0].y = 110;  
  213.     Pt[1].x = 455; Pt[1].y = 80;  
  214.     Pt[2].x = 455; Pt[2].y = 140;  
  215.     Polygon(hDC, Pt, 3);   
  216.     DeleteObject(NewBrush);  
  217.   
  218.     NewBrush = CreateSolidBrush(RGB(0, 0, 255));   
  219.     SelectObject(hDC, NewBrush);  
  220.     Pt[0].x = 425; Pt[0].y = 180;  
  221.     Pt[1].x = 455; Pt[1].y = 150;  
  222.     Pt[2].x = 395; Pt[2].y = 150;  
  223.     Polygon(hDC, Pt, 3);   
  224.     DeleteObject(NewBrush);  
  225.     //EndPaint(hWnd, &Ps);  
  226.     ReleaseDC(hWnd,hDC);  
  227. }  
  228.   
  229. //椭圆  
  230. void ellipse(HWND &hWnd,HDC &hDC,HBRUSH &NewBrush,RECT &r,PAINTSTRUCT &Ps)//椭圆  
  231. {  
  232.     //hDC = BeginPaint(hWnd, &Ps);  
  233.     hDC=GetDC(hWnd);  
  234.     NewBrush = CreateSolidBrush(RGB(255, 0, 0));   
  235.     SelectObject(hDC, NewBrush);  
  236.     Ellipse(hDC,500,500,600,600);  
  237.     DeleteObject(NewBrush);  
  238.     //EndPaint(hWnd, &Ps);  
  239.     ReleaseDC(hWnd,hDC);  
  240. }  
  241.   
  242. void DrawContent(HWND hWnd)  
  243. {  
  244.     HDC         hDC;  
  245.     PAINTSTRUCT Ps;   
  246.     hDC = BeginPaint(hWnd, &Ps);//用BeginPaint的话消息一定要WM_Paint响应  
  247.     EndPaint(hWnd, &Ps);  
  248.   
  249. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值