急等,多谢!!!
解决方案 »
- MFC画坐标轴及动态曲线
- opencv2.X cvcam.h cvcam.lib cvcam.dll
- MFC基础请教
- 如何重绘Dialog在任务栏的图标
- 开启XP风格情况下,为什么同一个Button放在主窗体上和放在一个GroupBox上面显示效果不同?奇怪!
- 救命:我的List Control在98下正常,在xp下就出问题了
- 请介绍几个国外有关图形编程的网址!!!
- 怎么连接数据库是跳出“无效指针”的错误?有源码,大家帮帮忙吧,我的头已经很大了
- 请问ActiveX怎样才能使控件的字体和容器的字体一样?
- 线程在哪些情况下会退出?
- 新手上路,提个简单的问题,希望能得到高手的指点.
- CJLibrary609怎么用中文
重载了OnMouseMove(....,Point)时,那个Point就是鼠标的坐标:)
#include <windows.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MousePos");
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(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass))
{
MessageBox(NULL,TEXT("窗口类注册失败!"),szAppName,MB_ICONERROR);
return 0;
} hWnd = CreateWindow(szAppName,NULL,
WS_CHILD | WS_VISIBLE,
0,0,
60,20,
GetDesktopWindow(),NULL,hInstance,NULL);
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd); while (GetMessage(&msg,NULL,0,0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
POINT pt;
TCHAR Info[MAX_PATH]; switch ( message )
{
case WM_CREATE:
SetTimer(hWnd,1000,100,NULL);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
GetCursorPos(&pt);
wsprintf(Info,TEXT("(%3d,%3d)"),pt.x,pt.y);
DrawText(hdc,Info,-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER );
EndPaint(hWnd,&ps);
return 0;
case WM_TIMER:
GetCursorPos(&pt);
SetWindowPos(hWnd,NULL,pt.x,pt.y,0,0,SWP_NOZORDER | SWP_NOSIZE);
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case WM_DESTROY :
KillTimer(hWnd,1000);
PostQuitMessage(0);
return 0;
} return DefWindowProc(hWnd,message,wParam,lParam);
}
用计时器每隔一小段时间取鼠标的位置就可以了。
用WM_MOUSEMOVE消息是不行的,因为只有鼠标在窗口上面移动时,窗口才收到WM_MOUSEMOVE消息,要是鼠标不在窗口上面就收不到消息了。
除了我给的用计时器解决的方法外,应该还可以用ToolTip控件,鼠标钩子等方法解决。
2. 挂一个鼠标钩子
1. SetCapture/ReleaseCapture/WM_MOUSEMOVE
是不行的,因为只有鼠标键被按下时,鼠标捕捉才有效,你不能要求用户在移动鼠标的时候总是按下一个鼠标键吧?
--------------------------------------------------------------------------------
The SetCapture function sets the mouse capture to the specified window belonging to the current thread. SetCapture captures mouse input either when the mouse is over the capturing window, or when the mouse button was pressed while the mouse was over the capturing window and the button is still down. Only one window at a time can capture the mouse.If the mouse cursor is over a window created by another thread, the system will direct mouse input to the specified window only if a mouse button is down.
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
dc.SetBkColor(RGB(255, 255, 230));
CString strPoint;
strPoint.Format("x=%d,y=%d", m_ptOld);
CSize szText;
szText = dc.GetTextExtent(strPoint);
CRect rcText;
int iOffset = szText.cx / 2;
rcText.SetRect(m_ptOld.x - iOffset, m_ptOld.y - szText.cy, m_ptOld.x + szText.cx - iOffset, m_ptOld.y);
InvalidateRect(rcText, TRUE); strPoint.Format("x=%d,y=%d", point);
szText = dc.GetTextExtent(strPoint);
iOffset = szText.cx / 2;
rcText.SetRect(point.x - iOffset, point.y - szText.cy, point.x + szText.cx - iOffset, point.y);
ValidateRect(rcText);
dc.TextOut(point.x - iOffset, point.y - szText.cy, strPoint);
m_ptOld = point; CDialog::OnMouseMove(nFlags, point);
}