托盘程序中的窗口最小化后不能显示在任务栏而是显示在屏幕的左下角,请问如何解决?(100分)

  • 主题发起人 主题发起人 dywapple
  • 开始时间 开始时间
D

dywapple

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟在做一个项目时需要创建一个托盘程序,在此托盘程序中有若干个窗口。下面是显示主窗口的代码(主窗口除了提供一个菜单以调用其他窗口外其实什么都不做):
unit XCH_main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ShellAPI, ExtCtrls, Menus, ImgList, StdCtrls,,
Buttons, FileCtrl,
DB, DBClient;

//自定义消息,当小图标捕捉到鼠标时间时Windows向回调函数发送此消息

type
TfrmMain = class(TForm)
upLoadTimer: TTimer;
PopupMenu1: TPopupMenu;
MIExit: TMenuItem;
ilsMain: TImageList;
MIMainForm: TMenuItem;
MIParamSetting: TMenuItem;
MIDownLSerSetting: TMenuItem;
MIUploadFile: TMenuItem;
MIDownLoadFile: TMenuItem;
MICancelSend: TMenuItem;
MIWatchInfo: TMenuItem;
MIWatchSendInfo: TMenuItem;
MIWatchReceiveInfo: TMenuItem;
MIUpLSerSetting: TMenuItem;
uploadFileListBox: TFileListBox;
downLoadTimer: TTimer;
MILoadBaseData: TMenuItem;
MISendFeedBackInfo: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
MIHelp: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
MIShowLog: TMenuItem;
feedbackTimer: TTimer;
feedbackConfirmTimer: TTimer;
MIFeedBack: TMenuItem;
O1: TMenuItem;
cdsTaskConfirm: THessianDataSet;
cdsTaskConfirmSelected: TBooleanField;
cdsTaskConfirmcreatetime: TDateField;
cdsTaskConfirmfeedbackid: TIntegerField;
cdsTaskConfirmremark: TStringField;
cdsTaskConfirmsign: TIntegerField;
cdsTaskConfirmfromsystemcode: TStringField;
cdsTaskConfirmfromsystemname: TStringField;
cdsTaskConfirmtosystemcode: TStringField;
cdsTaskConfirmtosystemname: TStringField;
pvdProcTask: THessianProvider;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
procedure MIExitClick(Sender: TObject);
procedure A1Click(Sender: TObject);
procedure MIMainFormClick(Sender: TObject);
procedure MIDownLSerSettingClick(Sender: TObject);
procedure MIUploadFileClick(Sender: TObject);
procedure MIDownLoadFileClick(Sender: TObject);
procedure MICancelSendClick(Sender: TObject);
procedure MIUpLSerSettingClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
LogInfo: TLogInfo;
FEventInfo: TEventInfo;
lstFiles: TStrings;
nid: TNotifyIconData;
procedure OnIconNotify(var Message: TMessage); Message MY_MESSAGE;
procedure WMSysCommand(var Message: TWMSysCommand); Message WM_SYSCOMMAND;
procedure ServerStateMessage(var Message: TMessage); Message LWM_ServerIsHaltMessage;
procedure SetFEventInfo(const Value: TEventInfo);
public
{ Public declarations }
property EventInfo: TEventInfo read FEventInfo write SetFEventInfo;
end;

var
frmMain: TfrmMain;
IsCallMe: Boolean = False;
IsMainFormExists: Boolean = False;

implementation

{$R *.dfm}

{ TForm1 }


//当小图标捕捉到鼠标事件时进入此过程
procedure TfrmMain.OnIconNotify(var Message: TMessage);
var
CursorPos: TPoint;
begin
//Exit;
if Message.LParam = WM_RBUTTONDOWN then
begin
//显示主菜单
//Exit;
SetForegroundWindow(Application.Handle);
Application.ProcessMessages;
GetCursorPos(CursorPos);
PopupMenu1.AutoPopup := True;
PopupMenu1.Popup(CursorPos.X , CursorPos.Y);
PostMessage(Self.Handle, WM_NULL, 0, 0);
end else
if Message.LParam = WM_LBUTTONDOWN then
begin
//显示主窗口
FormFactory.FormFactory('mainform');
end;
end;

//当主Form建立时通知Windows加入小图标
procedure TfrmMain.FormCreate(Sender: TObject);
var
sPath: string;
sMsg: String;
begin

