程序怎样得到系统焦点(50分)

  • 主题发起人 主题发起人 trancechow
  • 开始时间 开始时间
T

trancechow

Unregistered / Unconfirmed
GUEST, unregistred user!
点击系统托盘,使程序到最前端,并取得系统焦点<br>SetWindowPos不行,不能得到系统的焦点<br>上次好像在离线数据库里面找到过,现在找不到了
 
Application.BringToFront;
 
Application.BringToFront不能使程序得到系统焦点。我找到答案了,再问个问题,按任务栏使程序最小化的消息怎么截获,好像和按窗口的最小化按钮不是一个消息,用SC_MINIMIZE消息截获不了
 
其实我想做这样一件事情,就是当用户按下窗口最小化按钮,或者任务栏按钮或者系统的显示桌面按钮时,程序能从任务栏上隐藏,像flashget一样,怎么做
 
天哪,这个问题很难吗?
 
type<br>&nbsp; TFormCommServer = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; miRestore: TMenuItem;<br>&nbsp; &nbsp; miAbout: TMenuItem;<br>...<br>&nbsp; private<br>&nbsp; &nbsp; nid: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NOTIFYICONDATA;<br>&nbsp; &nbsp; OldWindowState: &nbsp;TWindowState;<br><br>...<br>&nbsp; &nbsp; procedure AppMinimize(Sender: TObject);<br>&nbsp; &nbsp; procedure ShowAppMainForm;<br>&nbsp; &nbsp; procedure HideAppMainForm;<br>&nbsp; &nbsp; procedure AddTrayIcon;<br>&nbsp; &nbsp; procedure RemoveTrayIcon;<br>&nbsp; &nbsp; procedure WndProcTrayIcon(var Message: TMessage);message WM_TRAYICON;<br>...<br>&nbsp; end;<br><br>procedure TFormCommServer.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; RemoveTrayIcon;<br>end;<br><br>procedure TFormCommServer.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Application.OnMinimize:= AppMinimize;<br>&nbsp; Application.Minimize;<br>&nbsp; AddTrayIcon;<br>end;<br><br>procedure TFormCommServer.AppMinimize(Sender: TObject);<br>begin<br>&nbsp; OldWindowState := WindowState;//mem it<br>&nbsp; ShowWindow(Application.Handle, SW_HIDE);<br>&nbsp; Hide;<br>end;<br><br>procedure TFormCommServer.ShowAppMainForm;<br>begin<br>&nbsp; ShowWindow(Application.Handle,SW_SHOW);<br>&nbsp; Application.ShowMainForm := True;<br>&nbsp; WindowState := OldWindowState;//restore it with old mem one//wsNormal;<br>&nbsp; Application.Restore;<br>&nbsp; Show;<br>end;<br><br>procedure TFormCommServer.HideAppMainForm;<br>begin<br>&nbsp; Application.ShowMainForm := False;<br>&nbsp; Application.Minimize;<br>end;<br><br>procedure TFormCommServer.AddTrayIcon;<br>begin<br>&nbsp; nid.cbSize := sizeof(NOTIFYICONDATA);<br>&nbsp; nid.uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;<br>&nbsp; nid.hIcon &nbsp;:= Application.Icon.Handle;<br>&nbsp; Move(PChar(Application.Title)^,nid.szTip,64);<br>&nbsp; nid.uCallbackMessage := WM_TRAYICON;<br>&nbsp; if nid.Wnd = Handle then<br>&nbsp; &nbsp; Shell_NotifyIcon(NIM_MODIFY, @nid)<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; nid.Wnd := Handle;<br>&nbsp; &nbsp; Shell_NotifyIcon(NIM_ADD, @nid);<br>&nbsp; end;<br>end;<br><br>procedure TFormCommServer.RemoveTrayIcon;<br>begin<br>&nbsp; if nid.Wnd &lt;&gt; 0 then<br>&nbsp; &nbsp; Shell_NotifyIcon(NIM_DELETE, @nid);<br>&nbsp; nid.Wnd := 0;<br>end;<br><br>procedure TFormCommServer.WndProcTrayIcon(var Message: TMessage);<br>var<br>&nbsp; MousePos: TPoint;<br>begin<br>&nbsp; if (Message.Msg = WM_TRAYICON) then<br>&nbsp; begin<br>&nbsp; &nbsp; if Message.LParam = WM_LBUTTONDBLCLK then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if not Visible then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Assigned(FormAbout) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not FormAbout.Showing then ShowAppMainForm<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else FormAbout.BringToFront;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowAppMainForm;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Assigned(FormAbout) and FormAbout.Showing then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormAbout.BringToFront;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; HideAppMainForm;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else if Message.LParam = WM_RBUTTONUP then//WM_RBUTTONDOWN then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; GetCursorPos(MousePos);<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow(Application.Handle);<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; PopupMenu1.Popup(MousePos.X,MousePos.Y);<br>&nbsp; &nbsp; end<br>&nbsp; end<br>end;<br><br><br>procedure TFormCommServer.miRestoreClick(Sender: TObject);<br>begin<br>&nbsp; if Assigned(FormAbout) then<br>&nbsp; &nbsp; if not FormAbout.Showing then ShowAppMainForm<br>&nbsp; &nbsp; else FormAbout.BringToFront;<br>end;<br><br>procedure TFormCommServer.miAboutClick(Sender: TObject);<br>begin<br>&nbsp; if Assigned(FormAbout) and not FormAbout.Showing then<br>&nbsp; &nbsp; FormAbout.ShowModal;<br>end;<br><br><br><br><br><br>DPR:<br>&nbsp; &nbsp; Application.Initialize;<br><br>&nbsp; &nbsp; ShowWindow(Application.Handle,SW_HIDE);<br>&nbsp; &nbsp; Application.ShowMainForm := False;<br><br>&nbsp; &nbsp; Application.CreateForm(TFormCommServer, FormCommServer);<br>&nbsp; &nbsp; Application.CreateForm(TFormCommSetting, FormCommSetting);<br>&nbsp; &nbsp; Application.CreateForm(TFormAbout, FormAbout);<br>&nbsp; &nbsp; Application.Run;<br><br>看看无防:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2325040
 
