码迷,mamicode.com
首页 > Windows程序 > 详细

终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)

时间:2016-02-06 01:37:19      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:

Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型,

而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据的含义也不一样。

time表示产生消息的时间,pt表示产生消息时鼠标的位置。

技术分享

里面没有Result的选项。然后我用VC2008实测MSG结构的大小:

#include <afx.h>

void Cxe111Dlg::OnBnClickedButton1()
{
    CString m_Str;
    int ddd = sizeof(MSG);
    m_Str.Format(_T("%d"), ddd);
    AfxMessageBox(m_Str);
}

结果等于28

void Cxe111Dlg::OnBnClickedButton1()
{
    CString m_Str;
    int ddd = sizeof(WM_SIZE);
    m_Str.Format(_T("%d"), ddd);
    AfxMessageBox(m_Str);
}

结果等于4

void Cxe111Dlg::OnBnClickedButton1()
{
    CString m_Str;
    int ddd = sizeof(WM_CHAR);
    m_Str.Format(_T("%d"), ddd);
    AfxMessageBox(m_Str);
}

结果等于4

-------------------------------------------------------------

再来看Delphi里的定义,它也有原模原样的定义,只不过一般情况下用不到:

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

  TMsg = tagMSG;
  MSG = tagMSG;

经过测试,它的大小当然也是28:

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(sizeof(tagMSG)));
end;

再看Delphi真正使用的消息结构,注意它包括了Result:

  PMessage = ^TMessage;
  TMessage = packed record
    Msg: Cardinal;
    case Integer of
      0: (
        WParam: Longint;
        LParam: Longint;
        Result: Longint);
      1: (
        WParamLo: Word;
        WParamHi: Word;
        LParamLo: Word;
        LParamHi: Word;
        ResultLo: Word;
        ResultHi: Word);
  end;

测试它的大小:

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(sizeof(TMessage)));
end;

结果等于16

-------------------------------------------------------------

再测试消息本身的大小:

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(sizeof(WM_CHAR)));
end;

结果等于2

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(sizeof(WM_SIZE)));
end;

结果等于1

-------------------------------------------------------------

再来看Delphi定义的消息结构体:

  TWMSize = packed record
    Msg: Cardinal;
    SizeType: Longint; { SIZE_MAXIMIZED, SIZE_MINIMIZED, SIZE_RESTORED,
                         SIZE_MAXHIDE, SIZE_MAXSHOW }
    Width: Word;
    Height: Word;
    Result: Longint;
  end;

  TWMKey = packed record
    Msg: Cardinal;
    CharCode: Word;
    Unused: Word;
    KeyData: Longint;
    Result: Longint;
  end;

  TWMChar = TWMKey;

  TWMPaint = packed record
    Msg: Cardinal;
    DC: HDC;
    Unused: Longint;
    Result: Longint;
  end;

  TWMCommand = packed record
    Msg: Cardinal;
    ItemID: Word;
    NotifyCode: Word;
    Ctl: HWND;
    Result: Longint;
  end;

  TWMNotify = packed record
    Msg: Cardinal;
    IDCtrl: Longint;
    NMHdr: PNMHdr;
    Result: Longint;
  end;

测试Delphi消息结构体的大小:

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(sizeof(TWMSize)));
  ShowMessage(IntToStr(sizeof(TWMChar)));
  ShowMessage(IntToStr(sizeof(TWMPaint)));
  ShowMessage(IntToStr(sizeof(TWMCommand)));
  ShowMessage(IntToStr(sizeof(TWMNotify)));
end;

怎么测都是16字节大小。。。

终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)

标签:

原文地址:http://www.cnblogs.com/findumars/p/5183689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!