想用c++寫一段全域隱藏滑鼠的代碼,就是說希望此時系統的滑鼠變為不可見被隱藏了,而并不是說滑鼠不能動,此時如果移動滑鼠還是會有訊息產生。試過花了一個全屏的框,然后用showcursor(false)但是發現此時滑鼠動不了從而也就沒辦法采集此時的滑鼠訊息了。求問各路大神!!!!
uj5u.com熱心網友回復:
可以啊BOOL CCheckCursorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
ShowCursor(FALSE);
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CCheckCursorDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
afxDump << point << "\n";
CDialog::OnMouseMove(nFlags, point);
}
結果:
344, 72)
(344, 67)
(343, 66)
(343, 64)
(342, 63)
(343, 63)
(345, 63)
(346, 63)
(349, 62)
(351, 60)
(356, 54)
(363, 48)
(373, 41)
(383, 29)
uj5u.com熱心網友回復:
showcursor 這個函式只能是應用程式本省吧,不是系統全域的。只是隱藏,不會限制滑鼠移動。
uj5u.com熱心網友回復:
extern void cursorHide(int execption_type,int point_x,int point_y){
HWND hwndF;//全屏后的視窗句柄
LPCTSTR m_lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS)),(HBRUSH)::GetStockObject(0),NULL);
int x_coord=::GetSystemMetrics(SM_CXSCREEN);//獲取螢屏的寬度
int y_coord=::GetSystemMetrics(SM_CYSCREEN);//獲取螢屏的高度
CRect rect(0,0,100,100);
hwndF=::CreateWindowEx(WS_EX_TOPMOST,m_lpszClassName,_T(""),WS_VISIBLE|WS_POPUP,0,0,rect.Width(),rect.Height(),NULL,0,NULL,NULL);
::SetWindowLong(hwndF, GWL_EXSTYLE, GetWindowLong(hwndF, GWL_EXSTYLE) | WS_EX_LAYERED);
::SetLayeredWindowAttributes(hwndF,0,1,2);
SYSTEMTIME sys;
GetLocalTime( &sys );
long time=sys.wHour*3600*1000+sys.wMinute*60*1000+sys.wSecond*1000+sys.wMilliseconds;
long tmpTime1=time;
ShowCursor(false);//隱藏滑鼠
while((!isNeedStop)&&(tmpTime1<time+2000)){
GetLocalTime( &sys );
tmpTime1=sys.wHour*3600*1000+sys.wMinute*60*1000+sys.wSecond*1000+sys.wMilliseconds;
}
ShowCursor(false);//隱藏滑鼠
}
uj5u.com熱心網友回復:
extern void cursorHide(int execption_type,int point_x,int point_y){
HWND hwndF;//全屏后的視窗句柄
LPCTSTR m_lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS)),(HBRUSH)::GetStockObject(0),NULL);
int x_coord=::GetSystemMetrics(SM_CXSCREEN);//獲取螢屏的寬度
int y_coord=::GetSystemMetrics(SM_CYSCREEN);//獲取螢屏的高度
CRect rect(0,0,x_coord,y_coord);
hwndF=::CreateWindowEx(WS_EX_TOPMOST,m_lpszClassName,_T(""),WS_VISIBLE|WS_POPUP,0,0,rect.Width(),rect.Height(),NULL,0,NULL,NULL);//創建置頂子視窗
::SetWindowLong(hwndF, GWL_EXSTYLE, GetWindowLong(hwndF, GWL_EXSTYLE) | WS_EX_LAYERED);//設定子視窗的擴展屬性
::SetLayeredWindowAttributes(hwndF,0,1,2);//設定子視窗透明,第三個引數即為透明度
SYSTEMTIME sys;
GetLocalTime( &sys );
long time=sys.wHour*3600*1000+sys.wMinute*60*1000+sys.wSecond*1000+sys.wMilliseconds;
long tmpTime1=time;
ShowCursor(false);//隱藏滑鼠
while((!isNeedStop)&&(tmpTime1<time+2000)){
GetLocalTime( &sys );
tmpTime1=sys.wHour*3600*1000+sys.wMinute*60*1000+sys.wSecond*1000+sys.wMilliseconds;
}
ShowCursor(false);//隱藏滑鼠
}
uj5u.com熱心網友回復:
麻煩能不能看下,上面是源代碼,采集滑鼠訊息的代碼沒有貼出來,現在這一段就是畫了一個跟螢屏一樣大的置頂視窗然后把它設成透明,這個時候發現滑鼠就根本移不動,不管隱不隱藏都移不動,接著再延時兩秒鐘,再現滑鼠。關鍵問題在于我希望先全域隱藏滑鼠,但是滑鼠是可移動的,在延時的這兩秒鐘內外部的執行緒可以采集滑鼠移動的訊息,但這時候卻移不動滑鼠所以訊息采不了,但是我不畫這個子視窗直接showcursor(false)又根本隱藏不了滑鼠,好捉急!!!
uj5u.com熱心網友回復:
WS_EX_LAYERED 使滑鼠訊息 進入 這個視窗下的 視窗 你當然 接受不到,試試 半透明 視窗。uj5u.com熱心網友回復:
贊贊贊,麻煩能不能具體說說我這個地方應該怎么改嘛uj5u.com熱心網友回復:
::SetLayeredWindowAttributes(hwndF,0,1,2);//設定子視窗透明,第三個引數即為透明度改 50% 試試
uj5u.com熱心網友回復:
還是不行啊 這時候滑鼠就突然閃一下 就不消失了uj5u.com熱心網友回復:
那個視窗 透明了 沒有 ?uj5u.com熱心網友回復:
那個透明視窗的 Cursor 是 ‘+’,干什么用的。請說明 到底 你想 干什么。
uj5u.com熱心網友回復:
::SetLayeredWindowAttributes(hwndF,0,1,2);1 不是 透明的; 0 才是 透明的
uj5u.com熱心網友回復:
我就是改的那個引數,透明度是0到255,所以1實際上已經接近于全透明,你希望我改成50%就是127左右,發現視窗是半透明的因為此時我可以隱約看到桌面,但實際上這時滑鼠還是動不了;那實際上我是希望構造一個滑鼠例外的場景,就是讓滑鼠突然消失一段時間,這時用戶若搖動滑鼠我就可以采集用戶面臨這種例外場景下的行為或者說滑鼠訊息,現在問題就是你看到了我用以上代碼來隱藏滑鼠的確可以隱藏但是滑鼠只要處于畫的那個置頂視窗里面就動不了,從而也就采集不到訊息,那如果不花這樣一個視窗直接showcursor(false)滑鼠就根本隱藏不了。(另外就是上面代碼最后一句寫錯應該是 shouwcuror(true)再現滑鼠)uj5u.com熱心網友回復:
你說透明視窗的‘cursor’是‘+’的是什么意思 我沒有理解uj5u.com熱心網友回復:
在vc6上 我改為:
#define WS_EX_LAYERED 0x00080000
void CCheckCursorDlg::cursorHide()
{
if(m_hwndF)
{
::DestroyWindow(m_hwndF);
m_hwndF=0;
}
LPCTSTR m_lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS)),(HBRUSH)::GetStockObject(BLACK_BRUSH),NULL);
int x_coord=::GetSystemMetrics(SM_CXSCREEN);//獲取螢屏的寬度
int y_coord=::GetSystemMetrics(SM_CYSCREEN);//獲取螢屏的高度
HWND desk=::GetDesktopWindow();
m_hwndF=::CreateWindowEx(WS_EX_TOPMOST|WS_EX_LAYERED,m_lpszClassName,_T(""),WS_VISIBLE | WS_POPUP,400,300,300,300,desk,0,NULL,NULL);
// ::SetWindowLong(hwndF, GWL_EXSTYLE, GetWindowLong(hwndF, GWL_EXSTYLE) | WS_EX_LAYERED);
//
HINSTANCE hInstTmp = LoadLibrary("User32.DLL");
if(hInstTmp)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函式指標
fun=(MYFUNC)GetProcAddress(hInstTmp, "SetLayeredWindowAttributes");
#define LWA_ALPHA 0x00000002
if(fun)
fun(m_hwndF,0,0,LWA_ALPHA); //表單句柄,顏色值,透明度30,透明方式 一般用2
FreeLibrary(hInstTmp);
}
ShowCursor(FALSE);
SetTimer(2,10000,0);
}
1.
::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS))
就是 ‘+’ ,透明度 為 1 時 可以 看到 這個 ‘+’ 滑鼠, 所以透明一定要 =0.
2. 那個 while((!isNeedStop)&&(tmpTime1<time+2000))
使 訊息堵塞,這個 透明 視窗 沒正常顯示, 請改為 定時器。
uj5u.com熱心網友回復:
我的透明視窗在 對話框 內 400,300,300,300,“但是滑鼠只要處于畫的那個置頂視窗里面就動不了”
=0 全透明時 對話框
void CCheckCursorDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
afxDump << point << "\n";
CDialog::OnMouseMove(nFlags, point);
}
還有輸出 ,你的 視窗 不在 對話框內 CRect rect(0,0,100,100);
滑鼠訊息 跑到 桌面去了。
uj5u.com熱心網友回復:
我用你寫的那個他也run不了啊,先是說沒定義m_hwndF,于是我定義了一個;完了說SetTimer(2,10000,0)引數不對應該是有四個引數我改成了SetTimer(m_hWnd,1,1000,NULL),最后跑的時候就卡在if(m_hwndF)這里動也動不了就強退vs。關于你說你的 視窗 不在 對話框內 CRect rect(0,0,100,100);,其實這個可以大小可以改的我寫成(0,0,100,100)就是為了方便對比看,滑鼠處于這個(0,0,100,100)的框里面就動不了也沒訊息,但是當滑鼠處于這個框外的時候可以自由移動也有訊息產生,所以我覺得不是這個while的原因
uj5u.com熱心網友回復:
大哥 我能不能加你個qq具體說一下 感謝感謝!!!uj5u.com熱心網友回復:
sorry, I donot use qq !uj5u.com熱心網友回復:
OnTimer(UINT nIDEvent){
if(nIDEvent==2)
{
KillTimer(2);
ShowCursor(TRUE);
}
}
uj5u.com熱心網友回復:
不過我始終不明白 那個 透明視窗的 作用。uj5u.com熱心網友回復:
因為不畫那個視窗,我用showcursor(false)就沒有反應,滑鼠就一直存在當然也可以自由動,必須要滑鼠在那個透明視窗內此時使用showcursor(false)滑鼠才會被隱藏。那你有沒有其他聯系方式,skype?還是微信uj5u.com熱心網友回復:
No other way to connect me ! Sorry!uj5u.com熱心網友回復:
BOOL CCheckCursorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){
// TODO: Add your message handler code here and/or call default
ShowCursor(FALSE);
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
不就是 隱藏 嗎?
uj5u.com熱心網友回復:
是必須要寫一個setcursor的函式嗎 直接用showcursor(false)根本沒有反應啊uj5u.com熱心網友回復:
setcursor函式不要你寫, 但 必須 成對, 即ShowCursor(FALSE);ShowCursor(TRUE);要配合好ShowCursor
The ShowCursor function displays or hides the cursor.
int ShowCursor(
BOOL bShow // cursor visibility flag
);
Parameters
bShow
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.
uj5u.com熱心網友回復:
問題就是 showcursor他只能區域隱藏 也就是只能在應用程式內隱藏 在全域里面是隱藏不了的uj5u.com熱心網友回復:
"在全域里面" ?Cursor 只有一個(滑鼠) ,而每個視窗都需要 管理 Cursor,獲取你說的“全域”視窗 , 再 ShowCursor()
uj5u.com熱心網友回復:
#define WS_EX_LAYERED 0x00080000
void CCheckCursorDlg::cursorHide()
{
LPCTSTR m_lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS)),
(HBRUSH)::GetStockObject(BLACK_BRUSH),NULL);
int x_coord=::GetSystemMetrics(SM_CXSCREEN);//獲取螢屏的寬度
int y_coord=::GetSystemMetrics(SM_CYSCREEN);//獲取螢屏的高度
HWND desk=::GetDesktopWindow();//
m_hwndF=::CreateWindowEx(WS_EX_TOPMOST|WS_EX_LAYERED|WS_EX_TRANSPARENT,m_lpszClassName,"",
WS_VISIBLE | WS_POPUP,
0,0,x_coord,y_coord,0,0,NULL,NULL);
//
HINSTANCE hInstTmp = LoadLibrary("User32.DLL");
if(hInstTmp)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函式指標
fun=(MYFUNC)GetProcAddress(hInstTmp, "SetLayeredWindowAttributes");
#define LWA_ALPHA 0x00000002
if(fun)
fun(m_hwndF,RGB(0,0,0),30,LWA_ALPHA); //透明度30,透明方式2
FreeLibrary(hInstTmp);
}
ShowCursor(FALSE);
SetTimer(2,10000,0);
}
void CCheckCursorDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==2)
{
KillTimer(2);
if(m_hwndF)
{// 關閉那個半透明視窗
::DestroyWindow(m_hwndF);
m_hwndF=0;
}
ShowCursor(TRUE);
}
CDialog::OnTimer(nIDEvent);
}
加了 滑鼠 穿透 風格 WS_EX_TRANSPARENT
不知道是不是你要的
uj5u.com熱心網友回復:
將滑鼠對應圖片設定為全透明!
uj5u.com熱心網友回復:
滑鼠玩起來也還不少竅門吶uj5u.com熱心網友回復:
試下這個方法:
#define OCR_NORMAL 32512
#define OCR_IBEAM 32513
#define OCR_WAIT 32514
#define OCR_CROSS 32515
#define OCR_UP 32516
#define OCR_SIZE 32640 /* OBSOLETE: use OCR_SIZEALL */
#define OCR_ICON 32641 /* OBSOLETE: use OCR_NORMAL */
#define OCR_SIZENWSE 32642
#define OCR_SIZENESW 32643
#define OCR_SIZEWE 32644
#define OCR_SIZENS 32645
#define OCR_SIZEALL 32646
#define OCR_ICOCUR 32647 /* OBSOLETE: use OIC_WINLOGO */
#define OCR_NO 32648
#define OCR_APPSTARTING 32650
#define OCR_HAND 32649
#define OCR_HELP 32651 /* only defined in wine */
HCURSOR hCursor = LoadCursorFromFile(_T("C:\\Users\\beanjoy\\Desktop\\HookMouseTest\\res\\cursor1.cur"));
SetSystemCursor(CopyCursor(hCursor), OCR_APPSTARTING);
SetSystemCursor(CopyCursor(hCursor), OCR_NORMAL);
SetSystemCursor(CopyCursor(hCursor), OCR_CROSS);
SetSystemCursor(CopyCursor(hCursor), OCR_HAND);
SetSystemCursor(CopyCursor(hCursor), OCR_HELP);
SetSystemCursor(CopyCursor(hCursor), OCR_IBEAM);
SetSystemCursor(CopyCursor(hCursor), OCR_NO);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZEALL);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZENESW);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZENS);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZENWSE);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZEWE);
SetSystemCursor(CopyCursor(hCursor), OCR_UP);
SetSystemCursor(CopyCursor(hCursor), OCR_WAIT);
SetSystemCursor(CopyCursor(hCursor), OCR_ICOCUR);
SetSystemCursor(CopyCursor(hCursor), OCR_SIZE);
SetSystemCursor(CopyCursor(hCursor), OCR_ICON);
使用SetSystemCursor設定系統的游標,注意cursor1.cur是一個全透明的游標檔案,可以用vs創建一個游標,然后用其他工具修改為全透明的游標。
然后SystemParametersInfo(SPI_SETCURSORS, 0, NULL, 0);恢復游標。
不過,有些不常見的游標形態,SetSystemCursor無法設定。比如與正常箭頭游標方向相反箭頭游標。
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
換成一個看不見的滑鼠圖形就可以了。uj5u.com熱心網友回復:
showcursor 只能作用在 當前應用程式本身,哪能全域隱藏uj5u.com熱心網友回復:
雖然不知道,但還是頂貼uj5u.com熱心網友回復:
如果樓主的意思是:1 創建一個 全屏 半透明視窗,關閉這個視窗的 滑鼠, 同時還要 檢測 滑鼠位置。
2 那么應該 子類化那個 “” 全屏 半透明視窗“”, 在WNDPROC里 隱藏 滑鼠, 在 MOUSEMOVE 里 檢測滑鼠。
uj5u.com熱心網友回復:
//
WNDPROC g_OldProc;
BOOL g_Once=FALSE;
LRESULT CALLBACK NewProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
CPoint pt;
switch(message)
{
case WM_SETCURSOR:
if(!g_Once)
{
ShowCursor(FALSE);
g_Once=TRUE;
}
break;
case WM_DESTROY:
ShowCursor(TRUE);
g_Once=FALSE;
break;
case WM_MOUSEMOVE:
pt.x = (int) LOWORD(lParam);
pt.y = (int) HIWORD(lParam);
afxDump << pt << "\n";
break;
}
return CallWindowProc(g_OldProc, hwnd, message, wParam, lParam);
}
//
#define WS_EX_LAYERED 0x00080000
void CCheckCursorDlg::cursorHide()
{
LPCTSTR m_lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,::LoadCursorA(NULL,MAKEINTRESOURCE(IDC_CROSS)),
(HBRUSH)::GetStockObject(BLACK_BRUSH),NULL);
int x_coord=::GetSystemMetrics(SM_CXSCREEN);//獲取螢屏的寬度
int y_coord=::GetSystemMetrics(SM_CYSCREEN);//獲取螢屏的高度
HWND desk=::GetDesktopWindow();// mouse penetrate : |WS_EX_TRANSPARENT
m_hwndF=::CreateWindowEx(WS_EX_TOPMOST|WS_EX_LAYERED,m_lpszClassName,"",
WS_VISIBLE | WS_POPUP,
0,0,x_coord,y_coord,0,0,NULL,NULL);
g_OldProc = (WNDPROC)SetWindowLong(m_hwndF, GWL_WNDPROC, (LONG)NewProc);
//
HINSTANCE hInstTmp = LoadLibrary("User32.DLL");
if(hInstTmp)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函式指標
fun=(MYFUNC)GetProcAddress(hInstTmp, "SetLayeredWindowAttributes");
#define LWA_ALPHA 0x00000002
if(fun)
fun(m_hwndF,RGB(0,0,0),30,LWA_ALPHA); //透明度30,透明方式2
FreeLibrary(hInstTmp);
}
SetTimer(2,10000,0);
}
timer中
if(nIDEvent==2)
{
KillTimer(2);
if(m_hwndF)
{
::DestroyWindow(m_hwndF);
m_hwndF=0;
}
}
呼叫;
BOOL CxxxxDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if((pMsg->message==WM_LBUTTONDOWN) && pMsg->hwnd==GetDlgItem(IDC_BUTTON3)->m_hWnd)
{
cursorHide();
}
return CDialog::PreTranslateMessage(pMsg);
}
uj5u.com熱心網友回復:
http://bbs.csdn.net/topics/70089930uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
不錯
uj5u.com熱心網友回復:



