标签:style blog ar color os sp for on div
WPF 用法:
Window2 wnd = new Window2();
wnd.Show();
wnd.Owner = this; // 设置子窗体的父窗体为当前窗体
c#用法
API用法:
// 添加API引用 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")] public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr); // 实例化子窗体 Window2 wnd = new Window2(); wnd.Show(); IntPtr parentHandle = new WindowInteropHelper(this).Handle; // 获得该窗体的句柄 IntPtr childHandle = new WindowInteropHelper(wnd).Handle; SetParent(childHandle, parentHandle); // 设置MDI窗体
和TopMost的区别是,TopMost是将窗体始终置于显示的前端,而弹出窗口是不会获得焦点。例如:Window自带的键盘。
C#
WPF
private const int GWL_STYLE = -16; // GWL_STYLE 指的是那些旧的窗口属性。相对于GWL_EXSTYLEGWL扩展属性而言的 private const long WS_CHILD = 0x40000000L; // Popup风格 [DllImport("user32.dll")] private extern static int SetWindowLong(IntPtr hwnd, int oldStyle, long newStyle); //newStyle采用long类型有时候会报错,建议采用int类型 [DllImport("user32.dll")] private extern static int GetWindowLong(IntPtr hwnd, int oldStyle); private void Window_Loaded_1(object sender, RoutedEventArgs e) { // 注意:该段代码不能写在构造函数中,因为窗体未加载,没有句柄 IntPtr handle = new WindowInteropHelper(this).Handle; int style = GetWindowLong(handle, GWL_STYLE); SetWindowLong(handle, GWL_STYLE, style | WS_CHILD); }
WPF
Window activeWindow=App.Current.Windows.OfType<Window>().SingleOrDefault(x=>x.IsActive)
API
// 获得活动窗体的句柄 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private extern IntPtr GetForegroundWindow(); // 获得根据窗体句柄窗体的名称 [DllImport("User32.dll")] private extern int GetWindowText(IntPtr handle, StringBuilder text, int maxLen); StringBuilder text = new StringBuilder(512); // 定义接受窗体名称的变量 IntPtr myPtr = GetForegroundWindow(); int i = GetWindowText(myPtr, text, 512);
.Net(WindowForm 和WPF) 常用的API函数
标签:style blog ar color os sp for on div
原文地址:http://www.cnblogs.com/lovewyw/p/4166080.html