weipiang 发表于 2020-3-31 00:32:47

C语言WINAPI学习 佩奇

一只佩奇


#include <windows.h>

void drawBeizer(HDC,int,int,int,int,int,int,int,int,int,int);
void drawEllipse(HDC,int,int,int,int,int,int,int);
void drawRoundRect(HDC,int,int,int,int,int,int,int,int,int);
void drawArc(HDC,int,int,int,int,int,int,int,int,int,int);
void drawbeiji(HDC,int);

void drawBeizer(HDC hdc,int p1x,int p1y,int p2x,int p2y,int p3x,int p3y,int p4x,int p4y,int l_c,int l_w){
        POINT a[]={p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y};
        HPEN hpen=CreatePen(PS_SOLID,l_w,l_c);
        HGDIOBJ olpen=SelectObject(hdc,hpen);
        PolyBezier(hdc,a,4);
        SelectObject(hdc,olpen);
        DeleteObject(hpen);
}
void drawEllipse(HDC hdc,int l,int t,int r,int b,int c_p,int c_l,int l_w){
        HBRUSH hbrush=CreateSolidBrush(c_p);
        HPEN hpen=CreatePen(PS_SOLID,l_w,c_l);
        HGDIOBJ oldbrush=SelectObject(hdc,hbrush);
        HGDIOBJ oldpen=SelectObject(hdc,hpen);
        if(c_p==NULL_BRUSH){
                SelectObject(hdc,GetStockObject(c_p));
        }
        Ellipse(hdc,l,t,r,b);
        SelectObject(hdc,oldbrush);
        SelectObject(hdc,oldpen);
        DeleteObject(hbrush);
        DeleteObject(hpen);
       
}
void drawRoundRect(HDC hdc,int l,int t,int r,int b,int rx,int ry,int c_p,int c_l,int l_w){
        HBRUSH hbrush=CreateSolidBrush(c_p);
        HPEN hpen=CreatePen(PS_SOLID,l_w,c_l);
        HGDIOBJ oldbrush=SelectObject(hdc,hbrush);
        HGDIOBJ oldpen=SelectObject(hdc,hpen);
        if(c_p==NULL_BRUSH){
                SelectObject(hdc,GetStockObject(c_p));
        }
        RoundRect(hdc,l,t,r,b,rx,ry);
       
        SelectObject(hdc,oldbrush);
        SelectObject(hdc,oldpen);
        DeleteObject(hbrush);
        DeleteObject(hpen);
       
}
void drawArc(HDC hdc,int l,int t,int r,int b,int sx,int sy,int ex,int ey,int c_l,int l_w){
        HPEN hpen=CreatePen(PS_SOLID,l_w,c_l);
        HGDIOBJ olpen=SelectObject(hdc,hpen);
        Arc(hdc,l,t,r,b,sx,sy,ex,ey);
        SelectObject(hdc,olpen);
        DeleteObject(hpen);
}
void drawbeiji(HDC hdc,int color){
        drawBeizer(hdc,213,167,185,177,176,265,207,280,color,4);
        drawBeizer(hdc,322,202,372,276,257,328,207,280,color,4);
        drawBeizer(hdc,350,130,413,154,345,219,311,208,color,4);
        drawBeizer(hdc,350,130,306,129,323,205,370,181,color,4);
        drawBeizer(hdc,350,130,306,131,235,144,208,173,color,4);
        drawBeizer(hdc,312,228,319,259,273,275,250,238,color,4);
        drawBeizer(hdc,235,163,222,102,157,128,216,175,color,4);
        drawBeizer(hdc,266,143,263,89,211,96,246,148,color,4);
        drawBeizer(hdc,210,278,181,297,166,355,175,393,color,4);
        drawBeizer(hdc,348,392,305,391,225,391,175,393,color,4);
        drawBeizer(hdc,313,282,336,307,349,357,348,392,color,4);
        drawBeizer(hdc,324,296,345,302,366,316,385,326,color,4);
        drawBeizer(hdc,199,298,176,305,158,314,140,324,color,4);
        drawBeizer(hdc,177,365,153,381,139,332,166,344,color,4);
        drawBeizer(hdc,137,352,142,381,174,367,166,344,color,4);
        drawBeizer(hdc,223,391,223,403,223,418,222,426,color,4);
        drawBeizer(hdc,295,391,293,405,295,420,296,426,color,4);
        drawEllipse(hdc,246,158,271,189,NULL_BRUSH,color,1);
        drawEllipse(hdc,279,147,304,170,NULL_BRUSH,color,1);
        drawEllipse(hdc,250,169,261,180,color,color,1);
        drawEllipse(hdc,284,153,295,164,color,color,1);
        drawEllipse(hdc,337,152,349,165,color,color,1);
        drawEllipse(hdc,354,149,366,162,color,color,1);
        drawRoundRect(hdc,205,421,254,440,20,20,color,color,1);
        drawRoundRect(hdc,282,421,331,440,20,20,color,color,1);
        drawArc(hdc,154,344,115,311,196,344,133,285,color,4);
        drawArc(hdc,389,339,364,307,402,291,349,345,color,4);       
}

/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
//        PolyBezier()
        PAINTSTRUCT ps;
        HDC hdc;
        switch(Message) {
               
                /* trap the WM_CLOSE (clicking X) message, and actually tell the window to close */
                case WM_PAINT:{
                        hdc=BeginPaint(hwnd,&ps);
                        drawbeiji(hdc,RGB(255,0,0));
                        EndPaint(hwnd,&ps);
                        break;
                }
                case WM_CLOSE: {
                        DestroyWindow(hwnd);
                        break;
                }
               
                /* Upon destruction, tell the main thread to stop */
                case WM_DESTROY: {
                        PostQuitMessage(0);
                        break;
                }
               
                /* All other messages (a lot of them) are processed using default procedures */
                default:
                        return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        WNDCLASSEX wc; /* A properties struct of our window */
        HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
        MSG Msg; /* A temporary location for all messages */

        /* zero out the struct and set the stuff we want to modify */
        memset(&wc,0,sizeof(wc));
        wc.cbSize               = sizeof(WNDCLASSEX);
        wc.lpfnWndProc       = WndProc; /* This is where we will send messages to */
        wc.hInstance       = hInstance;
        wc.hCursor               = LoadCursor(NULL, IDC_ARROW);
       
        /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszClassName = "WindowClass";
        wc.hIcon               = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
        wc.hIconSm               = LoadIcon(NULL, IDI_APPLICATION);

        if(!RegisterClassEx(&wc)) {
                MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
                return 0;
        }

        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, /* x */
                CW_USEDEFAULT, /* y */
                640, /* width */
                480, /* height */
                NULL,NULL,hInstance,NULL);

        if(hwnd == NULL) {
                MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
                return 0;
        }

        /*
                This is the heart of our program where all input is processed and
                sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
                this loop will not produre unreasonably CPU usage
        */
        while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
                TranslateMessage(&Msg); /* Translate keycodes to chars if present */
                DispatchMessage(&Msg); /* Send it to WndProc */
        }
        return Msg.wParam;
}

页: [1]
查看完整版本: C语言WINAPI学习 佩奇