uj5u.com熱心網友回復:
受教了,幫忙解決了一個大問題!
uj5u.com熱心網友回復:
做一個定時器,加一個回圈, ShowCursor (-1) < 0 時,滑鼠全域隱藏, > 0時全域顯示,然后系統有未知代碼不斷顯示滑鼠,所以你需要不斷隱藏滑鼠,才能全域隱藏uj5u.com熱心網友回復:
不能全域隱藏的原因不是showcursor本身,它本身是系統API不是么:) 而改變游標圖形也是一樣的結果。
根本原因就是schlafenhamster說的,每個表單都在管理游標,特別的,游標是可以注冊到表單類中、
作為類樣式存在的(RegisterWindowClass或者SetClassLong)。
從LZ的需求來看,注冊或設定空游標圖形到表單類中比回應那個OnSetCursor訊息要方便些吧。
uj5u.com熱心網友回復:
同求方法是什么uj5u.com熱心網友回復:
首先,你這個根本不叫全域隱藏滑鼠。其次,最大化后ShowCursor(FALSE),不影響WM_MOUSEMOVE訊息,我剛測過。
uj5u.com熱心網友回復:
HCURSOR hc = LoadCursorFromFile("xxx.cur");SetSystemCursor(hc, OCR_NORMAL);
xxx.cur是一個沒有像素的游標檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/116811.html
標籤:基礎類
上一篇:請問win32 設定帶圖片的button一直不顯示,怎么回事?
下一篇:HTTP上傳圖片失敗?
