0

可能重复:
获取当前光标位置

我创建这样的按钮:

button = CreateWindowEx(NULL,_T("Button"),NULL,
            WS_CHILD | WS_VISIBLE | BS_BITMAP | BS_NOTIFY ,
            pos.x, pos.y, BTN_SIZE, BTN_SIZE,
            hWndDlg, (HMENU)id,hIns, NULL);

我想拖动那个按钮,不知道如何让鼠标位置 X,Y 相对于 DialogWindow?感谢您的任何帮助?

4

2 回答 2

1

您收到的消息包含一个POINT名为 的结构pt。至少对于任何源自鼠标的消息,这是消息发出时鼠标的位置。那是在屏幕坐标中,所以你需要使用ScreenToClientor (最好)MapWindowPoints来获取相对于你的对话框的坐标。

于 2012-09-08T12:01:32.413 回答
0

试试这个代码,看看你能不能弄清楚一些事情。

#include <windows.h>
#include <stdio.h>

#define IDC_MAIN_BUTTON 101         // Button identifier
#define IDC_MAIN_EDIT   102         // Edit box identifier
HWND hEdit;

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
    WNDCLASSEX wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    wClass.cbClsExtra=NULL;
    wClass.cbSize=sizeof(WNDCLASSEX);
    wClass.cbWndExtra=NULL;
    wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon=NULL;
    wClass.hIconSm=NULL;
    wClass.hInstance=hInst;
    wClass.lpfnWndProc=(WNDPROC)WinProc;
    wClass.lpszClassName="Window Class";
    wClass.lpszMenuName=NULL;
    wClass.style=CS_HREDRAW|CS_VREDRAW;

    if(!RegisterClassEx(&wClass))
    {
        int nResult=GetLastError();
        MessageBox(NULL, "Window class creation failed\r\n", "Window Class Failed", MB_ICONERROR);
    }

    HWND hWnd=CreateWindowEx(NULL,  "Window Class", "Mouse coords", WS_OVERLAPPEDWINDOW, 200, 200,  640, 480, NULL, NULL, hInst, NULL);

    if(!hWnd)
    {
        int nResult=GetLastError();

        MessageBox(NULL,
            "Window creation failed\r\n",
            "Window Creation Failed",
            MB_ICONERROR);
    }

    ShowWindow(hWnd,nShowCmd);

    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));

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

    return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    char buffer[32];

    POINT mouse;




    switch(msg)
    {
            case WM_CREATE:
            {
                // Create an edit box
                hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT",  "",     WS_CHILD|WS_VISIBLE|    ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL, 50, 100, 200,   100, hWnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
                HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
                SendMessage(hEdit,  WM_SETFONT, (WPARAM)hfDefault,  MAKELPARAM(FALSE,0));
                SendMessage(hEdit,  WM_SETTEXT, NULL,   (LPARAM)"Insert text here...");

                // Create a push button
                HWND hWndButton=CreateWindowEx(NULL, "BUTTON",  "OK", WS_TABSTOP|WS_VISIBLE| WS_CHILD|BS_DEFPUSHBUTTON, 50, 220,  100, 24,  hWnd, (HMENU)IDC_MAIN_BUTTON, GetModuleHandle(NULL), NULL);
                SendMessage(hWndButton, WM_SETFONT, (WPARAM)hfDefault,  MAKELPARAM(FALSE,0));
            }
            break;

            case WM_LBUTTONDOWN:
            {

                mouse.x = LOWORD(lParam); 
                mouse.y = HIWORD(lParam);     

                ScreenToClient(hWnd, &mouse);

                sprintf(buffer,"Mouse coords:  %dx%d",mouse.x,mouse.y);

                MessageBox(NULL, buffer, "Information", MB_ICONINFORMATION);

            }
            break;


            case WM_COMMAND:


                switch(LOWORD(wParam))
                {

                    case IDC_MAIN_BUTTON:
                    {
                        mouse.x = LOWORD(lParam); 
                        mouse.y = HIWORD(lParam);      

                        ScreenToClient(hWnd, &mouse);

                        SendMessage(hEdit,  WM_GETTEXT, sizeof(buffer)/sizeof(buffer[0]),   reinterpret_cast<LPARAM>(buffer));
                        sprintf(buffer,"Mouse coords:  %dx%d",mouse.x,mouse.y);
                        MessageBox(NULL, buffer, "Mouse coords", MB_ICONINFORMATION);
                    }
                    break;
                }
                break;

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

    return DefWindowProc(hWnd,msg,wParam,lParam);
}
于 2012-09-08T11:59:33.173 回答