深入delphi编程理解之消息(二)发送消息函数及消息编号、消息结构体的理解

  一、delphi发送消息的函数主要有以下三个:

 (一)、SendMessage函数,其原型如下:

function SendMessage(
  hWnd: HWND;        {目标句柄}
  Msg: UINT;         {要发送的消息(编号)}
  wParam: WPARAM;    {消息参数}
  lParam: LPARAM     {消息参数}
): LRESULT; stdcall; {返回处理结果}

 (二)、PostMessage函数

function PostMessage(
  hWnd: HWND;     {目标句柄}
  Msg: UINT;      {要发送的消息(编号)}
  wParam: WPARAM; {消息参数}
  lParam: LPARAM  {消息参数}
): BOOL; stdcall; {是否发送成功}

  (三)、Perform 函数

function Perform(
  Msg: Cardinal;  {要发送的消息(编号)}
  WParam: WPARAM; {消息参数}
  LParam: LPARAM  {消息参数}
): LRESULT;       {返回处理结果}

  SendMessage和PostMessage是API函数,Perform是delphi从TControl类开始定义的方法,所有从TControl继承的子类对象都可以使用。SendMessage和Perform方法,发送消息后,要等系统回馈结果后才执行下一步操作,而PostMessage函数则只管发送消息,发送消息后程序会马上执行下一步代码。

  二、delphi 消息编号(Msg)规则及参数结构理解

      (一)、消息编号

  消息编号是window或者用户为各种不同消息定义唯一编号,他其实就是无符号整数(Cardinal),为了便于理解, delphi以常量形式对每一个数字进行定义,主要有以下几种形式:

   1、windows消息,以WM_为前缀,如WM_CLOSE为窗口关闭消息;

        2、delphi自定义消息,以CM_、CN为前缀;

  3、用户自定义消息 以WM_USER基数,如  Const MyMessage=WM_USER+1;

       定义消息编号主要作用是定位消息处理函数,作为message关键字的识别码,例如以下函数头的定义:

 procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;

      以上函数头定义了WM_KEYDOWN消息编号,其常量数值为 WM_KEYDOWN= $0100,其作用就是当窗口接受到编号为$100的消息就执行WMKeyDown这个函数。

   (二)、消息结构体

         消息结构体一般作为消息函数的参数类型出现,如上例中的TWMKeyDown就是一个消息结构体。消息结构体的标准定义如下:

PMsg = ^TMsg;
tagMSG = packed record
  hwnd: HWND;
  message: UINT;
  wParam: WPARAM;
  lParam: LPARAM;
  time: DWORD;
  pt: TPoint;
end;
TMsg = tagMSG;
MSG = tagMSG;

      这种标准结构体与delphi VCL消息结构体并不兼容,所以delphi定义了自己的消息结构体。

PMessage = ^TMessage;
TMessage = packed record
  Msg: Cardinal; {消息编号}
  case Integer of
    0: (
      WParam: WPARAM;
      LParam: LPARAM;
      Result: LRESULT);
    1: (
      WParamLo: Word;
      WParamHi: Word;
      LParamLo: Word;
      LParamHi: Word;
      ResultLo: Word;
      ResultHi: Word);
end;

  在我们实际使用中,虽然标准的结构体可以满足我们的使用,但由于使用过程中我们还要进行数据转换,为了更加便捷和易于理解,delphi在message单元文件中为我们定义大量的消息结构体,供我们直接调用。

