点和线的绘制一

#include<windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    HWND hwnd;
    static TCHAR szAppName[] = TEXT("Window Base App");
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(
        szAppName,
        TEXT("My Window App!"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    ShowWindow(hwnd, iCmdShow);
    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;
    int x, y;
    POINT apt[5] = { 100, 100, 200, 100, 200, 200, 100, 200, 100, 100 };
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        
        
        /*for (x = 0; x <= rect.right; x += 100)
        {
            MoveToEx(hdc, x, 0, NULL);                //改变当前位置
            LineTo(hdc, x, rect.bottom);
        }

        for (y = 0; y <= rect.bottom; y += 100)
        {
            MoveToEx(hdc, 0, y, NULL);
            LineTo(hdc, rect.right, y);
        }*/
        
        
        /*MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
        for (int i = 1; i < 5; i++)
        {
            LineTo(hdc, apt[i].x, apt[i].y);
        }*/
        
        
        /*Polyline(hdc, apt, 5);            */            //不使用和改变当前位置

        MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
        PolylineTo(hdc, apt + 1, 4);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
如何想在释放设备环境时保存设备环境属性,就需要wndclass.style = CS_OWNDC
 
使用CS_OWNDC时,只需要初始化一次设备环境属性,在再次改变之前,属性会一直有效。
CS_OWNDC只会影响GetDC和BeginPaint,而GetWindowDC不影响。
 
如果想改变设备环境属性,然后使用变更后的属性进行绘制,接着再恢复原来的属性,
则可以idSaved  = SaveDC(hdc)进行属性保存,现在可以改变属性并进行绘图,然后调用RestoreDC(hdc, idSaved)恢复属性。可以在调用RestoreDC之前多次调用SaveDC。RestoreDC(hdc, -1)可以恢复到最近依次有SaveDC保存的属性。
原文地址:https://www.cnblogs.com/Lu3ky-Athena/p/13647181.html