〓〓◆怎么把鼠标限制在一个范围里?◆〓〓马上给分!!!

anjy 2002-05-15 05:47:18
怎么把鼠标限制在一个范围里?比如,在一个对话框的应用程序里,吧鼠标限制在该窗口内。请具体点!
...全文
248 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
g_windy 2002-05-17
//Start
RECT rc;
GetWindowRect( &rc );
rc.top = rc.top+25;
SetForegroundWindow();
SetCapture();
ClipCursor( &rc );

//End
ReleaseCapture();
ClipCursor(NULL);
回复
anjy 2002-05-16
当然是系统范围内了
回复
webber84 2002-05-16
关键是看你要在多大程度上限制鼠标。
回复
anjy 2002-05-16
啊,又是钩子!!不用钩子不行吗?我对钩子实在没把握,好像系统钩子要用DLL,又是一大难点!
回复
webber84 2002-05-16
如果你想在系统范围内限制鼠标移动的话,好像只能用HOOK。因为用ClipCusor总能通过alt+tab来切换窗口,只要一切换到别的窗口,ClipCusor就不起作用了。
回复
golq 2002-05-16
补充问一下,如果窗口是园的呢?

我以前作了一个院窗口,没法用clipcursor,只有用mousemove判断,有没有好办法呀
回复
anjy 2002-05-16
TO:dou_ya(豆芽菜)
按你的方法,虽然一开始可以限定鼠标范围,但是,但用鼠标拖动一下改对话框,或新开一个其他任何窗口(如:资源管理器)后,限制范围就不起作用了。
回复
eastrock 2002-05-15
BOOL ClipCursor(
CONST RECT *lpRect);
回复
tifaling 2002-05-15
RECT rect;
rect.top=0;rect.botton=640;
rect.left=0;rect.right=480;
ClipCursor(&rect);
把鼠标限定在(0,0)~(640,480)
回复
dou_ya 2002-05-15
ClipCursor 是 Win32 API,MFC 没有进行封装。

ClipCursor
The ClipCursor function confines the cursor to a rectangular area on the screen. If a subsequent cursor position (set by the SetCursorPos function or the mouse) lies outside the rectangle, the system automatically adjusts the position to keep the cursor inside the rectangular area.

BOOL ClipCursor(
CONST RECT *lpRect // screen coordinates
);
Parameters
lpRect
[in] Pointer to the RECT structure that contains the screen coordinates of the upper-left and lower-right corners of the confining rectangle. If this parameter is NULL, the cursor is free to move anywhere on the screen.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
The cursor is a shared resource. If an application confines the cursor, it must release the cursor by using ClipCursor before relinquishing control to another application.

The calling process must have WINSTA_WRITEATTRIBUTES access to the window station.
回复
dou_ya 2002-05-15
BOOL C***Dlg::StartClipCusor()
{
RECT rc;
GetWindowRect( &rc );

return ClipCursor( &rc );
}

BOOL C***Dlg::EndClipCursor()
{
return ClipCursor(NULL);
}
回复
linning6 2002-05-15
找本参考书看看嘛。
回复
vc8 2002-05-15
RECT rc;
RECT oldRc;
GetClipCursor( &oldRc );
GetWindowRect( &rc );
ClipCursor( &rc );
....
//退出
ClipCursor( &oldRc );
回复
webber84 2002-05-15
masterz() 说的是SDK的方法。如果你用MFC的话,直接用CWnd*调用函数就是了。
回复
anjy 2002-05-15
hwnd是什么,如何得到?
回复
masterz 2002-05-15
Confining a Cursor
The following example confines the cursor to the application's window and then restores the cursor to its previous window. The example uses the GetClipCursor function to record the area in which the cursor can move and the ClipCursor function to confine and restore the cursor.

RECT rcClip; // new area for ClipCursor
RECT rcOldClip; // previous area for ClipCursor

// Record the area in which the cursor can move.

GetClipCursor(&rcOldClip);

// Get the dimensions of the application's window.

GetWindowRect(hwnd, &rcClip);

// Confine the cursor to the application's window.

ClipCursor(&rcClip);

//
// Process input from the confined cursor.
//

// Restore the cursor to its previous area.

ClipCursor(&rcOldClip);
Because there is only one cursor at a time available in the system, an application that confines the cursor must restore the cursor before relinquishing control to another window.
回复
anjy 2002-05-15
范围怎么设?
回复
webber84 2002-05-15
ClipCursor()
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-05-15 05:47
社区公告

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