处理鼠标移动事件

gerrardgld 2009-09-12 04:23:36
我想实现在鼠标移时输出它的坐标,这段代码错在哪啊?谢谢

case WM_MOUSEMOVE:
HDC hdc1;
hdc1=GetDC(hwnd);
POINT point;

GetCursorPos(&point );// address of structure for cursor position
int x=point.x;
int y=point.y;

TextOutA(...);
...全文
138 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gerrardgld 2009-09-12
  • 打赏
  • 举报
回复
#include<windows.h>
#include<stdio.h>


LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="kop";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd;

hwnd=CreateWindowEx(WS_EX_WINDOWEDGE,"kop","kopkop",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;

while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"weixin",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","weixin",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"维新培训",strlen("维新培训"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:


default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}


帮我实现下WM_MOUSEMOVE事件 在窗口0,0位置输出鼠标坐标,谢谢,我超级新手。。。
wenqinwuhai 2009-09-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dirdirdir3 的回复:]
不需要GetCursorPos(),wm_mousemove本身就有坐标....
int x=LOWORD(lParam);
int y=HIWORD(lParam);

什么错了?
[/Quote] 饿
副组长 2009-09-12
  • 打赏
  • 举报
回复
哦,在switch case语句中间是不许定义变量的,CString strText;或者int x; int y;等都是不可以的。
fish_gao 2009-09-12
  • 打赏
  • 举报
回复
注意

switch ( index )
{
case 0:
int x = 0;
int y = 0;
break;
}



switch ( index )
{
case 0:
{
int x = 0;
int y = 0;
}
break;
}

之间的差别,还有1楼的方法已经可以的了,不用自己去获取鼠标位置
副组长 2009-09-12
  • 打赏
  • 举报
回复
不知道你想做什么,鼠标移到哪儿就在哪儿输出字符串吗?这样光写不删会满页都是字的。

光GetDC 没有 ReleaseDC。
gerrardgld 2009-09-12
  • 打赏
  • 举报
回复
1>c:\documents and settings\杰拉德\my documents\visual studio 2008\projects\myfirst\myfirst\myfirst.cpp(88) : error C2361: “default”标签跳过“y”的初始化操作
1> c:\documents and settings\杰拉德\my documents\visual studio 2008\projects\myfirst\myfirst\myfirst.cpp(81) : 参见“y”的声明
1>c:\documents and settings\杰拉德\my documents\visual studio 2008\projects\myfirst\myfirst\myfirst.cpp(88) : error C2361: “default”标签跳过“x”的初始化操作
1> c:\documents and settings\杰拉德\my documents\visual studio 2008\projects\myfirst\myfirst\myfirst.cpp(80) : 参见“x”的声明
Gujinguo 2009-09-12
  • 打赏
  • 举报
回复
case WM_MOUSEMOVE:
{
HDC hdc1 = GetDC(hwnd);
....
CString strText;
strText.Format(_T("Position %d, %d"), x, y);
TextOutA(hdc1, 0, 0, strText, 0);
}
dirdirdir3 2009-09-12
  • 打赏
  • 举报
回复
不需要GetCursorPos(),wm_mousemove本身就有坐标....
int x=LOWORD(lParam);
int y=HIWORD(lParam);

什么错了?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