首页 > 系统编程 > GDI 线段绘制示例程序
2018
05-30

GDI 线段绘制示例程序

#include <windows.h>
#include <strsafe.h>
#include <stack>

typedef struct tagLINE {
    POINT ptStart;
    POINT ptEnd;
}LINE;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    TCHAR szBuffer[100];
    size_t nLength;
    static int cxClient, cyClient;
    static BOOL fDrawing;
    static LINE arrLine[1000];
    static int length;
    static LINE line;
    int i;

    switch (message)
    {
    case WM_CREATE:
        length = 0;
        fDrawing = FALSE;
        return 0;

    case WM_LBUTTONUP:
        
        if (fDrawing) {
            line.ptEnd.x = LOWORD(lParam);
            line.ptEnd.y = HIWORD(lParam);
            arrLine[length] = line;
            length++;
        }
        else {
            line.ptStart.x = LOWORD(lParam);
            line.ptStart.y = HIWORD(lParam);
        }

        fDrawing = !fDrawing;

        InvalidateRect(hwnd, NULL, TRUE);
        
        return 0;

    case WM_SIZE:
        hdc = GetDC(hwnd);

        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        ReleaseDC(hwnd, hdc);
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);

        for (i = 0; i < length; i++) {
            MoveToEx(hdc, arrLine[i].ptStart.x, arrLine[i].ptStart.y, NULL);
            LineTo(hdc, arrLine[i].ptEnd.x, arrLine[i].ptEnd.y);
        }

        StringCchPrintf(szBuffer, sizeof(szBuffer) / sizeof(TCHAR), TEXT("Size: %d x %d"), cxClient, cyClient);
        StringCchLength(szBuffer, sizeof(szBuffer) / sizeof(TCHAR), &nLength);

        SetTextAlign(hdc, TA_TOP | TA_RIGHT);
        TextOut(hdc, cxClient, 0, szBuffer, nLength);

        EndPaint(hwnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

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


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdLine)
{
    LPCTSTR lpszClassName = TEXT("Demo");
    LPCTSTR lpszWindowName = TEXT("Demo Window");

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

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

    HWND hwnd = CreateWindow(
        lpszClassName,
        lpszWindowName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    ShowWindow(hwnd, nCmdLine);
    UpdateWindow(hwnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

打赏 赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

最后编辑:
作者:Yenyu
Yenyu
编程爱好者

留下一个回复

你的email不会被公开。