使用User32激活窗体

//找到窗体

[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 ); 


IntPtr hWnd1 = FindWindow(null, "主窗口标题");                        
IntPtr hWnd2 = FindWindowEx(hWnd1, IntPtr.Zero, null, "控件文本");  
IntPtr hWnd3 = FindWindowEx(hWnd1, IntPtr.Zero, "控件类名", null);  

IntPtr h= FindWindow ( null, "" ); 
if ( h!= IntPtr.Zero ) 

IntPtr h2= FindWindowEx(h, 0, null, "3" ); 
IntPtr ParenthWnd = FindWindow(null, ""); 

private void button1_Click(object sender, EventArgs e)
{
int hwnd = GetActiveWindow();
Form2 F2 = new Form2();
F2.StartPosition = FormStartPosition.Manual;
F2.Show();
IntPtr I = new IntPtr(hwnd);
SetActiveWindow(I);

}

//激活窗体
[DllImport("user32.dll搜索")]
public static extern int GetActiveWindow();

[DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hwnd);

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

你可能感兴趣的:(使用User32激活窗体)