P
pcexplorer
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我从TForm继承的叫TAppFrm功能是更改系统菜单<br>(主要是Delphi写的程序都有个通病,就是系统菜单和标准<br>的不一样,太丑)<br>你可以发现在任务栏上单击右键弹出的系统菜单和标准的<br>是一样的,但问题来了当你从TAppFrm继承创建一个实例<br>如Form1在Form1上放一个Button,再创建一个实例<br>如Form2,Button的Click中<br>if not assigned(Form2) then<br> Form2:=TForm2.Create(self);<br>try<br> Form2.ShowModal;<br>finally<br> Form2.Free;<br> Form2:=nil;<br>end;<br>你可以首先使程序失去焦点然后在任务栏上单击该程序,问题<br>就出现在这里你发先主窗口Form1盖住了Form2由于是ShowModal<br>所以你的Form1是无法接受键盘和鼠标的,于是我想到了<br>CreateParams中的WndParent,我从TForm继承叫AppChildFrm<br>重载CreateParams.<br>将Params.WndParent:=Application.MainForm.Handle;<br>Modal Window问题解决,但是我又发现问题问题还有<br>当你在Form1上放置一个TOpenDialog用同样的方法发现<br>OpenDialog窗口到主窗口后面去了<br><br>先看看我的自定义类和测试程序<br>我是百思不得其解!!!<br><br>{这是一个封装标准Windows窗口的类,其中TAppFrm窗口类}<br>{是具有标准Windows风格主窗口类,TAppFrm窗口类必须和}<br>{TAppChildFrm窗口类一起使用}<br>unit AppWnd;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br> TAppFrm = class(TForm)<br> procedure CreateParams(var Params:TCreateParams);override;<br> private<br> procedure WMSysCommand(var Msg:TWMSysCommand);message WM_SYSCOMMAND;<br> procedure WMMenuSelect(var Msg:TWMMenuSelect);message WM_MENUSELECT;<br> procedure CreateAppWnd;<br> { Private declarations }<br> public<br> { Public declarations }<br> constructor Create(AOwner: TComponent); override;<br> destructor Destroy; override;<br> end;<br><br>{--------------------------------------------------------------}<br> TAppChildFrm = class(TForm)<br> procedure CreateParams(var Params:TCreateParams);override;<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> constructor Create(AOwner: TComponent); override;<br> destructor Destroy; override;<br> end;<br>{--------------------------------------------------------------}<br><br>implementation<br><br>{ TAppFrm }<br><br>constructor TAppFrm.Create(AOwner: TComponent);<br>begin<br> inherited;<br> CreateAppWnd;<br>end;<br><br>procedure TAppFrm.CreateAppWnd;<br>begin<br> SetWindowLong(Application.Handle,GWL_EXSTYLE,<br> GetWindowLong(Application.Handle,GWL_EXSTYLE) or WS_EX_TOOLWINDOW);<br>end;<br><br>procedure TAppFrm.CreateParams(var Params: TCreateParams);<br>begin<br> inherited CreateParams(Params);<br> Params.ExStyle:=WS_EX_APPWINDOW;<br>end;<br><br>destructor TAppFrm.Destroy;<br>begin<br> inherited;<br> //<br>end;<br><br>procedure TAppFrm.WMMenuSelect(var Msg: TWMMenuSelect);<br>begin<br> inherited;<br> with Application do<br> case Msg.IDItem of<br> SC_MINIMIZE :Hint:='将该窗口缩小为图标。';<br> SC_CLOSE :Hint:='关闭该窗口。';<br> SC_MAXIMIZE :Hint:='将该窗口扩大为全屏幕显示。';<br> SC_SIZE :Hint:='重新调整该窗口的尺寸。';<br> SC_MOVE :Hint:='移动该窗口。';<br> SC_RESTORE :Hint:='将该窗口恢复为正常大小。'<br> else<br> if Msg.MenuFlag = MF_SYSMENU then<br> Hint:='';<br> end;<br>end;<br><br>procedure TAppFrm.WMSysCommand(var Msg: TWMSysCommand);<br>begin<br> if Msg.CmdType=SC_MINIMIZE then<br> WindowState:=wsMinimized<br> else<br> inherited;<br>end;<br><br>{--------------------------------------------------------------}<br><br>{ TAppChildFrm }<br><br>constructor TAppChildFrm.Create(AOwner: TComponent);<br>begin<br> inherited;<br> //<br>end;<br><br>procedure TAppChildFrm.CreateParams(var Params:TCreateParams);<br>begin<br> inherited CreateParams(Params);<br> Params.WndParent:=Application.MainForm.Handle;<br>end;<br><br>destructor TAppChildFrm.Destroy;<br>begin<br> inherited;<br> //<br>end;<br><br>end.<br><br>{----------------------以下是我的测试程序-------------------------------}<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if opendialog1.Execute then<br> begin<br> //<br> end;<br>end;<br><br>{在Form2中别忘了要引用我的AppWnd单元}<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> if not Assigned(Form2) then<br> Form2:=TForm2.Create(self);//从我的AppChildFrm继承<br> try<br> Form2.ShowModal;<br> finally<br> Form2.Free;<br> Form2:=nil;<br> end;<br>end;<br><br>{不引用我的AppWnd单元}<br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> if not Assigned(Form3) then<br> Form3:=TForm3.Create(self);//直接从TForm创建<br> try<br> Form3.ShowModal;<br> finally<br> Form3.Free;<br> Form3:=nil;<br> end;<br>end;<br><br>{千万要试了再发言,不要灌水,如果你是低手请你不要发言}