不行啊,按显示桌面,程序并没有在任务栏上消失啊,按显示桌面的按钮程序并没有被最小化,所以接管最小化事件并不起作用
 
捕获最小化消息很简单。<br>在Additional页找到ApplicationEvents控件,添加一个到表单中,然后在其事件OnMinimize事件中写下代码就可以了<br>procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);<br>begin<br>&nbsp; &nbsp; //供参考<br>&nbsp; &nbsp;showwindow(application.handle,sw_handle);<br>&nbsp; &nbsp;//其它操作<br>end;
 
拜托,显示桌面后程序并没有处在最小化状态啊,根本不会触发OnMinimize事件!!!
 
你要的就是这个<br>没错的<br><br><br>{uses中加入 shellapi<br>File-&gt;New Applicaton 在Interface部分定义一个消息常量:const WM_NID=WM_USER+1000; 系统规定从WM_USER开始为用户自定义消息。<br>定义一个全局变量: NotifyIcon:TNotifyIconData,NotifyIcon是非常重要的一个变量,整个程序基本上是围着这个变量在转。TNotifyIconData是一个记录类型,按住Ctrl键,在TNotifyIconData 双击即进入ShellAPI.pas单元。(注:在Delphi中,这是一个非常好的对源代码进行分析的方法,源代码说明一切,你要想知道程序背后的内幕,最好的方法就是分析源代码!)此时出现了以下赋值语句:<br>  TNotifyIconData = TNotifyIconDataA,这个意思很明显,就是说TNotifyIconData和TNotifyIconDataA是同种数据类型,接着往下看有:<br>TNotifyIconDataA = _NOTIFYICONDATAA,意思与刚才的一样,再往下看:<br>type<br>_NOTIFYICONDATAA = record<br>cbSize: DWORD;<br>Wnd: HWND;<br>uID: UINT;<br>uFlags: UINT;<br>uCallbackMessage: UINT;<br>hIcon: HICON;<br>szTip: array [0..63] of AnsiChar;<br>end;<br>  这可真是“千呼万唤始出来,犹抱琵琶半遮面”。现在大家很清楚了,我们刚才定义的全局变量NotifyIcon其实是一个包含有7个成分的记录类型变量,就相当于C/C++中的结构体变量(C/C++的程序员应该是再熟悉不过了)。下面我们逐个来解释记录类型中的7个部分各有什么功能。<br>  1&gt; cbSize就是你定义的NotifyIcon变量的大小,用SizeOf(TNotifyIconData)可以取得,如果你是一个熟练的C/C++程序员,你应该不会陌生。在C/C++中,每当要为一个结构体变量分配内存的时候都要:通过 SizeOf(Struct type) 来获知存放一个这样的结构体变量要多少内存。<br>  2&gt; Wnd是一个句柄,你希望托盘程序产生的消息有哪个窗体来处理就让Wnd指向那个窗体。<br>  例如:你准备在任务栏的托盘小图标上单击时窗体是窗体在“显示”和“隐藏”之间切换,则把Wnd指向主窗体。<br>&nbsp;  3&gt; uID:如果你要创建多个托盘小程序,那么怎么区分它们呢?就是靠这个ID号来区分。<br>&nbsp;  4&gt; uFlags是一个标志位,它表示当前所创建的托盘程序具有哪些性质:<br>&nbsp;   NIF_ICON 表示当前所设置的图标(即hIcon的值)是有效的<br> NIF_MESSAGE 表示当前所设置的系统消息(即uCallBackMessage的值)是有效的<br>  NIF_TIP 表示当前所设置的提示条(即szTip的值)是有效的。<br> 5&gt; uCallBackMessage这是7个部分里面最重要的一个。这里指定一个回调消息,也就是说这里定义一个消息名,当你单击或者右击托盘图标的时候就会向你在Wnd所指向的窗体发送一个在uCallBackMessage中定义的消息名,然后你在程序中定义一个消息出来函数来处理这个消息。这样就把Windows关于消息的整套流程都处理好了。<br> 6&gt; hIcon为托盘图标的句柄,根据这个句柄你就可以增加、修改、删除图标。<br>&nbsp; 7&gt; szTip就是当你的鼠标放到任务栏托盘的小图标上的时候弹出来的提示信息。}<br>unit Unit1;<br><br>interface<br><br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,shellapi, StdCtrls, Menus;<br>&nbsp; const<br>WM_NID=WM_USER+1000; //定义一个消息常量 ,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //系统规定从WM_USER开始为用户自定义消息<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; procedure installicon;<br>&nbsp; &nbsp; procedure N1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br><br>&nbsp; private<br>procedure IconOnClick(var message:TMessage); message WM_NID;<br>//自定义mouse的click事件<br>procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;<br>//截获 WM_SYSCOMMAND消息,看窗体是否处于最小化状态<br>&nbsp; public<br><br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; NotifyIcon:TNotifyIconData;//NotifyIcon是非常重要的一个变量,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //整个程序基本上是围着这个变量在转。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//TNotifyIconData是一个记录类型<br><br>implementation<br><br>{$R *.dfm}<br>procedure TForm1.WMSysCommand(var Message:TMessage);<br>begin<br>if Message.WParam = SC_ICON then &nbsp; &nbsp; &nbsp; //最小化了<br>begin<br>form1.hide;<br>installicon;<br>end<br>else<br>inherited;<br>end;<br><br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>Shell_NotifyIcon(NIM_DELETE,@NotifyIcon); &nbsp;//从任务栏删除图标<br>end;<br><br><br>procedure TForm1.installicon;<br>begin<br>application.ShowMainForm:=false;<br>//SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);<br>//上面的函数可以和application一样用。但是只隐藏任务栏<br>//NotifyIcon为全局变量,在程序的开头已经定义了<br>with NotifyIcon do<br>begin<br>cbSize:=SizeOf(TNotifyIconData);<br>Wnd:=Handle; //指向当前窗体Form1的句柄<br>uID:=1;<br>uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;<br>uCallBackMessage:=WM_NID;<br>hIcon:=Application.Icon.Handle;<br>szTip:='任务栏演示程序';<br>end;<br>//把设置好的变量NotifyIcon加入到系统中以便处理<br>Shell_NotifyIcon(NIM_ADD,@NotifyIcon);<br>end;<br><br><br>procedure TForm1.IconOnClick(var message:TMessage); <br>//处理鼠标在指示状态栏对应的图标上的单击事件 <br>var <br>&nbsp; p:TPoint; <br>begin <br>&nbsp; //如果按下的是鼠标左键,并且允许显示,则显示Form2 <br>&nbsp; if message.lParam=WM_LBUTTONDOWN then<br>&nbsp; &nbsp; Form1.Show;<br>&nbsp; //如果按下的是鼠标右键,则显示弹出菜单 <br>&nbsp; if(message.lParam=WM_RBUTTONDOWN)then <br>&nbsp; begin<br><br>&nbsp; &nbsp; GetCursorPos(p);<br>&nbsp; &nbsp; PopupMenu1.Popup(p.x,p.y);<br>&nbsp; end; <br>end;<br><br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br>form1.Show;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br>showwindow(application.Handle,SW_SHOWNA);<br>Shell_NotifyIcon(NIM_DELETE,@NotifyIcon);<br>end;<br><br>end.<br>&nbsp;<br><br>+分~
 
我真的想哭,请大家仔细看看我的问题,显示桌面后程序并没有处在最小化状态啊,根本收不到最小化的消息!!!
 

Similar threads

回复
0
查看
709
不得闲
S
回复
0
查看
751
SUNSTONE的Delphi笔记
S
S
回复
0
查看
758
SUNSTONE的Delphi笔记
S
后退
顶部