??Windows编程中怎样把鼠标隐藏在窗口内部区域??

gl615 2008-07-01 01:29:34
我知道 ShowCursor 是隐藏鼠标的函数,但我用了之后,鼠标在窗口中全部隐藏了,窗口的外部区域也是。

我在之前还加了GetClientRect 取得窗口内部矩形。。也不行。

我只是想把鼠标仅仅 在窗口内部处于隐藏状态。。

怎么做啊,请指教啊、、!
下面是短小的源码:

#include <windows.h>


//函数声明
BOOL InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );

//变量说明
HWND hWnd; //窗口句柄
//************************************************************
//函数:WinMain( )
//功能:Windows程序入口函数。创建主窗口,处理消息循环
//************************************************************
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if ( !InitWindow( hInstance, nCmdShow ) ) return FALSE; //创建主窗口
//如果创建不成功则返回FALSE并同时退出程序
MSG msg;
//进入消息循环:
for(;;)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if ( msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}

//************************************************************
//函数:InitWindow( )
//功能:创建窗口
//************************************************************

static BOOL InitWindow( HINSTANCE hInstance, int nCmdShow )
{
//定义窗口风格:
WNDCLASS wc;
wc.style = NULL;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = CreateSolidBrush(0x0); //暗红色的背景
wc.lpszMenuName = NULL;
wc.lpszClassName = "My_Test";
RegisterClass(&wc);//注册窗口
//按所给参数创造窗口
hWnd = CreateWindow("My_Test",
"I'll be the one!",
WS_OVERLAPPEDWINDOW,//WS_POPUP|WS_MAXIMIZE,
CW_USEDEFAULT,//0,
CW_USEDEFAULT,//0,
CW_USEDEFAULT,//GetSystemMetrics( SM_CXSCREEN ), //此函数返回屏幕宽度
CW_USEDEFAULT,//GetSystemMetrics( SM_CYSCREEN ), //此函数返回屏幕高度
NULL,NULL,hInstance,NULL);

if( !hWnd ) return FALSE;
ShowWindow(hWnd,nCmdShow);//显示窗口
UpdateWindow(hWnd);//刷新窗口
MoveWindow(hWnd,150,100,400,500,true);
RECT rect;
GetClientRect(hWnd,&rect); //....................
//GetWindowRect(hWnd,&rect);
ShowCursor(FALSE); //....................
return TRUE;
}

//************************************************************
//函数:WinProc( )
//功能:处理窗口消息
//************************************************************

LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}




...全文
202 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
gl615 2008-07-02
  • 打赏
  • 举报
回复
我知道答案了:
case WM_SETCURSOR:
SetCursor(NULL);
break;
、、、
gl615 2008-07-01
  • 打赏
  • 举报
回复
我知道,这样的话 把鼠标移出窗体,再进来就不行了。。
yc_8301 2008-07-01
  • 打赏
  • 举报
回复

case WM_MOUSEMOVE:
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor
if(yPos >= (rect.top) && yPos <= rect.bottom)
ShowCursor(FALSE);
else
ShowCursor(TRUE);
break;



判断时,添加
ShowCursor(TRUE);

gl615 2008-07-01
  • 打赏
  • 举报
回复
把鼠标移出窗体,再进来就不行了。。
yc_8301 2008-07-01
  • 打赏
  • 举报
回复

LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
int xPos;
int yPos;
switch( message )
{
case WM_QUIT:
ExitProcess(0);
break;
case WM_CLOSE:
SendMessage(hWnd, WM_QUIT, NULL, NULL);
break;
case WM_MOUSEMOVE:
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor
if(yPos >= (rect.top) && yPos <= rect.bottom)
ShowCursor(FALSE);
break;
default:
break;
}
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}


试一试这个是否可以?(只是个思路)
另外
case WM_MOUSEMOVE:
消息后可能需要自己做判断,判断鼠标是否在窗体内。在窗体内则隐藏!

如果不可以,带高手出现!

liangwei_2008 2008-07-01
  • 打赏
  • 举报
回复
先得到你窗口在屏幕中的区域

在mousemove里做判断,

根据鼠标当前位置,判断是否在窗口的区域内,

是的化ShowCursor(TRUE)
不是的话 ShowCursor(FALSE)

gl615 2008-07-01
  • 打赏
  • 举报
回复
只是比较抽象的隐藏。。。
ShowCursor
The ShowCursor function displays or hides the cursor.

int ShowCursor(
BOOL bShow // cursor visibility
);
Parameters
bShow
[in] Specifies whether the internal display counter is to be incremented or decremented. If bShow is TRUE, the display count is incremented by one. If bShow is FALSE, the display count is decremented by one.
Return Values
The return value specifies the new display counter.

Remarks
This function sets an internal display counter that determines whether the cursor should be displayed. The cursor is displayed only if the display count is greater than or equal to 0. If a mouse is installed, the initial display count is 0. If no mouse is installed, the display count is –1.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.

See Also
Cursors Overview, Cursor Functions, ClipCursor, GetCursorPos, SetCursor, SetCursorPos

Built on Thursday, May 11, 2000
biosli 2008-07-01
  • 打赏
  • 举报
回复
查查MSDN:

int ShowCursor( BOOL bShow);
gl615 2008-07-01
  • 打赏
  • 举报
回复
能不能说具体怎么实现啊
gl615 2008-07-01
  • 打赏
  • 举报
回复
关键就是 不会 添加有效的 鼠标隐藏API 啊。。
biosli 2008-07-01
  • 打赏
  • 举报
回复
建个新工程把他的默认WinProc函数拷贝过去~~
然后在他的基础上加代码。
gl615 2008-07-01
  • 打赏
  • 举报
回复
没有实现啊,我要的是窗口内部隐藏,我弄的在整个窗口都隐藏了啊。。。
biosli 2008-07-01
  • 打赏
  • 举报
回复
在WM_MOUSEMOVE里面判断一下,然后添加鼠标隐藏API。
yc_8301 2008-07-01
  • 打赏
  • 举报
回复
你这个不是已经实现了吗?
gl615 2008-07-01
  • 打赏
  • 举报
回复
自己顶顶

64,688

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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