我在visual-c++中创建了一个win32应用程序,但是这个程序没有打印鼠标坐标,所有其他事件都工作正常。谁能告诉我如何在visual-c++ win32应用程序中获取鼠标坐标?
希望得到快速和积极的回应。
// ttt.cpp : Defines the entry point for the application.
// TO Demonstrate the Mouse Events
#include "windows.h"
#include "stdafx.h"
#include "stdio.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int x,y;
LPCWSTR msgdown = (LPCWSTR)L"Left Mouse Button Down" ;
LPCWSTR msgup = (LPCWSTR)L"Left Mouse Button UP" ;
LPCWSTR msgdblclk = (LPCWSTR)L"Left Mouse Button Dbl clk" ;
LPCWSTR rmsgdown = (LPCWSTR)L"Right Mouse Button Down" ;
LPCWSTR rmsgup = (LPCWSTR)L"Right Mouse Button UP" ;
LPCWSTR rmsgdblclk = (LPCWSTR)L"Right Mouse Button Dbl clk" ;
LPCWSTR rwheel = (LPCWSTR)L"Mousescroll" ;
//LPCWSTR txtmsg = (LPCWSTR)L"position" ;
LPCWSTR mouse = (LPCWSTR)L"Mouse" ;
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
MessageBox(hWnd,msgdown,mouse,MB_OK);
break;
case WM_LBUTTONUP:
MessageBox(hWnd,msgup,mouse,MB_OK);
break;
case WM_LBUTTONDBLCLK:
MessageBox(hWnd,msgdblclk,mouse,MB_OK);
break;
case WM_RBUTTONUP:
MessageBox(hWnd,rmsgup,mouse,MB_OK);
break;
case WM_RBUTTONDOWN:
MessageBox(hWnd,rmsgdown,mouse,MB_OK);
break;
case WM_RBUTTONDBLCLK:
MessageBox(hWnd,rmsgdblclk,mouse,MB_OK);
break;
case WM_MOUSEWHEEL:
MessageBox(hWnd,rwheel,mouse,MB_OK);
break;
char text[50];
POINT p;
sprintf(text,"Mouse Position: X=%d, Y=%d",p.x,p.y);
LPCWSTR textmsg = (LPCWSTR)text;
SetWindowText(hWnd,textmsg);
break;
/*POINT pt;
GetCursorPos(&pt);
int a = (int)pt.x;
int b = (int)pt.y;*/
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
LPCTSTR className=(LPCTSTR)"Mouse Test";
WNDCLASSEX wc;
wc.cbSize =sizeof(WNDCLASSEX);
wc.style =CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
MessageBoxA(NULL,"mouse events","mouse",MB_OK);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,(LPCWSTR)"Error Registering Class",(LPCWSTR)"Error RegisterClassEx",MB_OK | MB_ICONERROR);
return 1;
}
HWND hwmd = CreateWindowEx(0,className,(LPCWSTR)L"Mouse Test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,400,300,NULL,NULL,hInstance,NULL);
ShowWindow(hwmd,SW_SHOWDEFAULT);
UpdateWindow(hwmd);
if(!hwmd)
{
MessageBox(NULL,(LPCWSTR)"Error Creating Window",(LPCWSTR)"Error CreateWindowEx",MB_OK | MB_ICONERROR);
return 1;
}
MSG msg;
while(GetMessage(&msg,NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}发布于 2011-02-03 22:00:50
正如我在评论中提到的,下面的代码块1)永远不会被访问,2)即使您将其设置为可访问,它也不会工作:
case WM_MOUSEWHEEL:
MessageBox(hWnd,rwheel,mouse,MB_OK);
break;
char text[50]; // no case to get you here!
POINT p;
sprintf(text,"Mouse Position: X=%d, Y=%d",p.x,p.y);
LPCWSTR textmsg = (LPCWSTR)text; // will not work!
SetWindowText(hWnd,textmsg);
break;发布于 2011-02-03 23:39:04
特别是在WM_MOUSEWHEEL消息中,光标坐标是在lParam中传递的。LOWORD(lParam)应该是x,HIWORD(lParam)应该是y。坐标是相对于屏幕的,而不是相对于窗口的。使用ScreenToClient()进行转换。
在WM_xBUTTONDOWN/UP和WM_MOUSEMOVE中lParam的含义是相同的,但坐标是相对于窗口的工作区的。
发布于 2011-02-03 23:25:18
使用GetCursorInfo()获取鼠标在任意时间点的位置。如果只想跟踪鼠标实际移动的时间,请处理WM_MOUSEMOVE。
See this上一个问题/答案以了解更多信息。
正如其他人所说,您需要用字符串修复Unicode/char *问题。
https://stackoverflow.com/questions/4886978
复制相似问题