I have created a win32 application in visual-c++ but this programing not printing the mouse coordinates all other events working properly can any one tell me how to get mouse coordinates in visual-c++ win32 application ?
hoping for quick and positive response.
// 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;
}
As I mentioned in the comment, the following code block 1) will never be reached, and 2) will not work even if you make it reachable:
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;
Specifically in WM_MOUSEWHEEL message, the cursor coordinates are passed in lParam. LOWORD(lParam)
would be x, HIWORD(lParam)
would be y. Coordinates are relative to the screen, not to your window. Use ScreenToClient() to convert.
The meaning of lParam in WM_xBUTTONDOWN/UP and WM_MOUSEMOVE is the same, but the coordinates are relative to the client area of your window.
Use GetCursorInfo() to get the location of the mouse at any point in time. If you want to track only when the mouse actually moves, handle WM_MOUSEMOVE
.
See this previous question/answer for more information.
And as others have said, you'll need to fix your Unicode/char * issues with your strings.
精彩评论