怎样将除本窗体之外的所有windows窗体最小化?(50分)

  • 主题发起人 主题发起人 yanyuwuhen
  • 开始时间 开始时间
Y

yanyuwuhen

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; 我是这样做的:<br>&nbsp; &nbsp; &nbsp; &nbsp;1.获得所有窗体句柄<br>&nbsp; &nbsp; &nbsp; &nbsp;2.postmessage()<br>&nbsp; &nbsp; &nbsp; &nbsp;3.在自身窗体中截获WM_SYSCOMMAND消息,处理最小化事件。<br>&nbsp; &nbsp;1.2实现正常,所有的窗体都最小化了,而且3中也接收到了的最消化消息,但我的窗体<br>还是被最小化了!请各位高手帮帮忙,谢谢!<br>//*************************************************************************<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; CheckBox1: TCheckBox;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp;procedure WMSysCommand(var Msg: TMessage);message WM_SYSCOMMAND;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; flagMin:boolean;<br>implementation<br><br>{$R *.dfm}<br>procedure MinAllForms;<br>var<br>&nbsp; hw:HWnd;<br>begin<br>&nbsp; hw:=Form1.handle;<br>&nbsp; while hw&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if IsWindowVisible(hw) then<br>&nbsp; &nbsp; Postmessage(hw,WM_SYSCOMMAND,SC_MINIMIZE,0);<br>&nbsp; &nbsp; hw:=GetnextWindow(hw,GW_HWNDNEXT);<br>&nbsp; end;<br>end;<br>procedure TForm1.WMSysCommand(var Msg: TMessage);<br>begin<br>&nbsp; case Msg.WParam of<br>&nbsp; &nbsp; SC_MINIMIZE: showmessage('Min');<br>&nbsp; &nbsp; else inherited;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;MinAllForms;<br>end;<br><br>end.<br>&nbsp;
 
根本没有处理呀!<br>procedure TForm1.WMSysCommand(var Msg: TMessage);<br>begin<br>&nbsp; case Msg.WParam of<br>&nbsp; &nbsp; SC_MINIMIZE: <br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; showmessage('Min');<br>&nbsp; &nbsp; msg:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; else inherited;<br>&nbsp; end;<br>end;
 
别发给自己窗口不就得了?<br>procedure MinAllForms;<br>var<br>&nbsp; hw:HWnd;<br>begin<br>&nbsp; hw:=Form1.handle;<br>&nbsp; while hw&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if IsWindowVisible(hw) [red]and (hw &lt;&gt;Form1.Handle)[/red] then<br>&nbsp; &nbsp; Postmessage(hw,WM_SYSCOMMAND,SC_MINIMIZE,0);<br>&nbsp; &nbsp; hw:=GetnextWindow(hw,GW_HWNDNEXT);<br>&nbsp; end;<br>end;<br>
 
to zhukewen<br>&nbsp; 我试过你的这种方法,但是仍然被最小化了。<br>to 天真<br>&nbsp; 我不太清楚消息的机制,如果自身定义的消息不处理不行么(相当于接到最小化消息之后什么也不做)? <br>&nbsp; 而且我加过msg.result:=-1;也不行;<br>&nbsp; postmessage是不等待返回的。
 
方法不是很好,但能解决问题。<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; CheckBox1: TCheckBox;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; ax,ay,bx,by:integer;<br>&nbsp; &nbsp; flagMin:boolean;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; procedure WMSysCommand(var Msg: TMessage);message WM_SYSCOMMAND;<br>&nbsp; &nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>{ TForm1 }<br>procedure MinAllForms;<br>var<br>&nbsp; hw:HWnd;<br>begin<br>&nbsp; hw:=Form1.handle;<br>&nbsp; while hw&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if IsWindowVisible(hw) then<br>&nbsp; &nbsp; &nbsp; Postmessage(hw,WM_SYSCOMMAND,SC_MINIMIZE,0);<br>&nbsp; &nbsp; hw:=GetnextWindow(hw,GW_HWNDNEXT);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.WMSysCommand(var Msg: TMessage);<br>begin<br>&nbsp; case Msg.WParam of<br>&nbsp; &nbsp; SC_MINIMIZE: ;//showmessage('Min');<br>&nbsp; &nbsp; else inherited;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; ax := form1.Left;<br>&nbsp; ay := form1.Top ;<br>&nbsp; bx := form1.Width;<br>&nbsp; by := form1.Height;<br>&nbsp; MinAllForms;<br>&nbsp; Timer1.Enabled := True;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>&nbsp; Timer1.Enabled := False;<br>&nbsp; SETWINDOWPOS(Form1.handle,HWND_TOP,ax,ay,bx,by,SWP_SHOWWINDOW);<br>end;<br><br>end.
 
