小辉论坛精华贴
这段程序实现的功能是用键盘的方向键来控制鼠标,可有点小问题我不明白:
本应该在按住方向键不松时,鼠标箭头会跑的越来越快,可现在箭头只能匀
速运动!请指教!
int repeat=0;
POINT ptCursor;
RECT rect;
switch (message)
{
case WM_KEYDOWN:
if(wParam!=VK_LEFT&&wParam!=VK_RIGHT&&wParam!=VK_UP&&wParam!=VK_DOWN)
break;
GetCursorPos(&ptCursor);
ScreenToClient(hwnd,&ptCursor);
repeat++;
switch(wParam)
{
case VK_LEFT:
ptCursor.x-=repeat;
break;
case VK_RIGHT:
ptCursor.x+=repeat;
break;
case VK_UP:
ptCursor.y-=repeat;
break;
case VK_DOWN:
ptCursor.y+=repeat;
break;
default:
return(NULL);
}
GetClientRect(hwnd,&rect);
if(ptCursor.x>=rect.right)
ptCursor.x=rect.right-1;
else if(ptCursor.x<rect.left)
ptCursor.x=rect.left;
if(ptCursor.y>=rect.bottom)
ptCursor.y=rect.bottom-1;
else if(ptCursor.y<rect.top)
ptCursor.y=rect.top;
ClientToScreen(hwnd,&ptCursor);
SetCursorPos(ptCursor.x,ptCursor.y);
break;
cipher_answer:
因为repeat是一个自动变量,每次调用WndProc时都会重新变为0,当然是匀速的。
如果改成static的,那会越来越快,不管你放不放手。
Q:斑竹真乃高人也!
Q:再麻烦斑竹一次:我在原程序上加上了
case WM_KEYUP:
{
repeat=1;
break;
}
后,按键盘为什么没有反映??
static int repeat=0;
POINT ptCursor;
RECT rect;
switch (message)
{
case WM_KEYDOWN:
if(wParam!=VK_LEFT&&wParam!=VK_RIGHT&&wParam!=VK_UP&&wParam!=VK_DOWN)
break;
GetCursorPos(&ptCursor);
ScreenToClient(hwnd,&ptCursor);
repeat++;
switch(wParam)
{
case VK_LEFT:
ptCursor.x-=repeat;
break;
case VK_RIGHT:
ptCursor.x+=repeat;
break;
case VK_UP:
ptCursor.y-=repeat;
break;
case VK_DOWN:
ptCursor.y+=repeat;
break;
default:
return(NULL);
}
case WM_KEYUP:
{
repeat=1;
break;
}
GetClientRect(hwnd,&rect);
if(ptCursor.x>=rect.right)
ptCursor.x=rect.right-1;
else if(ptCursor.x<rect.left)
ptCursor.x=rect.left;
if(ptCursor.y>=rect.bottom)
ptCursor.y=rect.bottom-1;
else if(ptCursor.y<rect.top)
ptCursor.y=rect.top;
ClientToScreen(hwnd,&ptCursor);
SetCursorPos(ptCursor.x,ptCursor.y);
break;
cipher_answer:
抱歉,现在才回。可能你自己也找到错误了。
你把WM_KEYUP的处理放错地方了。
正确的位置如下:
ClientToScreen(hWnd,&ptCursor);
SetCursorPos(ptCursor.x,ptCursor.y);
break;
case WM_KEYUP:
repeat=1;
break;
case WM_PAINT:
Q:呵呵,大侠果然料事如神啊!我发完帖子才看到放错位置了,浪费大家的时间了。