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 ...
随机推荐
- java线程基础巩固---策略模式在Thread和Runnable中的应用分析
在上篇[http://www.cnblogs.com/webor2006/p/7709647.html]中已经学习了Runnable出现的好处,其实这种设计是采用的一种策略模式,所以为了进一步理解Ru ...
- C#实现异步阻塞TCP(SocketAsyncEventArgs,SendAsync,ReceiveAsync,AcceptAsync,ConnectAsync)
1.类 (1)socket IO操作内存管理类 BufferManager // This class creates a single large buffer which can be divid ...
- 创建yum本地仓库,将阿里仓库同步到本地,并定时更新
很多时候为了加速自己内部的rpm包安装速度,都会搭建自己的yum源仓库,而使用系统光盘自带的源,由于软件版本比较落后,所以不太适用,而大家都在用的阿里仓库比较好用,所以就想到了把阿里仓库的rpm全部拉 ...
- BZOJ3157 国王奇遇记——神奇的推式子
先膜一发Miskcoo,大佬的博客上多项式相关的非常全 原题戳我 题目大意 求 \[\sum\limits_{i=1}^{n}i^mm^i\] 题解 设一个函数\(f(i)=\sum\limits_{ ...
- 第二个爬虫之爬取知乎用户回答和文章并将所有内容保存到txt文件中
自从这两天开始学爬虫,就一直想做个爬虫爬知乎.于是就开始动手了. 知乎用户动态采取的是动态加载的方式,也就是先加载一部分的动态,要一直滑道底才会加载另一部分的动态.要爬取全部的动态,就得先获取全部的u ...
- py操作mongodb总结
python使用的版本 python3. python操作mongodb使用的是pymongo,安装方法: pip install pymongo 测试 PyMongo 接下来我们可以创建一个测试文件 ...
- springbootdruidmybatismysql多数据源事务管理
springboot+druid+mybatis+mysql+多数据源事务管理 分布式事务在java中的解决方案就是JTA(即Java Transaction API):springboot官方提供了 ...
- 查看PublicKeyToken和生成PublicKeyToken
http://hi.baidu.com/honfei/item/7777500b20d8ff8a02ce1bd2
- luogu 2294 [HNOI2005]狡猾的商人 差分约束
一个差分约束模型,只需判一下有没有负环即可. #include <bits/stdc++.h> #define N 103 #define M 2004 #define setIO(s) ...
- 二维FFT,IFFT,c语言实现
学习DIP第6天 完整内容迁移至http://www.face2ai.com/DIP-2-4-二维FFT-IFFT-c语言实现/ http://www.tony4ai.com/DIP-2-4-二维FF ...