哪位大虾能详细说一下PeekMessage函数的用法?
包括MSG参数的声明,HWND参数要取FORM1窗体的句柄以及其它3个参数的意义。
最好再来一个例子,最重要是讲解一下,不要粘一大段我看不懂的代码。
谢谢各位……

解决方案 »

  1.   


    帮助里面说得再明白不过了
    The PeekMessage function checks a thread message queue for a message and places the message (if any) in the specified structure. BOOL PeekMessage(    LPMSG lpMsg, // pointer to structure for message
        HWND hWnd, // handle to window
        UINT wMsgFilterMin, // first message
        UINT wMsgFilterMax, // last message
        UINT wRemoveMsg  // removal flags
       );
     ParameterslpMsgPoints to an MSG structure that contains message information from the Windows-based application queue. hWndIdentifies the window whose messages are to be examined. wMsgFilterMinSpecifies the value of the first message in the range of messages to be examined. wMsgFilterMaxSpecifies the value of the last message in the range of messages to be examined. wRemoveMsgSpecifies how messages are handled. This parameter can be one of the following values: Value Meaning
    PM_NOREMOVE Messages are not removed from the queue after processing by PeekMessage.
    PM_REMOVE Messages are removed from the queue after processing by PeekMessage.
     You can optionally combine the value PM_NOYIELD with either PM_NOREMOVE or PM_REMOVE. However, PM_NOYIELD has no effect on 32-bit Windows applications. It is defined in Win32 solely to provide compatibility with applications written for previous versions of Windows, where it was used to prevent the current task from halting and yielding system resources to another task. 32-bit Windows applications always run simultaneously.  Return ValuesIf a message is available, the return value is nonzero.
    If no messages are available, the return value is zero. 
    不过要注意与GetMessage的区别
    如果消息队列里没有消息,PeekMessage不会等待,返回0
    而GetMessage则会等待消息队列里面有消息才返回
      

  2.   

    The PeekMessage function checks a thread message queue for a message and places the message (if any) in the specified structure. BOOL PeekMessage(    LPMSG lpMsg, // pointer to structure for message
        HWND hWnd, // handle to window
        UINT wMsgFilterMin, // first message
        UINT wMsgFilterMax, // last message
        UINT wRemoveMsg  // removal flags
       );
     ParameterslpMsgPoints to an MSG structure that contains message information from the Windows-based application queue. hWndIdentifies the window whose messages are to be examined. wMsgFilterMinSpecifies the value of the first message in the range of messages to be examined. wMsgFilterMaxSpecifies the value of the last message in the range of messages to be examined. wRemoveMsgSpecifies how messages are handled. This parameter can be one of the following values: Value Meaning
    PM_NOREMOVE Messages are not removed from the queue after processing by PeekMessage.
    PM_REMOVE Messages are removed from the queue after processing by PeekMessage.
     You can optionally combine the value PM_NOYIELD with either PM_NOREMOVE or PM_REMOVE. However, PM_NOYIELD has no effect on 32-bit Windows applications. It is defined in Win32 solely to provide compatibility with applications written for previous versions of Windows, where it was used to prevent the current task from halting and yielding system resources to another task. 32-bit Windows applications always run simultaneously.  Return ValuesIf a message is available, the return value is nonzero.
    If no messages are available, the return value is zero. ResUnlike the GetMessage function, the PeekMessage function does not wait for a message to be placed in the queue before returning. 
    PeekMessage retrieves only messages associated with the window identified by the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters. If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread making the call. (PeekMessage does not retrieve messages for windows that belong to other threads.) If hWnd is -1, PeekMessage only returns messages with a hWnd value of NULL, as posted by the 
    PostAppMessage function. If wMsgFilterMin and wMsgFilterMax are both zero, PeekMessage returns all available messages (that is, no range filtering is performed). The WM_KEYFIRST and WM_KEYLAST constants can be used as filter values to retrieve all keyboard messages; the WM_MOUSEFIRST and WM_MOUSELAST constants can be used to retrieve all mouse messages. 
    The PeekMessage function normally does not remove WM_PAINT messages from the queue. WM_PAINT messages remain in the queue until they are processed. However, if a WM_PAINT message has a null update region, PeekMessage does remove it from the queue.See AlsoGetMessage, IsChild, MSG, PostAppMessage, WaitMessage 
    哈哈,不好意思了,楼主..........
      

  3.   

    真不幸,怎么这里的高手都喜欢省事呢
    这些MSDN中的东西,你们丝毫不变的粘下来,是不能帮我解决问题的。
    我来这里是解决问题的,我不是来查MSDB或Delphi Help的,如果我想查的话
    我的电脑里也有上述这些东西,我要的是更详细的讲解,一个专家或教授在讲课时
    是不会仅仅搬着书本来念一段的,你说呢^_^
      

  4.   

    peekMessage就有像 GetMessage,用来取得消息;
    PeekMessage的第一个参数 @Msg ,在Delphi里是 TMsg, 
    你可以这样定义: var MyMsg : TMsg;
    ////////////////// main.pas //////////////
    unit main;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,MyThread;
    const
          WM_MyMessage=WM_USER+100;
    type
      TForm1 = class(TForm)
        BtnQuitThread: TButton;
        ListBox1: TListBox;
        BtnPost: TButton;
        BtnStart: TButton;
        BtnExit: TButton;
        procedure BtnQuitThreadClick(Sender: TObject);
        procedure BtnStartClick(Sender: TObject);
        procedure BtnPostClick(Sender: TObject);
        procedure BtnExitClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
          MyThread : MsgThread;
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.BtnQuitThreadClick(Sender: TObject);
    begin
    //  MyThread.Terminate;
      if MyThread = nil then exit;  if PostThreadMessage(MyThread.ThreadID,
          WM_QUIT,0,0) then
          Caption := 'Post Message Ok!'
      else
          Caption := 'Post Message Fail!';
    end;procedure TForm1.BtnStartClick(Sender: TObject);
    begin
      if (MyThread = nil) then
          MyThread := MsgThread.Create(false);
    end;procedure TForm1.BtnPostClick(Sender: TObject);
    begin
      if MyThread = nil then exit;  if PostThreadMessage(MyThread.ThreadID,
          WM_MyMessage,0,0) then
          Caption := 'Post Message Ok!'
      else
          Caption := 'Post Message Fail!';
    end;procedure TForm1.BtnExitClick(Sender: TObject);
    begin
      MyThread.Free;
      Close;
    end;end.
    ///////////// MyThread.pas/////////////////
    unit MyThread;interfaceuses
      Classes,windows, Messages;const
          WM_MyMessage=WM_USER+100;
    type
      MsgThread = class(TThread)
      private
        { Private declarations }
        FMyString : string;
      protected
        procedure Execute; override;
        procedure ShowString;
      end;implementation{ Important: Methods and properties of objects in VCL can only be used in a
      method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure PMessage.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ PMessage }
    uses Main;procedure MsgThread.Execute;
    var Msg : TMsg;
    begin
      { Place thread code here }
      FMyString := 'Thread Start!';
      Synchronize(ShowString);  while  (not Terminated) do
      begin
          if PeekMessage(Msg,0,0,0,PM_REMOVE) then
            begin
                if (Msg.message = WM_QUIT) then
                  begin
                    FMyString := 'Thread Quit';
                    Synchronize(ShowString);
                    Terminate;
                  end;
                if (Msg.message = WM_MyMessage) then
                  begin
                    FMyString := 'Thread Get a USER Message!';
                    Synchronize(ShowString);
                  end;
            end;
      end;
    end;procedure MsgThread.ShowString;
    begin
       Form1.ListBox1.Items.Add(FMyString);
    end;end.
      

  5.   

    BOOL PeekMessage(
        LPMSG lpMsg, // pointer to structure for message
        HWND hWnd, // handle to window
        UINT wMsgFilterMin, // first message
        UINT wMsgFilterMax, // last message
        UINT wRemoveMsg  // removal flags
       );
    lpMsg:一个TMsg结构的指针,即PeekMessage返回时,如果取得消息,则放入消息到这个结构里面。
    hWnd:取得某个Handle的消息,如果为0,则是消息队列中的全部消息
    wMsgFilterMin, wMsgFilterMax: 指定取消息的Min/Max Message ID值,如果你只是想取得某个消息,可以通过指定Min/Max来过滤。
    wRemoveMsg: PM_NOREMOVE, PM_REMOVE
    第一值表示取得消息后并不删除
    第一值表示取得消息后并删除消息
    其中第一值还有意思是表示创建消息队列的标识。
    在线程中使用PeekMessage,它与GetMessage一个是非阻塞和阻塞的区别,还有一个就是PeekMessage需要调用PeekMessage(msg, 0, 0, 0, PM_NOREMOVE);进行创建一个消息队列,否则在其它地方的PeekMessage调用会失败。一般的规则就是这样,如果需要更多的东西,看上面的help or MSDN