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 窗体的更多相关文章

  1. C# winform 将其他程序嵌入Form窗体

    嵌入类 public class ExeImpaction { public void FrmClosing() { try { if (!process.HasExited) process.Kil ...

  2. WinForm中如何实现在容器控件中嵌入form窗体(panel与子窗体)

    今天在做项目时候遇到一个问题,窗体分为左右两部分,要求在左边栏点击按钮时,右边动态加载窗体最后想到用panel实现,经历几次失败,并查找资料后,终于搞定 说明:如果多次切换需加入 panel.clea ...

  3. 通过 WIN32 API 实现嵌入程序窗体

    写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做 ...

  4. C#通过WIN32 API实现嵌入程序窗体

    本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,分享给大家供大家参考.具体如下: 这是一个不使用COM,而是通过WIN32 API实现的示例, 它把写字板程序嵌在了自己的一个面板中. ...

  5. 【Win32 API】利用SendMessage实现winform与wpf之间的消息传递

    原文:[Win32 API]利用SendMessage实现winform与wpf之间的消息传递 引言    有一次心血来潮,突然想研究一下进程间的通信,能够实现消息传递的方法有几种,其中win32ap ...

  6. 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体

    WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...

  7. 重温 Win32 API ----- 截屏指定窗体并打印

    朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...

  8. WPF 调用API修改窗体风格实现真正的无边框窗体

    原文:WPF 调用API修改窗体风格实现真正的无边框窗体 WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm ...

  9. exe程序嵌入Winform窗体

    1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...

随机推荐

  1. sklearn--数据集的处理 模型参数选择

    1.随机划分训练集和测试集 sklearn.model_selection.train_test_split 一般形式: train_test_split是交叉验证中常用的函数,功能是从样本中随机的按 ...

  2. Linux学习笔记(一)分区

    一.硬件设备文件名 二.设备文件名 /dev/hda1(IDE硬盘接口) /dev/sda1(SCSI硬盘接口.SATA硬盘接口) 其中,a代表第1个硬盘(以此类推,b为第2个硬盘),1代表第1个分区 ...

  3. bat 判断命令是否执行成功

    bat 判断命令是否执行成功 连接符形式,&& 表示成功,|| 表示失败,例如: call xxx.bat && (goto succeed) || goto fail ...

  4. [SCOI2016]美味——主席树+按位贪心

    原题戳这里 题解 让异或值最大显然要按位贪心,然后我们还发现加上一个\(x_i\)的效果就是所有\(a_i\)整体向右偏移了,我们对于\({a_i}\)开个主席树,支持查询一个区间中有多少个在\([L ...

  5. MyBatis执行原理图

    作者:W&L 推荐: 陶邦仁的博客 (1)加载配置并初始化       触发条件:加载配置文件 配置来源于两个地方,一处是配置文件,一处是Java代码的注解,将SQL的配置信息加载成为一个个M ...

  6. 基于STM32调试工具STM-STUDIO-STM32的使用

    手上有stlink下载器,正好看到官网有这个工具,可以在运行中实时查看变量的数据.这一点和ucos的ucprobe很类似. 参考https://mp.weixin.qq.com/s?src=11&am ...

  7. Python3-BaseType

    counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print(counter) print(mil ...

  8. 引爆炸弹——DFS&&联通块

    题目 链接 在一个$n \times m$方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地 ...

  9. BZOJ 1453 (线段树+并查集)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1453 题意:一个 n*n 的矩阵,每个位置有黑/白两种颜色,有 m 次操作,每次可以翻转 ...

  10. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...