关于托盘与窗体的问题,请教各位高手 ( 积分: 10 )

  • 主题发起人 主题发起人 文生
  • 开始时间 开始时间

文生

Unregistered / Unconfirmed
GUEST, unregistred user!
当程序运行后便自动缩到托盘中,当点击托盘时,程序的主窗体才t可以显示出来.
我用application.ShowMainForm:=false; 来隐藏主窗体,但当主窗体显示出来后,主窗体里面放元件去看不到了,请教各位
 
当程序运行后便自动缩到托盘中,当点击托盘时,程序的主窗体才t可以显示出来.
我用application.ShowMainForm:=false; 来隐藏主窗体,但当主窗体显示出来后,主窗体里面放元件去看不到了,请教各位
 
unit U_Main;

interface

uses
// Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
// Dialogs, Menus, ImgList;

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Gauges, StdCtrls, Menus,shellapi, ExtCtrls, Buttons,
mmsystem, ImgList, ComCtrls, RzEdit, ToolWin,inifiles;

type
//------------------定义托盘图标的数据类型 ---------------------------
PNotifyIconData = ^TNotifyIconDataA;
TNotifyIconDataA = record
cbSize : DWORD;
Wnd : HWND;
uID : UINT;
uFlags : UINT;
uCallbackMessage : UINT;
hIcon : HICON;
szTip : array [0..63] of AnsiChar;
end;
//----------------------------------------------------------------------
type
TFr_Client = class(TForm)
PopupMenu1: TPopupMenu;
Nmain: TMenuItem;
Nabout: TMenuItem;
Nexit: TMenuItem;
ImageList1: TImageList;
Panel1: TPanel;
RzRichEdit1: TRzRichEdit;
Panel2: TPanel;
Splitter1: TSplitter;
Panel3: TPanel;
RzRichEdit2: TRzRichEdit;
Panel4: TPanel;
PopupMenu2: TPopupMenu;
Enter1: TMenuItem;
Enter2: TMenuItem;
FontDialog1: TFontDialog;
ToolBar1: TToolBar;
Tfont: TToolButton;
Tbold: TToolButton;
Tita: TToolButton;
Tunder: TToolButton;
Tface: TToolButton;
Tie: TToolButton;
Tmess: TToolButton;
PopupMenu3: TPopupMenu;
N1: TMenuItem;
Panel5: TPanel;
Bsend: TButton;
Bdown: TButton;
Label1: TLabel;
Button1: TButton;
N2: TMenuItem;
N3: TMenuItem;
function CurrText: TTextAttributes;
procedure NexitClick(Sender: TObject);
procedure NaboutClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
procedure NmainClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BdownClick(Sender: TObject);
procedure Enter1Click(Sender: TObject);
procedure Enter2Click(Sender: TObject);
procedure TfontClick(Sender: TObject);
procedure TboldClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure TitaClick(Sender: TObject);
procedure TunderClick(Sender: TObject);
procedure N3Click(Sender: TObject);
private
{ Private declarations }
//-------------------------------------------------------------
IconData: TNotifyIconData;
procedure ShowIcon;
procedure IconOnClick(var message:TMessage); message WM_USER+1;
Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;
//-------------------------------------------------------------
public
{ Public declarations }
end;

var
Fr_Client: TFr_Client;
//全程变量
Tray:integer;
ini:Tinifile;
app:string;
implementation

uses Uabout, USM;

{$R *.dfm}

function TFr_Client.CurrText: TTextAttributes;
begin
if RzRichEdit2.SelLength > 0 then
Result := RzRichEdit2.SelAttributes
else
Result := RzRichEdit2.DefAttributes;
end;


//托盘图标事件
procedure TFr_Client.IconOnClick(var message: Tmessage);
var p : TPoint;
begin
if (message.lParam = WM_LBUTTONDOWN) then
begin
ShowWindow(Handle, SW_Show )
end;


if (message.lParam = WM_RBUTTONDOWN) then
begin
GetCursorPos(p);
SetForegroundWindow (Application.Handle); //avoid a little bug.
Application.ProcessMessages;
popupmenu1.Popup( p.x ,p.y );
end;
end;

Procedure TFr_Client.WMSysCommand(Var Message : TMessage) ;
begin
//最小化显示托盘图标
if (Message.WParam = SC_MINIMIZE) or (Message.WParam =SC_CLOSE) then
begin
if Tray=0 then begin
ShowIcon;
Tray:=1
end else
begin
Shell_NotifyIcon( NIM_DELETE, @IconData );
ShowIcon;
end;
end
else
Inherited;
end;

procedure TFr_Client.ShowIcon;
begin
IconData.cbSize := SizeOf( IconData );
IconData.Wnd := Handle;
IconData.uID :=1;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage :=WM_USER+1;
IconData.hIcon := application.Icon.Handle;
IconData.szTip := '信息接收系统';
Shell_NotifyIcon( NIM_ADD, @IconData );
ShowWindow(Handle, SW_HIDE);
hide;
end;


procedure TFr_Client.NexitClick(Sender: TObject);
begin
close;
end;

procedure TFr_Client.NaboutClick(Sender: TObject);
begin
Fr_about.show;
end;


procedure TFr_Client.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
Shell_NotifyIcon( NIM_DELETE, @IconData ); //清除托盘图标
end;

procedure TFr_Client.FormShow(Sender: TObject);
begin
Tray:=0;
end;

procedure TFr_Client.NmainClick(Sender: TObject);
begin
ShowWindow(Handle, SW_SHOW );
end;

procedure TFr_Client.FormCreate(Sender: TObject);
begin
showwindow(application.Handle,sw_hide);
setwindowlong(application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW); //隐藏
//SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_APPWINDOW);不隐藏
ShowIcon;
end;


procedure TFr_Client.FormActivate(Sender: TObject);
begin
showwindow(application.Handle,sw_hide);
end;

end.
 
加上application.showmainform:=false能把主窗体隐蔽,可是为什么当点击托盘打开主窗体后,主窗体中却是一片空白,以前上面的控件全看不到了??????
 
你用两个窗体,不就行了。
 
没遇到过这样的问题。
用下面的语句来替换显示/隐藏,试试
visible:=not visible;
application.ShowMainForm:=visible;
setforegroundwindow(application.handle);
 
谁能给我做一个最简单的托盘程序:要求如下:

1/程序一运行便托盘到右下角
2/点击托盘,出现主窗体,主窗体上有一些按钮,和edit框,
3/最小化时便托盘到右下角
4/右点托盘出现退出菜单,
 
我解决了,谢谢
 
后退
顶部