wparam和lparam的思考

转载地址:http://www.cnblogs.com/magic-cube/archive/2011/04/27/2029913.html


首先看看定义:

1
2
3
4
typedef unsignedint        UINT;
typedef UINT WPARAM;//就是无符号整形
typedef long LONG;
typedef LONG LPARAM;//就是长整形

这就是所谓的wparam和lparam,从定义我们可以看出lparam比wparam要长,所以习惯上都用wparam来存储消息类型,而用lparam来存储要传递的参数,当然它的类型随着消息的不同而变化:如发送一个WM_LBUTTONDOWN消息,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SendMessage针对鼠标消息中wParam和lParam参数
SendMessage(Hwnd, WM_LBUTTONDOWN, wparam, lparam);
SendMessage(Hwnd, WM_LBUTTONUP, wparam, lparam); 
    
wParam //消息类型 
MK_CONTROL   Set if the ctrl key is down.   
MK_LBUTTON   Set if the left mouse button is down.   
MK_MBUTTON   Set if the middle mouse button is down.   
MK_RBUTTON   Set if the right mouse button is down.   
MK_SHIFT     Set if the shift key is down.   
    
lParam //传递参数
xPos = LOWORD(lParam);     //horizontal position of cursor   
yPos = HIWORD(lParam);     //vertical position of cursor
#include <windows.h> class CWnd { public: BOOL CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam); // window-creation data BOOL ShowWindow(int nCmdShow); BOOL UpdateWindow(); public: HWND m_hWnd; }; BOOL CWnd::CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam) // window-creation data { m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle, x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam); if(m_hWnd!=NULL) return TRUE; else return FALSE; } BOOL CWnd::ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd,nCmdShow); } BOOL CWnd::UpdateWindow() { return ::UpdateWindow(m_hWnd); } LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { WNDCLASS MyWnd; MyWnd.cbClsExtra = NULL; MyWnd.cbWndExtra = NULL; MyWnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); MyWnd.hCursor = LoadCursor(NULL, IDC_ARROW); MyWnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); MyWnd.hInstance = hInstance; MyWnd.lpfnWndProc = MyWndProc; MyWnd.lpszClassName = "Hello"; MyWnd.lpszMenuName = NULL; MyWnd.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&MyWnd); CWnd wnd; wnd.CreateEx(0,"Hello","CWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); wnd.ShowWindow(SW_SHOWNORMAL); wnd.UpdateWindow(); MSG msg; while (GetMessage(&msg,NULL,0,0)) //?????????? { TranslateMessage(&msg); //???? //4.???? DispatchMessage(&msg); //??????????? } return 0; } LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 在窗口中心绘制"Hello, World!" RECT rect; GetClientRect(hwnd, &rect); const char* text = "Hello, World!"; DrawText( hdc,text, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); EndPaint(hwnd, &ps); break; } case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) // 左键拖拽时绘制 { // 记录轨迹点... InvalidateRect(hwnd, NULL, FALSE); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } 拓展各种功能,尽可能多
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值