首页  编辑  

运行外部程序后使其主窗体居顶

Tags: /C#/进程、线程处理/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "user32.dll" )]

public static extern bool SetWindowPos( IntPtr hWnd,

    IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);        

public IntPtr HWND_TOPMOST = new IntPtr ( - 1 );

public uint SWP_NOMOVE = 2 ;

public uint SWP_NOSIZE = 1 ;

public uint SWP_NOACTIVATE = 0x10 ;

private void button1_Click( object sender , EventArgs e)

{

    Process vProcess = Process . Start( "notepad.exe" );

    while (vProcess . MainWindowHandle == IntPtr . Zero) vProcess . Refresh();

   SetWindowPos(vProcess . MainWindowHandle, HWND_TOPMOST, 0 , 0 , 0 , 0 ,

       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

}