《windows程序设计》鼠标消息(12)
Posted yishen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《windows程序设计》鼠标消息(12)相关的知识,希望对你有一定的参考价值。
代码如下:
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, windows, Winapi.Messages, Vcl.Dialogs; type Point = record x: Integer; y: Integer; end; var swndClass: tagWNDCLASS; message: MSG; mHwnd: hwnd; cxClient, cyClient: Integer; function WindowProc(hwnd: hwnd; uMsg: UINT; wParam: wParam; lParam: lParam): LRESULT; stdcall; var i: integer; uhdc: HDC; ps: PAINTSTRUCT; begin case uMsg of WM_CREATE: begin end; WM_SIZE: begin cxclient := loword(lParam); cyclient := HiWord(lParam); result := 0; Exit; end; wm_paint: begin uhdc := BeginPaint(hwnd, ps); EndPaint(hwnd, ps); Exit; end; WM_MOUSEMOVE: begin Writeln(‘鼠标移动:‘ + HiWord(lParam).ToString + ‘,‘+loword(lParam).ToString ); end; WM_LBUTTONDOWN: begin Writeln(‘鼠标左键按下:‘ + HiWord(lParam).ToString + ‘,‘+loword(lParam).ToString ); end; WM_LBUTTONUP: begin Writeln(‘鼠标左键弹起:‘ + HiWord(lParam).ToString + ‘,‘+loword(lParam).ToString ); end; WM_RBUTTONDOWN: begin Writeln(‘鼠标右键按下:‘ + HiWord(lParam).ToString + ‘,‘+loword(lParam).ToString ); end; WM_RBUTTONUP: begin Writeln(‘鼠标右键弹起:‘ + HiWord(lParam).ToString + ‘,‘+loword(lParam).ToString ); end; end; result := DefWindowProc(hwnd, uMsg, wParam, lParam); end; begin swndClass.cbClsExtra := 0; //窗口类扩展,无 swndClass.cbWndExtra := 0; //窗口实例扩展 swndClass.hbrBackground := COLOR_BACKGROUND; //窗口背景颜色黑色 //LoadCursor() swndClass.hCursor := LoadCursor(0, IDC_ARROW); //窗口采用箭头光标 swndClass.hIcon := LoadIcon(0, IDI_APPLICATION); //窗口最小化图标:采用缺省图标 swndClass.hInstance := hInstance; //窗口实例句柄 swndClass.lpfnWndProc := @WindowProc; //窗口处理函数 swndClass.lpszClassName := ‘myWnd‘; //窗口类名 swndClass.lpszMenuName := nil; //窗口菜单 swndClass.style := CS_DBLCLKS; //窗口样式 if RegisterClass(swndClass) = 0 then begin Writeln(‘windows class register error!‘); Exit; end; mHwnd := CreateWindowEx(0, ‘myWnd‘, ‘Delphi Windows‘, WS_OVERLAPPEDWINDOW, {滚动条添加} CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, HWND_DESKTOP, 0, hInstance, 0); ShowWindow(mHwnd, SW_SHOW); UpdateWindow(mHwnd); while GetMessage(message, 0, 0, 0) do begin TranslateMessage(message); DispatchMessage(message); end; end.
以上是关于《windows程序设计》鼠标消息(12)的主要内容,如果未能解决你的问题,请参考以下文章
终于懂了:Delphi重定义消息结构随心所欲,只需要前4个字节是消息编号就行了,跟Windows消息虽然尽量保持一致,但其实相互没有特别大的关系。有了这个,就有了主动,带不带句柄完全看需要。(代码片段