程序中有一个TrayIcon,可以最小化到托盘中,现在想在选项中加上一条“启动后最小化到托盘”,然后程序根据用户的设定来选择启动最小化或者正常显示,可是实现起来有很多问题1。在oncreate中加入
if(bOptStartMin)    TrayIcon1->Minimize();
  这样的话,程序就还是显示主窗口,,点主窗口的最小化按钮不起作用2。在oncreate中加入
if(bOptStartMin)
  {
   Application->ShowMainForm=false;
   TrayIcon1->Minimize();
  }  这样的话,程序启动后倒是最小化到托盘了,可是怎么也恢复不了,点图标后就在任务烂显示出来,不能把主窗口显示到桌面上来,加上Application.MainForm.Visible := True;也不行
我用的是 bcb ,我想delphi的方法和bcb 应该是通用的

解决方案 »

  1.   

    其他消息处理或者重新再放到托盘处的代码消息自己写写,比如当最小化窗口时,重新
    放到托盘等等project1.cpp//---------------------------------------------------------------------------#include <vcl.h>
    #pragma hdrstop
    //---------------------------------------------------------------------------
    USEFORM("Unit1.cpp", frmMain);
    //---------------------------------------------------------------------------
    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
            try
            {
                     Application->Initialize();
                     Application->CreateForm(__classid(TfrmMain), &frmMain);
                     Application->ShowMainForm=false;
                     Application->Run();...unit1.cpp
    //---------------------------------------------------------------------------#include <vcl.h>
    #pragma hdrstop#include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma link "trayicon"
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::IconTray(TMessage &Msg)
    {
      if(Msg.LParam==WM_LBUTTONUP || Msg.LParam==WM_RBUTTONUP)
        DelIcon();
      TForm::Dispatch(&Msg);
    }
    //---------------------------------------------------------------------------void __fastcall TfrmMain::AddIcon()
    {
      nid.cbSize=sizeof(TNotifyIconData);
      nid.hWnd=Handle;
      nid.uID=0;
      nid.uFlags=NIF_MESSAGE | NIF_ICON | NIF_TIP;
      nid.uCallbackMessage=WM_MENUMSG;
      nid.hIcon=Application->Icon->Handle;
      strcpy(nid.szTip,"My TrayIcon 2005");
      Shell_NotifyIcon(NIM_ADD,&nid);
      ShowWindow(Application->Handle,SW_HIDE);
    }
    //---------------------------------------------------------------------------void __fastcall TfrmMain::DelIcon()
    {
      ShowWindow(Application->Handle,SW_SHOW);
      Application->Restore();
      this->Show();
      SetForegroundWindow(Handle);
      Shell_NotifyIcon(NIM_DELETE,&nid);
    }
    //---------------------------------------------------------------------------void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
    {
      nid.cbSize=sizeof(TNotifyIconData);
      nid.hWnd=Handle;
      nid.uID=0;
      Shell_NotifyIcon(NIM_DELETE,&nid);
    }
    //---------------------------------------------------------------------------void __fastcall TfrmMain::FormCreate(TObject *Sender)
    {
      AddIcon();
      this->Hide();
    }
    //---------------------------------------------------------------------------unit1.h
    class TfrmMain : public TForm
    {
    __published: // IDE-managed Components
            void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
            void __fastcall FormCreate(TObject *Sender);
    private: // User declarations
    protected:
            BEGIN_MESSAGE_MAP
              MESSAGE_HANDLER(WM_MENUMSG,TMessage,IconTray)
            END_MESSAGE_MAP(TForm)
    public: // User declarations
            __fastcall TfrmMain(TComponent* Owner);
            TNotifyIconData nid;
            MESSAGE void __fastcall IconTray(TMessage &Msg);
            void __fastcall AddIcon();
            void __fastcall DelIcon();
    };
      

  2.   

    void __fastcall TfrmMain::SysMin(TWMSysCommand &Msg)
    {
      if(Msg.CmdType==SC_MINIMIZE)
      {
      // add your code,处理最小化窗体的消息
      }
      TForm::Dispatch(&Msg);
    }
      

  3.   

    用户怎样操作会最小化到托盘,窗口关闭或者最小化时?
    BCB没用过,估计只要模拟用户操作在启动时向主窗体发一个消息应该就可以吧
      

  4.   

    //---------------------------------------------------------------------------#ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>#define WM_MENUMSG WM_USER+100  //自定义的消息,抱歉,刚才没有全贴上,呵呵
    //---------------------------------------------------------------------------class TfrmMain : public TForm
    {
    __published: // IDE-managed Components
            void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
            void __fastcall FormCreate(TObject *Sender);
    private: // User declarations
    protected:
            BEGIN_MESSAGE_MAP
              MESSAGE_HANDLER(WM_MENUMSG,TMessage,IconTray)
            END_MESSAGE_MAP(TForm)
    public: // User declarations
            __fastcall TfrmMain(TComponent* Owner);
            TNotifyIconData nid;
            MESSAGE void __fastcall IconTray(TMessage &Msg);
            void __fastcall AddIcon();
            void __fastcall DelIcon();
    };
    ......