日常中,我们可能关心某应用的运行结果,编写这个小程序目的就是设定时间周期性将某应用的运行呈现在桌面的最上方,以便我们显看。本程序呈现的是中金财富金融终端运行。
当不清楚某应用程序的接口名称时,可以使用VS中的spy++进行查看。如下图。
当找到parent window的caption后,可以使用api中的findwindow函数调用这个应用程序。
具体见下面代码:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
private void timer1_Tick(object sender, EventArgs e)
{
ii++;
if (ii % 2 == 0)
{
this.TopMost = true;
this.Show();
}
else
{
this.TopMost = false;
this.Hide();
IntPtr mainHandle = FindWindow(null, "中金财富金融终端V9.02 - [版面-自选股]");
//FindWindow("Afx:00400000:3:00010003:00000010:00000000", "中金财富金融终端V9.02");
if (mainHandle != null)
{
SetForegroundWindow(mainHandle);
SendMessage(mainHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0); // 最大化
}
}
}
源码:资源下载