学习windows编程 day3 之窗口绘画二:边框绘制函数

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

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 int cxClient, cyClient;
    static int cxClick, cyClick;
    static POINT apt[4];
    static int flag = 0;

    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        //贝塞尔曲线需要四个点
        apt[0].x = cxClient / 4;
        apt[0].y = cyClient / 2;

        apt[1].x = cxClient / 2;
        apt[1].y = cyClient / 4;

        apt[2].x = cxClient / 2;
        apt[2].y = cyClient *3/4;

        apt[3].x = cxClient *3/ 4;
        apt[3].y = cyClient / 2;

        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd,&ps);
        SelectObject(hdc, GetStockObject(BLACK_PEN));
        //画贝塞尔曲线
        PolyBezier(hdc, apt,4);

//边框绘制函数:内部可以填充
        //画矩形
        SelectObject(hdc, GetStockObject(BLACK_BRUSH));//会填充
        Rectangle(hdc, 100, 100, 300, 300);
        //画椭圆
        SelectObject(hdc, GetStockObject(WHITE_BRUSH));
        Ellipse(hdc, 100, 100, 300, 300);
        //画矩形,带圆角
        SelectObject(hdc, GetStockObject(BLACK_BRUSH));
        RoundRect(hdc, 500, 100, 600, 200, 10, 10);
        //画弧线
        Arc(hdc, 700, 100, 900, 200, 800, 50, 600, 150);
        //封闭弧线两点        和下面一样都是第一点开始,第二点结束,默认逆时针
        Chord(hdc, 700, 100, 900, 200, 800, 50, 600, 150);
        //画扇形,默认逆时针(中点到第一个点的连线逆时针扫描到第二条线)
        Pie(hdc, 700, 100, 900, 200 , 800, 250, 1000, 50);

        EndPaint(hwnd, hdc);
        break;
    case WM_LBUTTONDOWN:
        //左键修改第一和第二点位置
        cxClick = LOWORD(lParam);
        cyClick = HIWORD(lParam);
        if (flag == 0)
        {
            apt[0].x = cxClick;
            apt[0].y = cyClick;
            flag = 1;
        }
        else
        {
            apt[1].x = cxClick;
            apt[1].y = cyClick;
            flag = 0;
        }
        InvalidateRect(hwnd, NULL, TRUE);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }


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

 

posted @ 2018-03-04 21:48  山上有风景  阅读(573)  评论(0编辑  收藏  举报