WPF Win32 API 嵌入Form 窗体
WIn32 API:
public class Win32Native
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint GetWindowLong(IntPtr hwnd, int nIndex); [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]
public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong); public const UInt32 WS_POPUP = 0x80000000; //assorted constants needed public static int GWL_STYLE = -; public static int WS_CHILD = 0x40000000; //child window public static int WS_BORDER = 0x00800000; //window with border public static int WS_DLGFRAME = 0x00400000; //window with double border but no title public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar public const UInt32 WS_THICKFRAME = 0x40000; public const UInt32 WS_SIZEBOX = WS_THICKFRAME;
}
public class EmbeddedApp
{
[DllImport("user32.dll")]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent); [DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags); [DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool ShowWindow(IntPtr hWnd, short State); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool MoveWindow(IntPtr hWnd, int x,int y,int cx,int cy,bool repaint); public const int HWND_TOP = 0x0;
public const int WM_COMMAND = 0x0112;
public const int WM_QT_PAINT = 0xC2DC;
public const int WM_PAINT = 0x000F;
public const int WM_SIZE = 0x0005;
public const int SWP_FRAMECHANGED = 0x0020;
}
UserControl:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
} public Panel PanlParent
{
get { return this.panel1; }
}
}
WPF 代码:
前台:
<Window x:Class="WpfApplicationWin32.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:WpfApplicationWin32"
Title="MainWindow" Height="" Width="">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Name="btnOPen" Content="打开" Height="" Width="" Click="btnOPen_Click"></Button>
<Grid Grid.Row="">
<WindowsFormsHost Name="windowsFormsHost1" SizeChanged="windowsFormsHost1_SizeChanged">
<wf:UserControl1 x:Name="myUCParent"></wf:UserControl1>
</WindowsFormsHost>
</Grid>
</Grid>
</Window>
后台:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); }
Form1 f1;
IntPtr intptrChild = IntPtr.Zero;
private void btnOPen_Click(object sender, RoutedEventArgs e)
{
IntPtr intptrParent = myUCParent.PanlParent.Handle;
//
f1 = new Form1();
f1.Show();
intptrChild = f1.Handle;
//
Thread tt = new Thread(() =>
{
while (true)
{
if (intptrChild != IntPtr.Zero)
{
this.Dispatcher.Invoke(new Action(() =>
{
EmbeddedApp.SetParent(intptrChild, intptrParent);
EmbeddedApp.MoveWindow(intptrChild, , , myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
EmbeddedApp.ShowWindow(intptrChild, );
})); break;
} }
}); tt.IsBackground = true;
tt.Start();
} private void windowsFormsHost1_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (f1 == null) return;
EmbeddedApp.MoveWindow(f1.Handle, , , myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
}
}
转载请注明出处,谢谢!
WPF Win32 API 嵌入Form 窗体的更多相关文章
- C# winform 将其他程序嵌入Form窗体
嵌入类 public class ExeImpaction { public void FrmClosing() { try { if (!process.HasExited) process.Kil ...
- WinForm中如何实现在容器控件中嵌入form窗体(panel与子窗体)
今天在做项目时候遇到一个问题,窗体分为左右两部分,要求在左边栏点击按钮时,右边动态加载窗体最后想到用panel实现,经历几次失败,并查找资料后,终于搞定 说明:如果多次切换需加入 panel.clea ...
- 通过 WIN32 API 实现嵌入程序窗体
写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做 ...
- C#通过WIN32 API实现嵌入程序窗体
本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,分享给大家供大家参考.具体如下: 这是一个不使用COM,而是通过WIN32 API实现的示例, 它把写字板程序嵌在了自己的一个面板中. ...
- 【Win32 API】利用SendMessage实现winform与wpf之间的消息传递
原文:[Win32 API]利用SendMessage实现winform与wpf之间的消息传递 引言 有一次心血来潮,突然想研究一下进程间的通信,能够实现消息传递的方法有几种,其中win32ap ...
- 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体
WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...
- 重温 Win32 API ----- 截屏指定窗体并打印
朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...
- WPF 调用API修改窗体风格实现真正的无边框窗体
原文:WPF 调用API修改窗体风格实现真正的无边框窗体 WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm ...
- exe程序嵌入Winform窗体
1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...
随机推荐
- 多线程模块的同步机制event对象
多线程模块的同步机制event对象 线程的核心特征就是他们能够以非确定的方式(即何时开始执行,何时被打断,何时恢复完全由操作系统来调度管理,这是用户和程序员无法确定的)独立执行的,如果程序中有其他线程 ...
- tigervnc-server安装
#vncserver安装方法 #su - root rpm -ivh tigervnc-server-1.8.0-13.el7.x86_64.rpm cp /lib/systemd/system/vn ...
- Fiddler抓包HTTPS捕捉旧版App
“现在可以公开的情报:简易操作Fiddler抓包可能” 任何App的更新都限于苹果开发者规定,有时为了上架不得已放弃一些真正实用的功能,比如视频音频的直接下载,脚本的直接导入,手机上IPA的直接安装等 ...
- Derby 数据库 客户端 ij使用
Derby是开源的.嵌入式的Java数据库程序,ij是Derby提供的客户端工具,相当于其他数据库提供的sqlplus工具. ij是纯Java的程序,不用安装,使用起来就像运行普通的Java应用程序一 ...
- [原创]在Windows平台使用msys2、mingw64和vscode编写和调试C/C++代码
相关名词就不解释了,这里主要讲讲在vscode里怎么配,这里假设大家相关工具已经装好. 题外话:里面的大多数坑都是windows平台和linux平台的差异造成的,如果在linux平台配置,应该会顺利很 ...
- [唐胡璐]QTP技巧 - QTP菜单项消失
有时候QTP的菜单栏的下拉菜单为空。 解决方法:在菜单栏点击右键,选择“Customize”,在Customize窗口的ToolBarTab页,点击“Restore All”后即可。
- Mybatis一对一和一对多配置
作者:夕下奕林 问题描述 现在有三张数据表,表名为orders,orderdetail,items,分别表示订单,订单详情,商品. 其中一个订单包含多个订单详情,表示订单中的不同个具体的商品,订单详情 ...
- [MySQL优化] -- 如何了解SQL的执行频率
MySQL 客户端连接成功后,通过 show [session|global]status 命令 可以提供服务器状态信息,也可以在操作系统上使用 mysqladmin extended-status ...
- c++ 用eclipse建立一个类,并实例化并运行
新建项目 file->new->c/c++ project 项目结构 cpc.cpp //================================================= ...
- ila核数据输出
在Tcl Console中输入以下命令(其中dataxxxx表示文件名,hw_ila_2则为ila窗口名): write_hw_ila_data -csv_file -force dataxxxx [ ...