拥有的表单始终显示在其所有者表单的顶部。要使表单归所有者所有,可以将所有者表单的引用分配给所拥有表单的Onwer属性,例如:
var f = new Form();
f.Owner = this;
f.Show();
Run Code Online (Sandbox Code Playgroud)
将另一个进程的窗口设置为所有者
为此,您应该首先找到另一个进程的窗口句柄,然后使用SetWindowLongAPI函数将其设置为表单的所有者,例如:
//using System.Runtime.InteropServices;
//using System.Diagnostics;
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
private void button1_Click(object sender, EventArgs e)
{
var notepad = Process.GetProcessesByName("notepad").FirstOrDefault();
if(notepad!=null)
{
var owner = notepad.MainWindowHandle;
var owned = this.Handle;
var i = SetWindowLong(owned, -8 /*GWL_HWNDPARENT*/, owner);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您的表单将始终位于记事本窗口的顶部。
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |