學習windows編程 day4 之 多邊矩形填充

來源:互聯網
上載者:User

標籤:wpa   apt   message   ram   res   default   classname   isp   ica   

#include <windows.h>#include <math.h>LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);#define R 200#define PI 3.1415926int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){    //聲明全域資料:類名    static TCHAR szClassName[] = TEXT("MyWindows");    HWND hwnd;    MSG msg;    //註冊視窗類別    WNDCLASS wndclass;    wndclass.hInstance = hInstance;    wndclass.lpszClassName = szClassName;    wndclass.cbClsExtra = 0;    wndclass.cbWndExtra = 0;    wndclass.lpfnWndProc = WndProc;    wndclass.lpszMenuName = NULL;    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    wndclass.style = CS_HREDRAW;    if (!RegisterClass(&wndclass))    {        MessageBox(NULL, TEXT("this program must run in Windows NT!"), szClassName, MB_ICONERROR);        return 0;    }    hwnd = CreateWindow(        szClassName,        TEXT("MyFirstPractice"),        WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT,        CW_USEDEFAULT,        CW_USEDEFAULT,        CW_USEDEFAULT,        NULL,        NULL,        hInstance,        NULL        );    ShowWindow(hwnd, nShowCmd);    UpdateWindow(hwnd);    while (GetMessage(&msg, NULL, 0, 0))    {        TranslateMessage(&msg);        DispatchMessage(&msg);    }    return msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    HDC hdc;    PAINTSTRUCT ps;    RECT rect;    static HBRUSH hBrush, hOldBrush;//    繪製一個多邊形,可填充是需要頂點的個數polygon    POINT apt1[4] = { 100, 200, 200, 100, 300, 200,200, 300 };//    繪製一個封閉多邊形polyline需要頂點數+1    注意:用線繪製的封閉映像不具有填充能力//    POINT apt2[5] = { 400, 200, 500, 100, 600, 200, 500, 300, 400, 200 };    POINT apt2[5];    int cxClient, cyClient;    //星星的中心位置    switch (message)    {    case WM_PAINT:        hdc = BeginPaint(hwnd, &ps);        GetClientRect(hwnd, &rect);        cxClient = rect.right / 2;        cyClient = rect.bottom / 2;        //擷取五個頂點        //左上頂點        apt2[0].x = cxClient - (int)(R*cos(PI / 5));        apt2[0].y = cyClient - (int)(R*sin(PI / 5));        //右上頂點        apt2[1].x = cxClient + (int)(R*cos(PI / 5));        apt2[1].y = cyClient - (int)(R*sin(PI / 5));        //左下頂點        apt2[2].x = cxClient - (int)(R*cos(PI / 5));        apt2[2].y = cyClient + (int)(R*sin(PI / 5));        //上頂點        apt2[3].x = cxClient;        apt2[3].y = cyClient - R;        //右下頂點        apt2[4].x = cxClient + (int)(R*cos(PI / 5));        apt2[4].y = cyClient + (int)(R*sin(PI / 5));         hBrush = CreateSolidBrush(RGB(255,255,0));         hOldBrush = SelectObject(hdc, hBrush);//        SetPolyFillMode(hdc, ALTERNATE);    //交替填充        SetPolyFillMode(hdc, WINDING);    //螺旋填充,填充所有能夠一筆完成的圖形//          Polygon(hdc, apt2, 5);//         Polyline(hdc, apt2, 5);//          SelectObject(hdc, hOldBrush);         DeleteObject(hOldBrush);        EndPaint(hwnd, &ps);        break;    case WM_DESTROY:        PostQuitMessage(0);        return 0;    }    return DefWindowProc(hwnd, message, wParam, lParam);}

 

#include <windows.h>#include <math.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
#define R 200#define PI 3.1415926
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){//聲明全域資料:類名static TCHAR szClassName[] = TEXT("MyWindows");HWND hwnd;MSG msg;
//註冊視窗類別WNDCLASS wndclass;
wndclass.hInstance = hInstance;wndclass.lpszClassName = szClassName;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.lpfnWndProc = WndProc;wndclass.lpszMenuName = NULL;wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.style = CS_HREDRAW;
if (!RegisterClass(&wndclass)){MessageBox(NULL, TEXT("this program must run in Windows NT!"), szClassName, MB_ICONERROR);return 0;}
hwnd = CreateWindow(szClassName,TEXT("MyFirstPractice"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd, nShowCmd);UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}
return msg.wParam;}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){HDC hdc;PAINTSTRUCT ps;RECT rect;
static HBRUSH hBrush, hOldBrush;//繪製一個多邊形,可填充是需要頂點的個數polygonPOINT apt1[4] = { 100, 200, 200, 100, 300, 200,200, 300 };//繪製一個封閉多邊形polyline需要頂點數+1注意:用線繪製的封閉映像不具有填充能力//POINT apt2[5] = { 400, 200, 500, 100, 600, 200, 500, 300, 400, 200 };POINT apt2[5];int cxClient, cyClient;//星星的中心位置
switch (message){case WM_PAINT:hdc = BeginPaint(hwnd, &ps);GetClientRect(hwnd, &rect);
cxClient = rect.right / 2;cyClient = rect.bottom / 2;
//擷取五個頂點//左上頂點apt2[0].x = cxClient - (int)(R*cos(PI / 5));apt2[0].y = cyClient - (int)(R*sin(PI / 5));
//右上頂點apt2[1].x = cxClient + (int)(R*cos(PI / 5));apt2[1].y = cyClient - (int)(R*sin(PI / 5));
//左下頂點apt2[2].x = cxClient - (int)(R*cos(PI / 5));apt2[2].y = cyClient + (int)(R*sin(PI / 5));
//上頂點apt2[3].x = cxClient;apt2[3].y = cyClient - R;
//右下頂點apt2[4].x = cxClient + (int)(R*cos(PI / 5));apt2[4].y = cyClient + (int)(R*sin(PI / 5));
 hBrush = CreateSolidBrush(RGB(255,255,0)); hOldBrush = SelectObject(hdc, hBrush);
//SetPolyFillMode(hdc, ALTERNATE);//交替填充SetPolyFillMode(hdc, WINDING);//螺旋填充,填充所有能夠一筆完成的圖形//  Polygon(hdc, apt2, 5);// Polyline(hdc, apt2, 5);//  SelectObject(hdc, hOldBrush); DeleteObject(hOldBrush);EndPaint(hwnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);return 0;}

return DefWindowProc(hwnd, message, wParam, lParam);}

學習windows編程 day4 之 多邊矩形填充

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.