Win32 Api 学习记录-Ocrosoft
浙江财经大学
信工学院ACM集训队

Win32 Api 学习记录

本文由 Ocrosoft 于 2018-05-07 19:45:18 发表

摘要:鼠标捕获,SetCapture,ReleaseCapture函数的使用

1.1.画矩形的例子,当鼠标移动到窗口外时仍然能够获得鼠标消息

1.2.问题

注意到后面WM_MOUSEMOVE消息处理的时候,调用了两次DrawBoxOutline函数,注释掉任意一句,都会有上次图像残留的问题,因为使用的NULL_BRUSH,(就算用WHITE_BRUSH,缩小的时候也是一样),

这个问题在之前画贝塞尔曲线的时候也有(https://www.ocrosoft.com/?p=2545),解决方法是Select一个WHITE_PEN,绘制一遍原来的曲线,然后Select原来的BLACK_PEN(这种方法在背景色不是白色的时候不能用),

这里也调用了函数两次,但是没有SelectObject,其实这里的处理方式更好,背景色不是白色也可以,

SetROP2(hdc, R2_NOT);

这一句设置线的颜色为背景色的反色,假设背景色是白色,那么现在显示出来的是黑色的线,接下来在处理MOUSEMOVE的时候,第一次调用DrawBoxOutline,设置反色,线的颜色就会使黑色的反色,白色,(取反再取反就是背景色),

所以相当于用背景色又画了原来的图形,覆盖掉了。

1.3.源码

#include <windows.h>

#define LPARAMX ((int)(short)LOWORD(lParam))
#define LPARAMY ((int)(short)HIWORD(lParam))

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

static char *szXPos, *szYPos, *szCated;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("BlokOut");
    HWND hwnd;
    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(nullptr, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = nullptr;
    wndclass.lpszClassName = szAppName;

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

    hwnd = CreateWindow(szAppName, TEXT("BlokOut"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

void DrawBoxOutline(HWND hwnd, POINT ptBeg, POINT ptEnd)
{
    HDC hdc;
    hdc = GetDC(hwnd);
    SetROP2(hdc, R2_NOT); // 设置前景色为背景色的反色
    SelectObject(hdc, GetStockObject(NULL_BRUSH)); // 空刷子,画出来就是个矩形框
    Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
    ReleaseDC(hwnd, hdc);
}

void to_str(char* pRet, int iVal)
{
    BOOL fNega = iVal < 0 ? true : false;
    iVal = fNega ? -iVal : iVal;
    for (int i = 5; i >= 0; i--)
        pRet[i] = (i == 5 ? '\0' : ' ');
    if (iVal == 0)
        pRet[4] = '0';
    int i;
    for (i = 4; iVal; iVal /= 10, i--)
        pRet[i] = (iVal % 10) + '0';
    if (fNega)
        pRet[i] = '-';
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static BOOL fBlocking, fValidBox;
    static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd;
    HDC hdc;
    PAINTSTRUCT ps;

    switch (message)
    {
    case WM_LBUTTONDOWN:
        ptBeg.x = ptEnd.x = LOWORD(lParam);
        ptBeg.y = ptEnd.y = HIWORD(lParam);

        DrawBoxOutline(hwnd, ptBeg, ptEnd);

        SetCapture(hwnd); // 开始捕获鼠标
        SetCursor(LoadCursor(NULL, IDC_CROSS)); // 改为十字鼠标

        fBlocking = TRUE;
        return 0;

    case WM_MOUSEMOVE:
        if(fBlocking)
        {
            SetCursor(LoadCursor(NULL, IDC_CROSS));

            DrawBoxOutline(hwnd, ptBeg, ptEnd);

            ptEnd.x = LPARAMX; // WORD是unsigned short,不能表示负数
            ptEnd.y = LPARAMY;

            DrawBoxOutline(hwnd, ptBeg, ptEnd);
        }

        if (szXPos == nullptr)
        {
            szXPos = new char[6];
            szYPos = new char[6];
            szCated = new char[5 + 2 + 5 + 1];
        }

        to_str(szXPos, LPARAMX);
        to_str(szYPos, LPARAMY);
        strcpy(szCated, szXPos);
        strcat(szCated, ", ");
        strcat(szCated, szYPos);
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;

    case WM_LBUTTONUP:
        if (fBlocking)
        {
            DrawBoxOutline(hwnd, ptBeg, ptEnd);

            ptBoxBeg = ptBeg;
            ptBoxEnd.x = LPARAMX;
            ptBoxEnd.y = LPARAMY;

            ReleaseCapture(); // 在BUTTONUP消息时解除鼠标捕获
            SetCursor(LoadCursor(NULL, IDC_ARROW));

            fBlocking = FALSE;
            fValidBox = TRUE;

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

    case WM_CHAR:
        if (fBlocking && wParam == '\x1B') // ESC, 取消绘图
        {
            DrawBoxOutline(hwnd, ptBeg, ptEnd);
            ReleaseCapture();
            SetCursor(LoadCursor(NULL, IDC_ARROW));

            fBlocking = FALSE;
        }
        return 0;

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

        if (fValidBox)
        {
            SelectObject(hdc, GetStockObject(BLACK_BRUSH));
            Rectangle(hdc, ptBoxBeg.x, ptBoxBeg.y, ptBoxEnd.x, ptBoxEnd.y);
        }

        if (fBlocking) // 不需要也没什么问题
        {
            SetROP2(hdc, R2_NOT);
            SelectObject(hdc, GetStockObject(NULL_BRUSH));
            Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
        }

        TextOut(hdc, 0, 0, szCated, 5 + 2 + 5);

        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
} 

欢迎转载,请保留出处与链接。Ocrosoft » Win32 Api 学习记录

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址