Win32学习(七) 鼠标消息
基本鼠標(biāo)消息
WM_LBUTTONDOWN - 鼠標(biāo)左鍵按下
WM_LBUTTONUP - 鼠標(biāo)左鍵抬起
WM_RBUTTONDOWN - 鼠標(biāo)右鍵按下
WM_RBUTTONUP - 鼠標(biāo)右鍵抬起
WM_MOUSEMOVE - 鼠標(biāo)移動(dòng)消息
雙擊消息
WM_LBUTTONDBLCLK - 鼠標(biāo)左鍵雙擊
WM_RBUTTONDBLCLK - 鼠標(biāo)右鍵雙擊
使用時(shí)需要在注冊(cè)窗口類的時(shí)候添加CS_DBLCLKS風(fēng)格。
滾輪消息
WM_MOUSEWHEL - 鼠標(biāo)滾輪消息
附帶信息:
wPARAM:
LOWORD - 其他按鍵的狀態(tài)
HIWORD - 滾輪的偏移量,通過(guò)正負(fù)值表示滾輪方向
正:向前滾動(dòng) 負(fù):向后滾動(dòng)
IPARAM:鼠標(biāo)當(dāng)前的位置,屏幕坐標(biāo)系
LOWORD - X坐標(biāo)
HIWORD - Y坐標(biāo)
使用:
通過(guò)偏移量,獲取滾動(dòng)的方向和距離 (只會(huì)出現(xiàn)120的倍數(shù)的數(shù))
鼠標(biāo)基本消息
附帶信息:
wPARAM : 其他按鍵的狀態(tài),例如Ctrl/Shift等
IPARAM: 鼠標(biāo)的位置,窗口客戶區(qū)坐標(biāo)系
LOWORD X坐標(biāo)位置
HIWORD Y坐標(biāo)位置
一般清空鼠標(biāo)按下/抬起成對(duì)出現(xiàn)。在鼠標(biāo)移動(dòng)過(guò)程中,會(huì)根據(jù)移動(dòng)速度產(chǎn)生一系列的WM_MOUSEMOVE消息.
鼠標(biāo)點(diǎn)擊事件
#include <windows.h> #include <stdio.h> HANDLE g_hOutput = 0; void OnLButtonDown(HWND hWnd,WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText,strlen(szText), NULL, NULL); }void OnLButtonUP(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam) {switch (msgID) {case WM_LBUTTONDOWN:OnLButtonDown(hWnd, wParam, IParam);break;case WM_LBUTTONUP:OnLButtonUP(hWnd, wParam, IParam);break;case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, msgID, wParam, IParam); }int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {AllocConsole();g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);WNDCLASS wc = { 0 };wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wc.hCursor = NULL;wc.hIcon = NULL;wc.hInstance = hIns;wc.lpfnWndProc = WndProc;wc.lpszClassName = "Main";wc.lpszMenuName = NULL;wc.style = CS_HREDRAW | CS_VREDRAW;RegisterClass(&wc); //將以上所有賦值全部寫入操作系統(tǒng)中//在內(nèi)存創(chuàng)建窗口HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);//顯示窗口ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);//消息循環(huán)MSG nMsg = { 0 };while (GetMessage(&nMsg, NULL, 0, 0)) { //抓消息TranslateMessage(&nMsg);//翻譯消息DispatchMessage(&nMsg);//派發(fā)消息:將消息交給窗口處理函數(shù)來(lái)處理。}return 0; } //需要在任務(wù)管理器中退出線程!鼠標(biāo)滑動(dòng)
#include <windows.h> #include <stdio.h> HANDLE g_hOutput = 0; void OnLButtonDown(HWND hWnd,WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText,strlen(szText), NULL, NULL); }void OnLButtonUP(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_MOUSEMOVE:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam) {switch (msgID) {case WM_MOUSEMOVE:OnMouseMove(hWnd, wParam, IParam);break;case WM_LBUTTONDOWN:OnLButtonDown(hWnd, wParam, IParam);break;case WM_LBUTTONUP:OnLButtonUP(hWnd, wParam, IParam);break;case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, msgID, wParam, IParam); }int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {AllocConsole();g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);WNDCLASS wc = { 0 };wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wc.hCursor = NULL;wc.hIcon = NULL;wc.hInstance = hIns;wc.lpfnWndProc = WndProc;wc.lpszClassName = "Main";wc.lpszMenuName = NULL;wc.style = CS_HREDRAW | CS_VREDRAW;RegisterClass(&wc); //將以上所有賦值全部寫入操作系統(tǒng)中//在內(nèi)存創(chuàng)建窗口HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);//顯示窗口ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);//消息循環(huán)MSG nMsg = { 0 };while (GetMessage(&nMsg, NULL, 0, 0)) { //抓消息TranslateMessage(&nMsg);//翻譯消息DispatchMessage(&nMsg);//派發(fā)消息:將消息交給窗口處理函數(shù)來(lái)處理。}return 0; } //需要在任務(wù)管理器中退出線程!雙擊事件
#include <windows.h> #include <stdio.h> HANDLE g_hOutput = 0; void OnLButtonDown(HWND hWnd,WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText,strlen(szText), NULL, NULL); }void OnLButtonUP(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONUP:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_MOUSEMOVE:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnLButtonDblClk(HWND hWnd) {const char* szText = "WM_LBUTTONDBLCLK\n";WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam) {switch (msgID) {case WM_LBUTTONDBLCLK:OnLButtonDblClk(hWnd);break;//case WM_MOUSEMOVE:// OnMouseMove(hWnd, wParam, IParam);break;case WM_LBUTTONDOWN:OnLButtonDown(hWnd, wParam, IParam);break;case WM_LBUTTONUP:OnLButtonUP(hWnd, wParam, IParam);break;case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, msgID, wParam, IParam); }int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {AllocConsole();g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);WNDCLASS wc = { 0 };wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wc.hCursor = NULL;wc.hIcon = NULL;wc.hInstance = hIns;wc.lpfnWndProc = WndProc;wc.lpszClassName = "Main";wc.lpszMenuName = NULL;wc.style = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;RegisterClass(&wc); //將以上所有賦值全部寫入操作系統(tǒng)中//在內(nèi)存創(chuàng)建窗口HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);//顯示窗口ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);//消息循環(huán)MSG nMsg = { 0 };while (GetMessage(&nMsg, NULL, 0, 0)) { //抓消息TranslateMessage(&nMsg);//翻譯消息DispatchMessage(&nMsg);//派發(fā)消息:將消息交給窗口處理函數(shù)來(lái)處理。}return 0; } //需要在任務(wù)管理器中退出線程!滾輪消息
#include <windows.h> #include <stdio.h> HANDLE g_hOutput = 0; void OnLButtonDown(HWND hWnd,WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONDOWN:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText,strlen(szText), NULL, NULL); }void OnLButtonUP(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_LBUTTONUP:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM IParam) {char szText[256] = { 0 };sprintf_s(szText, "WM_MOUSEMOVE:其他按鍵狀態(tài):%d,X=%d,Y=%d\n", wParam, LOWORD(IParam), HIWORD(IParam));WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnLButtonDblClk(HWND hWnd) {const char* szText = "WM_LBUTTONDBLCLK\n";WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }void OnMouseWheel(HWND hWnd, WPARAM wParam) {short nDelta = HIWORD(wParam);char szText[256] = { 0 };sprintf_s(szText, "WM_MOUSEWHEEL:nDetal = %d\n", nDelta);WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL); }LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam) {switch (msgID) {case WM_MOUSEWHEEL:OnMouseWheel(hWnd,wParam);break;case WM_LBUTTONDBLCLK:OnLButtonDblClk(hWnd);break;//case WM_MOUSEMOVE:// OnMouseMove(hWnd, wParam, IParam);case WM_LBUTTONDOWN:OnLButtonDown(hWnd, wParam, IParam);break;case WM_LBUTTONUP:OnLButtonUP(hWnd, wParam, IParam);break;case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, msgID, wParam, IParam); }int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {AllocConsole();g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);WNDCLASS wc = { 0 };wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wc.hCursor = NULL;wc.hIcon = NULL;wc.hInstance = hIns;wc.lpfnWndProc = WndProc;wc.lpszClassName = "Main";wc.lpszMenuName = NULL;wc.style = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;RegisterClass(&wc); //將以上所有賦值全部寫入操作系統(tǒng)中//在內(nèi)存創(chuàng)建窗口HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);//顯示窗口ShowWindow(hWnd, SW_SHOW);UpdateWindow(hWnd);//消息循環(huán)MSG nMsg = { 0 };while (GetMessage(&nMsg, NULL, 0, 0)) { //抓消息TranslateMessage(&nMsg);//翻譯消息DispatchMessage(&nMsg);//派發(fā)消息:將消息交給窗口處理函數(shù)來(lái)處理。}return 0; } //需要在任務(wù)管理器中退出線程!總結(jié)
以上是生活随笔為你收集整理的Win32学习(七) 鼠标消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux 介绍快速浏览
- 下一篇: docker 运行花生壳实现内外网穿透