Application.ShowMainForm := False;

nid.cbSize := SizeOf(nid); //nid变量的字节数
nid.Wnd := Handle; //主窗口句柄
nid.uID := 0; //内部标识,可设为任意数
nid.hIcon := Application.Icon.Handle; //要加入的图标句柄
nid.szTip := '数据交换组件';
nid.uCallbackMessage := MY_MESSAGE; //回调函数消息
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; //指明那些字段有效
if not Shell_NotifyIcon(NIM_ADD , @nid) then
begin
ShowMessage('Failed');
Application.Terminate;
end;
//将程序窗口样式设为Tool窗口,可避免在任务条上出现
//SetWindowLong(Application.Handle , GWL_EXSTYLE , WS_EX_TOOLWINDOW);
//SetWindowLong(Self.Handle , GWL_EXSTYLE , WS_EX_TOOLWINDOW);
end;


//程序北关闭时通知Windows去掉小图标
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(DownLoadServerStateObject) then
DownLoadServerStateObject.Free;
if Assigned(UpLoadServerStateObject) then
UpLoadServerStateObject.Free;
if Assigned(LogInfo) then
LogInfo.Free;

Action := caHide;

end;

//主窗口初始化完毕并显示时将激活OnPaint重画事件,此时将主窗口隐藏
procedure TfrmMain.FormPaint(Sender: TObject);
begin
if not IsCallMe
then Hide;
end;

procedure TfrmMain.MIExitClick(Sender: TObject);
var
nid: TNotifyIconData;
begin
if MsgBox('确定要退出系统吗?' , '系统提示' , msYN_Q) = mrYes then
begin
nid.cbSize := SizeOf(nid);
nid.uID := 0;
nid.Wnd := Handle;
Shell_NotifyIcon(NIM_DELETE , @nid);
//释放FormFactory
if FormFactory <> nil then
FreeAndNil(FormFactory);

Application.Terminate;
end;
end;

procedure TfrmMain.A1Click(Sender: TObject);
begin
IsCallMe := True;
frmMain.ShowModal;
IsCallMe := False;
end;

procedure TfrmMain.WMSysCommand(var Message: TWMSysCommand);
begin
inherited;
if Message.Msg = WM_SYSCOMMAND then
begin
if Message.CmdType = SC_MINIMIZE
then Self.Hide;
end;
end;

procedure TfrmMain.FormShow(Sender: TObject);
var
hWnd: THandle;
MyMsg: TWMSysCommand;
MyPointer: Pointer;
begin
//FormFactory.FormFactory('mainform');
Exit;
hWnd := FindWindow('TfrmMain' , 'frmMain');
if hWnd <> 0 then
begin
MyMsg.Msg := WM_SYSCOMMAND;
MyMsg.CmdType := SC_MINIMIZE;
MyPointer := @MyMsg;
PostMessage(hWnd , Cardinal(MyPointer) , 0 , 0);
end;
//upLoadTimer.Enabled := False;
end;




procedure TfrmMain.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_Delete , @nid);
end;

end.

当程序启动以后主窗口会自动在任务栏右下角显示(靠近时间的地方),通过主程序提供的菜单调用其他窗口,并把其他窗口最小化时,发现最小化的窗口并不出现在任务栏而是出现在屏幕的左下角,我希望它出现在任务栏,请问这个问题该如何解决?

小弟在此谢谢各位大虾帮助。
 
用RzLib里面的systray控件,或者找一个单独的systray控件吧,这样比较省事,不要将第三方控件当成毒药,适当用几个还是好的
 
谢谢你的提醒。
除了这个方法外还有其他解决方案吗?
 
去掉任务栏
SetWindowLong (Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW)
 
呵呵,我是想让程序的窗口在最小化时显示在任务栏上啊。
 
我自己顶,一定不能沉下来
 
我再顶。
 
我还是顶
 
继续,不要放弃,一定要让高手看到这个问题
 
不是高手,帮顶
 
Sorry,看错你的意思了正确的方法如下:
//重载窗口的CreateParams
procedure CreateParams(var Params:TCreateParams);override;

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle:=WS_EX_APPWINDOW;
end;
 
谢谢两位的帮助。我的QQ是3890637,如果可以请加我为好友,麻烦注明是DELPHI
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
763
SUNSTONE的Delphi笔记
S
I
回复
0
查看
427
import
I
后退
顶部