windows編程中畫筆與畫刷

來源:互聯網
上載者:User
#include <windows.h>#include <iostream.h>//聲明視窗函數原型LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);BOOL Initinstance(HINSTANCE hInstance,int nCmdShow);void MyPaint(HDC hdc);ATOM MyRegisterClass(HINSTANCE hInstance);HINSTANCE hInst;HPEN hPen[7];HBRUSH hBru[7];int sPen[7]={PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};int sBru[7]={HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};//--------------------------------------------//主函數int WINAPI WinMain(HINSTANCE hInstance,   HINSTANCE PreInstance,   LPSTR lpCmdLine,   int nCmdShow){//定義視窗控制代碼MSG msg;//定義一個用來儲存訊息的變數MyRegisterClass(hInstance);if(!Initinstance(hInstance,nCmdShow)) return false;while(GetMessage(&msg,NULL,0,0))//訊息迴圈{TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}//-------------------------------------//處理訊息的視窗函數ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);  wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = NULL;wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = "canvas";wcex.hIconSm = NULL;return RegisterClassEx(&wcex);}BOOL Initinstance(HINSTANCE hInstance,int nCmdShow){HWND hWnd;HDC hdc;int i;hInst = hInstance;hWnd = CreateWindow("canvas","繪圖視窗",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if(!hWnd) return FALSE;MoveWindow(hWnd,10,10,650,350,true);ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);for(i=0;i<7;i++){hPen[i] = CreatePen(sPen[i],1,RGB(255,0,0));if(i==6)hBru[i] = CreateSolidBrush(RGB(0,255,0));elsehBru[i] = CreateHatchBrush(sBru[i],RGB(0,255,0));}hdc = GetDC(hWnd);MyPaint(hdc);ReleaseDC(hWnd,hdc);return TRUE;}void MyPaint(HDC hdc){int i,x1,x2,y;for(i=0;i<7;i++){y = (i+1) * 30;SelectObject(hdc,hPen[i]);MoveToEx(hdc,30,y,NULL);LineTo(hdc,100,y);}x1 = 120;x2 = 180;for(i=0;i<7;i++){SelectObject(hdc,hBru[i]);Rectangle(hdc,x1,30,x2,y);x1 += 70;x2 += 70;}}LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){PAINTSTRUCT ps;HDC hdc;int i;switch(message){case WM_PAINT:{hdc = BeginPaint(hWnd,&ps);MyPaint(hdc);EndPaint(hWnd,&ps);}break;case WM_DESTROY:{for(i = 0;i < 7 ;i++){DeleteObject(hPen[i]);DeleteObject(hBru[i]);}PostQuitMessage(0);}break;default:return DefWindowProc(hWnd,message,wParam,lParam);}return 0;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.