谢谢sun77wind,这是一种办法,但是能不能从截获最小化消息的方法来做?
 
//建立事件<br>procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);<br>begin<br>&nbsp; Case Msg.message of<br>&nbsp; &nbsp; WM_SYSCOMMAND:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Msg.wParam=SC_MINIMIZE then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Handled:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//在Form1的OnCreate里:<br>Application.OnMessage:=AppOnMessage;<br>//ok[:D]
 
to pcexplorer<br>&nbsp; 我将appOnmessage付给Application.OnMessage时出错?<br>&nbsp; [Error] Unit1.pas(92): Incompatible types: 'method pointer and regular procedure'<br>&nbsp; 我最终的目的不仅是最小化自身之外的窗体,而且要做到可重复操作这一步;sun77wind的方<br>法中第一次可以,但下一次就把自己也最小化了---我再试试,也可能是我程序中的其它部分的<br>问题,昨天时间仓促,仅看了一下效果;<br>&nbsp; 谢谢大家的指点,还请继续支招!
 
sorry, sun77wind 的方法没有问题,是我自己的原因;<br>希望还能得到其他方法。
 
1。BroadcastSystemMessage WM_Syscommand to BSM_ALLCOMPONENTS,<br>这样就不需要一个一个的发了<br><br>2。WM_SysCommand 当 uCmdType = SC_MINIMIZE 时, wParam 没有使用,你可以设为 1<br><br>3。自己的窗口中响应 WM_SysCommand,如果 uCmdType = MINIMIZE , wParam 为 1 时<br>就不最小化
 
我有一个很好的方法,而且我已经实验过了:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, ExtCtrls, Buttons, Menus, ToolWin, ComCtrls, ImgList,<br>&nbsp; OleServer, Shell32_TLB;<br><br>type<br>&nbsp; &nbsp; &nbsp;TMainForm = class(TForm);<br>&nbsp; &nbsp; &nbsp;Button1: TButton;<br>end;<br>function MinimizeWindow(handle:hwnd;lparam:lparam):bool;stdcall;<br>implementation<br><br>procedure TMainForm.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;EnumWindows(@MiniMizeWindow,0);//列出所有窗体<br>end;<br>function MinimizeWindow(handle:hwnd;lparam:lparam):bool;<br>var<br>&nbsp; &nbsp; WindowTitle:array[0..255] of char;<br>begin<br>&nbsp; &nbsp; &nbsp;if IsWindowVisible(handle) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(handle,WindowTitle,255);//获取窗体的标题<br>&nbsp; &nbsp; &nbsp; &nbsp; if WindowTitle&lt;&gt;'你的窗体的标题' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PostMessage(handle,wm_syscommand,sc_minimize,0);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;result:=true;<br>end;<br>别忘了给分啊!
 
如果采用不给本窗体发送消息的办法(zhukewen,luan_sheng 用的都是这种方法),只是<br>判断不是form的handle和caption(一回事)是不行的,应为最小化消息还会发到project,<br>一样会将本窗体最小化的!这样就还需判断不是自身的project;<br>&nbsp; &nbsp;[?]我使用GetnextWindow(hw,GW_HWNDNEXT)中发现form1.handle的下一个句柄就是project的,<br>请问这种规律是固定的么?(我这里都是这样)<br>&nbsp; &nbsp;当然也可以判断一下是否是project的句柄,两种方法和判断form的一样;<br>这种不给本身发送消息的办法基本是完成了,发过消息在恢复的方法也可行;还想听听自身<br>截获最小化消息的方法,940801能具体谈一下么?同时也欢迎大家提出其它的方法!(贪得<br>无厌了一些,而且我给的分也不多,但我想,大家在这里关键是探讨问题,增进知识,并且<br>消息的应用场合是很多的,解决这个问题的方法也具有一定的普遍性,还请大家多多指点;<br>高手不要骂我呦,小弟笨了些。<br><br>&nbsp;<br>&nbsp; &nbsp;
 
yanyuwuhen:<br>有一点忘了说明:<br>如果你把Application的Title和主窗体的Caption设置为同一个值,应用程序就不会关闭了!<br>
 
我还想知道一下用截获消息解决这个问题的方法;<br>sun77wind 有你16分,luan_sheng有你14分;一起发;<br>如果过一段时间的仍无法得到第三种方法,就将所有的分发给你们两个。分不多,略表心意。
 
谢谢大家的帮助。
 
后退
顶部