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 ...
随机推荐
- web开发:javascript高级
一.事件案例 二.循环绑定之变量污染 三.事件的绑定与取消 四.事件对象 一.事件案例 <!DOCTYPE html> <html> <head> <meta ...
- 用 Spark 处理复杂数据类型(Array、Map、JSON字符串等)
split 对列表进行切割,然后生产新的列表 from pyspark.sql.functions import split# 对 key列按照 0-9之间的数字进行风格 重新命名为 s df.se ...
- linux(1)
Linux/Unix操作系统 OS 系统软件 用户.应用程序 <-OS-> 硬件:CPU Memory Disk 外设管理软件测试方向: 被测系统主要的操作系统,监控系统资源.使用系统常用 ...
- 【小知识】证明 $1$ 到 $n$ 的立方和公式
scb 发明了小学奥数(确信) Formula \(\sum\limits_{i=1}^n i^3 = (\sum\limits_{i=1}^n i)^2\) Provement 构造一个矩阵 \(a ...
- 十一届河南省赛-checkpoints(个人解法)-能AC代码
大致题意: zznuoj,大致题意:从A点出发达到B点去解救人质,再从B点返回到A点,经历第二遍的点只计算一次即可,AB两点不计数!求完成任务最少需要经过的点数. 大致思路:暴力!从起点到终点,和从终 ...
- 登录网站的autohotkey脚本
;如果需要这个软件的代码逻辑,删除敏感信息只需要把上面3行代码删除即可. !:: ;login经过测试必须用ie浏览器,在默认软件里面浏览器设置成ie即可,其他浏览器不让send密码 ;虽然通过程序把 ...
- P3254——DP&&入门
题目 给定一个$n \times m$的$01$矩形,选择其中为$1$的位置,要求互不相邻,问方案数. 解决方案 直接dp因为状态较多,数组很难直接表示出来,我们采用二进制状态压缩存储. 用$dp[i ...
- 【Android-开发环境】 eclipse开发环境搭建
1.下载安装JDK jdk下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- 多个idea项目使用同一个tomcat
配置好tomcat后,每个项目使用不同的虚拟路径.并且把tomcat端口改为不一样的就可以了.
- yii框架学习(获取插入后的id)
插入单条数据, 获取插入数据的id. $model->attributes['id']; new model 添加数据的时候, 需要注意, 如果是想要循环添加多条数据的情况, new mode ...