c#實現Form窗體始終在桌面最前端顯示

方法一this

//調用APIorm

[System.Runtime.InteropServices.DllImport("user32", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]事件

public static extern IntPtr GetFocus(); //得到本窗體的句柄string

[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetForegroundWindow")]變量

public static extern bool SetFocus(IntPtr hWnd);//設置此窗體爲活動窗體object


// 定義變量,句柄類型定時器

public IntPtr han;方法

private void Form1_Load(object sender, EventArgs e){im

//在窗體加載的時候給變量賦值,即將當前窗體的句柄賦給變量static

han = this.Handle;

}
private void timer1_Tick(object sender, EventArgs e){

// 加載一個定時器控件,驗證當前WINDOWS句柄是否和本窗體的句柄同樣,若是不同,則激活本窗體

if (han != GetFocus()){

SetFocus(han);

}

this.WindowState = FormWindowState.Normal;

}

 

方法二

[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr GetActiveWindow();//獲取當前窗體的活動狀態

// 判斷當前窗口是否處於活動狀態的方法

private bool ThisIsActive(){ return (GetActiveWindow() == this.Handle);}

private void timer1_Tick(object sender, EventArgs e){

if (!ThisIsActive()){

this.Activate();

}

this.WindowState = FormWindowState.Normal;

}

 

方法三

 

[DllImport("user32")] private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); [DllImport("user32")] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //在窗體On_Load事件中添加(Santos的代碼): IntPtr hDeskTop=FindWindow("Progman", "Program Manager"); SetParent(this.Handle,hDeskTop);

相關文章
相關標籤/搜索