TWMNoParams
TWMCancelMode       = TWMNoParams;
TWMChildActivate    = TWMNoParams;
TWMClear            = TWMNoParams;
TWMClose            = TWMNoParams;
TWMCopy             = TWMNoParams;
TWMCut              = TWMNoParams;
TWMDestroy          = TWMNoParams;
TWMDestroyClipboard = TWMNoParams;
TWMDrawClipboard    = TWMNoParams;
TWMFontChange       = TWMNoParams;
TWMGetDlgCode       = TWMNoParams;
TWMGetFont          = TWMNoParams;
TWMGetHotKey        = TWMNoParams;
TWMGetTextLength    = TWMNoParams;
TWMMDIGetActive     = TWMNoParams;
TWMMDIIconArrange   = TWMNoParams;
TWMMDIRefreshMenu   = TWMNoParams;
TWMNCDestroy        = TWMNoParams;
TWMPaintIcon        = TWMNoParams;
TWMPaste            = TWMNoParams;
TWMQueryDragIcon    = TWMNoParams;
TWMQueryNewPalette  = TWMNoParams;
TWMQueryOpen        = TWMNoParams;
TWMQueueSync        = TWMNoParams;
TWMRenderAllFormats = TWMNoParams;
TWMSysColorChange   = TWMNoParams;
TWMTimeChange       = TWMNoParams;
TWMQueryUIState     = TWMNoParams;
TWMUndo             = TWMNoParams;
TWMKey
TWMChar       = TWMKey;
TWMKeyDown    = TWMKey;
TWMKeyUp      = TWMKey;
TWMDeadChar   = TWMChar;
TWMSysChar    = TWMKey;
TWMSysKeyDown = TWMKey;
TWMSysKeyUp   = TWMKey;
TWMMouse
TWMLButtonDblClk = TWMMouse;
TWMLButtonDown   = TWMMouse;
TWMLButtonUp     = TWMMouse;
TWMMButtonDblClk = TWMMouse;
TWMMButtonDown   = TWMMouse;
TWMMButtonUp     = TWMMouse;
TWMMouseMove     = TWMMouse;
TWMRButtonDblClk = TWMMouse;
TWMRButtonDown   = TWMMouse;
TWMRButtonUp     = TWMMouse;
TWMMouseWheel
TMSHMouseWheel
TWMWindowPosMsg
TWMWindowPosChanged  = TWMWindowPosMsg;
TWMWindowPosChanging = TWMWindowPosMsg;
TWMScroll
TWMHScroll = TWMScroll;
TWMVScroll = TWMScroll;
TWMActivate
TWMActivateApp
TWMAskCBFormatName
TWMChangeCBChain
TWMCharToItem
TWMVKeyToItem = TWMCharToItem;
TWMChooseFont_GetLogFont
TWMCommand
TWMCompacting
TWMCompareItem
TWMCopyData
TWMCreate
TWMCtlColor
TWMCtlColorBtn       = TWMCtlColor;
TWMCtlColorDlg       = TWMCtlColor;
TWMCtlColorEdit      = TWMCtlColor;
TWMCtlColorListbox   = TWMCtlColor;
TWMCtlColorMsgbox    = TWMCtlColor;
TWMCtlColorScrollbar = TWMCtlColor;
TWMCtlColorStatic    = TWMCtlColor;
TWMDDE_Ack
TWMDDE_Advise
TWMDDE_Data
TWMDDE_Execute
TWMDDE_Initiate
TWMDDE_Poke
TWMDDE_Request
TWMDDE_Terminate
TWMDDE_Unadvise
TWMDeleteItem
TWMDevModeChange
TWMDrawItem
TWMDropFiles
TWMEnable
TWMEndSession
TWMEnterIdle
TWMEnterMenuLoop
TWMExitMenuLoop = TWMEnterMenuLoop;
TWMEraseBkgnd
TWMGetIcon
TWMGetMinMaxInfo
TWMGetText
TWMHotKey
TWMHScrollClipboard
TWMIconEraseBkgnd = TWMEraseBkgnd;
TWMInitDialog
TWMInitMenu
TWMInitMenuPopup
TWMKillFocus
TWMMDIActivate
TWMMDICascade
TWMMDICreate
TWMMDIDestroy
TWMMDIMaximize
TWMMDINext
TWMMDIRestore
TWMMDISetMenu
TWMMDITile
TWMMeasureItem
TWMMenuChar
TWMMenuSelect
TWMMouseActivate
TWMMove
TWMMoving
TWMNCActivate
TWMNCCalcSize
TWMNCCreate
TWMNCHitTest
TWMNCHitMessage
TWMNCLButtonDblClk = TWMNCHitMessage;
TWMNCLButtonDown   = TWMNCHitMessage;
TWMNCLButtonUp     = TWMNCHitMessage;
TWMNCMButtonDblClk = TWMNCHitMessage;
TWMNCMButtonDown   = TWMNCHitMessage;
TWMNCMButtonUp     = TWMNCHitMessage;
TWMNCMouseMove     = TWMNCHitMessage;
TWMNCRButtonDblClk = TWMNCHitMessage;
TWMNCRButtonDown   = TWMNCHitMessage;
TWMNCRButtonUp     = TWMNCHitMessage;
TWMNCPaint
TWMNextDlgCtl
TWMNotify
TWMNotifyFormat
TWMPaint
TWMPaintClipboard
TWMPaletteChanged
TWMPaletteIsChanging
TWMParentNotify
TWMPower
TWMQueryEndSession
TWMQuit
TWMRenderFormat
TWMSetCursor
TWMSetFocus
TWMSetFont
TWMSetHotKey
TWMSetIcon
TWMSetRedraw
TWMSetText
TWMShowWindow
TWMSize
TWMSizeClipboard
TWMSpoolerStatus
TWMStyleChange
TWMStyleChanged  = TWMStyleChange;
TWMStyleChanging = TWMStyleChange;
TWMSysCommand
TWMSysDeadChar
TWMSystemError
TWMTimer
TWMUIState
TWMChangeUIState = TWMUIState;
TWMUpdateUIState = TWMUIState;
TWMVScrollClipboard
TWMWinIniChange
TWMSettingChange
TWMHelp
TWMDisplayChange
TWMContextMenu
TWMPrint
TWMPrintClient = TWMPrint;

深入delphi编程理解之消息(二)发送消息函数及消息编号、消息结构体的理解

上一篇:什么是SEO渠道资源整合


下一篇:一个看一次就永远不会忘的windows环境开发小技巧