Win32Api Windows移动

时间:2018-10-07 10:11:48

标签: winapi

按下鼠标中键时如何移动窗口?

如果使用鼠标左键,则为

case WM_LBUTTONDOWN:
{
    SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
return 0;

但不适用于中间按钮

 case WM_MBUTTONDOWN:
    {
        SendMessage(hwnd, WM_NCMBUTTONDOWN, HTCAPTION, 0);
    }
return 0;

但是我不确定WM_NCMBUTTONDOWN

我没有其他型号,请需要帮助

1 个答案:

答案 0 :(得分:0)

恐怕您必须用困难的方式做到这一点。我尝试了所有明显的操作,并且如果您要用鼠标左键以外的任何方式拖动窗口,WM_NCHITTEST不会将其剪切。

这里有一些对我有用的代码(在Windows 10上测试)。为了简洁起见,我已经排除了错误检查:

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>

// WndProc
LRESULT CALLBACK MyWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static int xClick, yClick;

    switch (uMsg)
    {
        case WM_NCMBUTTONDOWN:
        case WM_LBUTTONDOWN:
        case WM_MBUTTONDOWN:
        {
            SetCapture (hWnd);
            POINT pt = { GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam) };
            if (uMsg == WM_NCMBUTTONDOWN)
                ScreenToClient (hWnd, &pt);
            xClick = pt.x;
            yClick = pt.y;
            return 0;
        }

        case WM_LBUTTONUP:
        case WM_MBUTTONUP:
            ReleaseCapture ();
            return 0;

        case WM_MOUSEMOVE:
        {
            if (GetCapture() == hWnd)
            {
                RECT wr;
                GetWindowRect (hWnd, &wr);
                int xMouse = GET_X_LPARAM (lParam);
                int yMouse = GET_Y_LPARAM (lParam);
                int xWindow = wr.left + xMouse - xClick;
                int yWindow = wr.top + yMouse - yClick;
                SetWindowPos (hWnd, NULL ,xWindow, yWindow, 0, 0 , SWP_NOSIZE | SWP_NOZORDER);
            }
            return 0;
        }
    }

    return DefWindowProc (hWnd, uMsg, wParam, lParam);
}


// WinMain
int CALLBACK _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    HINSTANCE hInstance = NULL;
    WNDCLASS wc = { };
    wc.lpszClassName = __T ("MyWindowClass");
    wc.lpfnWndProc = MyWndProc;
    RegisterClass (&wc);
    HWND hWnd = CreateWindow (wc.lpszClassName, __T ("My Window"),
        WS_CAPTION | WS_BORDER | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, hInstance, 0);

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

希望这会有所帮助。