首页 > 代码库 > .Net(WindowForm 和WPF) 常用的API函数

.Net(WindowForm 和WPF) 常用的API函数

未完成

 

1、子窗口(Child Window)

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窗体

2、弹出窗口(Popup Window)

和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);}

3、活动窗体(Active Window)

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

 

4、窗体样式(Window Style)

5、重载Window样式(CreateParams

6、重载Window消息(WndProc)

7、无标题栏窗体移动(HTCLIENT

8、注册全局热键(Global HotKey)

 

.Net(WindowForm 和WPF) 常用的API函数