帮我看看以下程序哪里出错了(50分)

  • 主题发起人 主题发起人 小唐
  • 开始时间 开始时间

小唐

Unregistered / Unconfirmed
GUEST, unregistred user!
program Project1;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; Windows,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.RES}<br><br>var<br>&nbsp; hMutex : Thandle;<br>&nbsp; WaitResult : word;<br>&nbsp; BroadcastList : DWORD;<br>&nbsp; MessageID : DWORD;<br><br>begin<br>&nbsp; &nbsp; &nbsp;MessageID := RegisterWindowMessage('Check For Choice Previous Inst');<br>&nbsp; &nbsp; &nbsp;hMutex := createMutex(nil,false,pchar('App_Choice'));<br>&nbsp; &nbsp; &nbsp;WaitResult := WaitForSingleObject(hMutex,10);<br>&nbsp; &nbsp; &nbsp;if (waitResult=WAIT_TIMEOUT) then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BroadcastList :=BSM_APPLICATIONS;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.Title := 'Choice Organics Purchase &amp; Sales System';<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.Initialize;<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.Run;<br>&nbsp; &nbsp; &nbsp; &nbsp; ReleaseMutex(hMutex);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseHandle(hMutex);<br>end.<br>{<br><br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.}<br><br>///////////////////////////////////////////////////////////////<br>///////////////////////////////////////////////////////////////<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>// &nbsp; &nbsp;MessageID : DWORD;<br>&nbsp; &nbsp; procedure OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);<br>begin<br>if Msg.Message =MessageID then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; show;<br>&nbsp; &nbsp; &nbsp; WindowState := wsMaximized;<br>&nbsp; &nbsp; &nbsp; BringToFront;<br>&nbsp; &nbsp; &nbsp; SetFocus;<br>&nbsp; &nbsp; &nbsp; Handled := true;<br>&nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Application.OnMessage:= OnAppMessage;<br>end;<br><br><br>end.<br><br>程序能够运行,但是达不到我要的效果,我想达到像winamp那样的效果:<br>当程序被最小化时,再次双击桌面上的图标时,就会将最小化的程序激活并恢复成原样。
 
不懂,GZ。。。
 
下面是完美的解决方案<br>始终保持一个实例,不论是最小化,还是被别的窗口挡住,还是隐藏,都能弹到最前端。<br><br>//工程文件单元<br>program Project1;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; Windows,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1},<br>&nbsp; Unit2 in 'Unit2.pas';<br><br>{$R *.RES}<br><br>var<br>&nbsp; UniqueMutex: HWND;<br><br>begin<br>&nbsp; { 用于注册新消息和建立互斥量的字符串要尽量写的长一些,特殊一些,以确保唯一 }<br>&nbsp; UniqueMsgID := RegisterWindowMessage('aaaaaaaaaa');<br>&nbsp; UniqueMutex := CreateMutex(nil, false, PChar('bbbbbbbbbb'));<br>&nbsp; if GetLastError &lt;&gt; ERROR_ALREADY_EXISTS then<br>&nbsp; begin<br>&nbsp; &nbsp; Application.Initialize;<br>&nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; Application.Run;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; SendMessage(HWND_BROADCAST, UniqueMsgID, 0, 0); //此处不可用PostMessage<br>&nbsp; &nbsp; ReleaseMutex(UniqueMutex);<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>end.<br><br>//主窗口单元<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<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 WndProc(var Msg: TMessage); override;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.DFM}<br><br>{ TForm1 }<br><br>procedure TForm1.WndProc(var Msg: TMessage);<br>begin<br>&nbsp; if Msg.Msg = UniqueMsgID then<br>&nbsp; begin<br>&nbsp; &nbsp; if IsIconic(Application.Handle) then //如果程序已经是最小化,则...<br>&nbsp; &nbsp; &nbsp; Application.Restore<br>&nbsp; &nbsp; else //否则,不论是被别的窗口挡住还是隐藏起来了,都...<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Show;<br>&nbsp; &nbsp; &nbsp; ShowWindow(Application.Handle, SW_SHOW); //在没有拖放窗口的时候,这两句代码可以不要<br>&nbsp; &nbsp; &nbsp; SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_APPWINDOW);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetForegroundWindow(Application.Handle);<br>&nbsp; end;<br>&nbsp; inherited WndProc(Msg);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Hide;<br>end;<br><br>end.<br><br>//公共单元(没有窗体)<br>unit Unit2;<br><br>interface<br><br>var<br>&nbsp; UniqueMsgID: Cardinal; //用来存储自定义消息的常量标识符。放在这里,都可以访问得到。<br><br>implementation<br><br>end.<br>
 
非常感谢小笨笨的热心帮助!
 
经试验,将WndProc改成如下才可将窗口调用到最前,<br>BringWindowToTop和SetForegroudWindow等都不起作用,不知为何?<br>///////////////////////////////////////////////////////////////////<br>procedure TForm1.WndProc(var Msg: TMessage);<br>begin<br>&nbsp; if Msg.Msg = UniqueMsgID then<br>&nbsp; begin<br>&nbsp; &nbsp; if IsIconic(Application.Handle) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.Restore;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else begin &nbsp;//暂时不考虑hide的情况<br>&nbsp; &nbsp; &nbsp; Application.Minimize;<br>&nbsp; &nbsp; &nbsp; Application.Restore;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; inherited WndProc(Msg);<br>end;
 
????有什么问题吗?
 
后退
顶部