delphi封裝了消息.雖然可以寫消息處理事件,
但我現在有一個想法就是能不能用一個函數來接管所有的
對本程序的消息..
如在form上還有edit,button等我要怎么知道消息是發給哪個控件的呢?
嫌分少可再加,但請給出個sample

解决方案 »

  1.   

    SetwindowLong(m_hWnd,GWL_WNDPROC,YourProc)
    設定新的窗口過程
    在YourProc中處理所有消息
      

  2.   

    to cool099:
    我知道用setwindowlong可以設置函數..
      但我舉個實例,我有兩個edit  .edit1,edit2
     當我收到信息是wm_settext時我要怎么識別是發給誰的.
     
      

  3.   

    要用MESSAGE中的参数来判断..
      但是我不清楚MESSAGE的参数具体表示的意义.有哪位能赐教
      

  4.   

    在窗體的WNDPROC裡
    WM_CREATE:
    hwndEdit1=CreateWindow("EDIT","edit1",WS_BORDER|WS_VISIBLE,0,0,100,60,hWnd,NULL,hInst,NULL);
    preWndproc=SetWindowLong(hwndEdit1,GWL_WNDPROC,(LPARAM)(WNDPROC)Edit_Proc);
    break;LRESULT CALLBACK Edit_Proc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    LRESULT lresult=0;
    switch(message)
    {
    case WM_SETTEXT:
    MessageBox(hWnd,(LPCTSTR)lParam,"info",MB_OK);
    //SetWindowText(hWnd,(LPCTSTR)lParam);
            
            default:
        lresult=CallWindowProc((WNDPROC)GetClassLong(hWnd,GCL_WNDPROC),hWnd,message,wParam,lParam);
    }
    return lresult;
    }
      

  5.   

    另一方法是用Hook,截獲所有消息
      

  6.   

    直接写一个过程;
    接管Application.OnMessage 就行了!!
      

  7.   

    接管Application.OnMessage是不行的,不信可以试试
      

  8.   

    试试这个         //检查一个线程消息队列,将所选的范围保存到消息纪录中
             //BOOL PeekMessage(
             //             LPMSG lpMsg,          // 消息记录的指针
             //             HWND hWnd,          // 窗口句柄
             //             UINT wMsgFilterMin,  // 第一个消息
             //             UINT wMsgFilterMax,  // 最后一个消息
             //             UINT wRemoveMsg      // 标志    Value        Meaning
             //            );                                  PM_NOREMOVE 处理后保留在消息队列中
             //                                                PM_REMOVE 处理后从消息队列中清除
             //要做的第一件事是检查是否有消息在等待。
             //使用PeekMessage()可以在不锁住我们的程序的前提下对消息进行检查。
             //许多程序使用GetMessage(),也可以很好的工作。
             //但使用GetMessage(),程序在收到paint消息或其他别的什么窗口消息之前不会做任何事。
             If (PeekMessage(msg, 0, 0, 0, PM_REMOVE)) Then //检查是否有消息
                // wMsgFilterMin,wMsgFilterMax 这两个参数都为0,返回所有可用的消息
                Begin
                   If (msg.message = WM_QUIT) Then // 如果是退出消息
                      finished := True      //改变循环条件,退出
                   Else
                      Begin                 // 否则处理消息
                         // 翻译消息,然后发送消息,使得WndProc() 或 Windows能够处理他们。
                         TranslateMessage(msg); //翻译消息
                         DispatchMessage(msg); //发送消息
                      End;
                End
      

  9.   

    不用这么复杂吧,直接窗口的WndProc就可以了,覆盖这个函数,在里面添加新的消息选择项就OK了!
      

  10.   

    樓上的對啊﹐self.WndProc:=xxxProc;
      

  11.   

    据个例子,我要对WM_WINDOWPOSCHANGING消息进行处理
    方法如下:
    这是在类中的声明:
    procedure OnPosChange(var Msg: TWmWindowPosChanging);
          message WM_WINDOWPOSCHANGING;这是具体实现:
    procedure TfrmTJZongTi.OnPosChange (var Msg: TWmWindowPosChanging);
    begin//frm是另一个窗口
      if msg.WindowPos.x > frmMain.ClientRect.Right  - frmMain.clbMenu.Width - width - 4 then
        msg.WindowPos.x := frmMain.ClientRect.Right  - frmMain.clbMenu.Width - width - 4;
      if msg.WindowPos.x < frmMain.ClientRect.Left then
        msg.WindowPos.x := frmMain.ClientRect.Left;
      if msg.WindowPos.y > frmMain.ClientRect.Bottom - frmMain.stbStatus.Height - height - 4 then
        msg.WindowPos.y := frmMain.ClientRect.Bottom - frmMain.stbStatus.Height - height - 4;
      if msg.WindowPos.y < frmMain.ClientRect.Top then
        msg.WindowPos.y := frmMain.ClientRect.Top;
    end;
      

  12.   

    我希望你看看DELPHI开发人员指南,上面写的非常好。比这么问要明白得多,花上一段时间去书店看也好啊。
      

  13.   

    用TNotifyEvent 和 tag来处理 .  给例.  只要找到tag就可以判定是谁.procedure InitMenu(mm:TMainMenu; proc:TNotifyEvent);
    var
      j, i:integer;
    begin
      gMainMenu:=mm;
      for i:=0 to mm.Items.Count -1 do begin   
        for j:=0 to mm.Items[i].Count -1 do begin  
          if not Assigned(mm.Items[i].Items[j].OnClick) then
          begin
            mm.Items[i].Items[j].OnClick:=proc;
            if mm.Items[i].Items[j].Name = 'mmView' then
                mm.Items[i].Items[j].tag := WMN_VIEW;
            if mm.Items[i].Items[j].Name = 'mmEdit' then
                mm.Items[i].Items[j].tag := WMN_EDIT;
            if mm.Items[i].Items[j].Name = 'mmSearch' then
                mm.Items[i].Items[j].tag := WMN_SEARCH;
            if mm.Items[i].Items[j].Name = 'mmAddRef' then
                mm.Items[i].Items[j].tag := WMN_ADDREF;
            if mm.Items[i].Items[j].Name = 'mmBack' then
                mm.Items[i].Items[j].tag := WMN_BACK;
            if mm.Items[i].Items[j].Name = 'mmNewFrom' then
                mm.Items[i].Items[j].tag := WMN_NEWFROM;
          end;
        end;
      end;
    end;
      

  14.   

    建议看看vcl源码,windows系统消息的封装是在Tcontrol中完成的,在源码中能看到它的消息是怎么分发的(dispatchmessage())和怎么处理的(WndProc).
      

  15.   

    WndProc!
    这个一定是你想要的