为什么不能传递窗体的handle给另一个类的hwnd数据类型的变量?????如何才能在另一个类中得到窗体的句柄??

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows,shellapi,QMenus, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, Menus, StdCtrls; //自定义消息}
    const MY_MESSAGE = WM_USER + 102;
    const fmsg = WM_USER + 103;
    type
    TNotify=class(tcomponent)
      //const fmsg = WM_USER + 102;  //
       private
        fnd:TNotifyIconData;
        fhicon:hicon;
        //fmsg:tmessage;             //'ucallbackmessage    fwnd:hwnd;                 //'窗体句柄
        fRbutoonPop:Tpopupmenu;    //'右击托盘出现的菜单
        procedure OnIconNotify(var Message: TMessage); message fmsg;
      public
        constructor create(aowner:tcomponent);override;//'''fwnd1是窗体句柄
        procedure hideform();   //'''隐藏窗体
        procedure seticon(ficon:hicon); //'''设置图标
        procedure setmessage(msg:Tmessage);  //'''设置回调函数
        procedure setwnd(fwnd1:hwnd);
        procedure showme;                    //'''显示
        procedure setRbutoonPop(popmenu:Tpopupmenu); //'''设置右击鼠标时候弹出的菜单
      end;type
      TForm1 = class(TForm)
        imgicon: TImage;
        popmenu: TPopupMenu;
        open1: TMenuItem;
        close1: TMenuItem;
        exit1: TMenuItem;
        Button1: TButton;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
         fnotify:tnotify;  public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    //var  fnd:TNotifyIconData;
    begin
    //fnd.cbsize:=sizeof(fnd);
    //fnd.wnd:=form1.Handle;
    //fnd.uid:=1;
    //shell_notifyicon(nim_delete,@fnd);
    //shell_notifyicon(nim_delete,@fnd);
    end;{ TNotify }constructor TNotify.create(aowner:tcomponent);
    begin
    inherited create(nil);
    //
    //showmessage(inttostr(fwnd1));//fwnd:=fwnd1;
    //FWindowHandle := AllocateHWnd(WndProc);
    //inherited Create(AOwner);
    //fwnd := TForm(AOwner).Handle
    //fwnd:=  AllocateHWnd(OnIconNotify);
    end;procedure TNotify.hideform;
    begin
      showwindow(fwnd, SW_HIDE);
      Application.ShowMainForm := False;
    end;procedure TNotify.OnIconNotify(var Message: TMessage);
    var Busy:Boolean;
    begin
       busy:=false;
      if not Busy then
        begin
        Busy:=true;
        if Message.LParam=WM_LBUTTONDOWN then
          begin
          SetForegroundWindow(fwnd);
          showmessage('fddd');
          end;
          //if Application.MessageBox('Are you sure','Exit', MB_YESNO)=IDYES then Close;
        Busy := false;
        end;
    end;procedure TNotify.seticon(ficon: hicon);
    begin
      fhicon:=ficon;
    end;procedure TNotify.setmessage(msg: Tmessage);
    begin
      //fmsg:=msg;
    end;procedure TNotify.setRbutoonPop(popmenu: Tpopupmenu);
    begin
      fRbutoonPop:=popmenu;
    end;procedure TNotify.setwnd(fwnd1: hwnd);
    begin
    fwnd:=fwnd1;
    //fwnd:=findwindow('tform1',nil);
    end;procedure TNotify.showme;
    begin
     //
      fnd.cbsize:=sizeof(fnd);
      fnd.wnd:=fwnd;
      fnd.uid:=1;
      fnd.uflags:=nif_message or nif_icon or nif_tip;
      fnd.ucallbackmessage:=fmsg;
      fnd.hIcon:=fhicon;
      shell_notifyicon(nim_add,@fnd);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    //fnotify.create(nil);
      fnotify.setwnd(self.Handle) ;
      fnotify.seticon(imgicon.Picture.Icon.Handle);
      //fnotify.setmessage(MY_MESSAGE);
      fnotify.setRbutoonPop(popmenu);
     
      fnotify.showme;
       
    end;end.
      

  2.   

    每个窗口在系统中有唯一的HANDLE,是不能重复的,只读的;
      

  3.   

    procedure TNotify.setwnd(fwnd1: hwnd);
    begin
    fwnd:=fwnd1;////////////出错出错???????????
    //fwnd:=findwindow('tform1',nil);
    end;