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 = -16;

        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="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Name="btnOPen" Content="打开" Height="23" Width="100" Click="btnOPen_Click"></Button>
        <Grid Grid.Row="1">
            <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, 0, 0, myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
                            EmbeddedApp.ShowWindow(intptrChild, 5);
                        }));

                        break;
                    }

                }
            });

            tt.IsBackground = true;
            tt.Start();
        }

        private void windowsFormsHost1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (f1 == null) return;
            EmbeddedApp.MoveWindow(f1.Handle, 0, 0, myUCParent.PanlParent.Width, myUCParent.PanlParent.Height, true);
        }
    }

转载请注明出处,谢谢!

时间: 2024-08-15 04:33:51

WPF Win32 API 嵌入Form 窗体的相关文章

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

写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做到, 以下是详细注释过的主要源代码. 我把它封装到一个类中: [csharp] view plaincopy using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys

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

本文实例讲述了C#通过WIN32 API实现嵌入程序窗体的方法,分享给大家供大家参考.具体如下: 这是一个不使用COM,而是通过WIN32 API实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做到, 以下是详细注释过的主要源代码. 我们把它封装到一个类中: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

利用win32 api实现进程通信---通过剪切板

// c#中win32 api的调用 //windows 消息机制的原理 //clipboard viewer chain. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.I

C语言调用WIN32 API教程之1创建窗口

本学习笔记基于VC++6.0开发环境,通过c语言编程语言,调用win32 API进行windows系统应用程序开发. 1,打开VC++6.0,点击 文件->新建->工程->Win32 Application 工程名填写example1,点击确定,选择 一个空工程,点击完成. 2,点击"新建文件" 按钮,新建一个空白文件,点击 文件->另存为 输入文件名example1.c 选择工作空间对应的文件夹,保存. 3,点击FileView,右击Source File,点

C#调用Win32 api学习总结

从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微软留给我们直接控制Windows的接口. 一.    基础知识 Win32 API是C语言(注意,不是C++语言,尽管C语言是C++语言的子集)函数集. 1. Win32 API函数放在哪里? Win32 API函数是Windows的核心,比如我们看到的窗体.按钮.对话框什么的,都是依靠Win32函

从.NET平台调用Win32 API

小序        Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微软留给我们直接控制Windows的接口.想玩儿吗?呵呵,太难了.        C#使用非常简单,写程序就像打拱猪,Sorry  -_-! ,搭积木一样简单.想玩儿吗?呵呵,没办法直接控制Windows的核心.        难道就没有两全其美的办法吗?当然不是!要不微软的产品早就没人买了.其实从C#(或者说.NET

Win32 API编程——前言

一丶什么是Win32 API? 简单来说,就是微软为了保护操作系统的安全稳定,不允许运行在用户层的进程随意操控系统内核,而是必须按照一定方式.就是说我们用户层要与系统内核层交互(比如对内存.进程操作),只能通过调用Windows内核层提供的接口函数,也就是Win32API来操控.这些API以DLL(动态链接库)的形式保存(一般在SYSTEM32文件夹中,你可以发现大量的DLL),我们最常用的是kernel32.dll.user32.dll和gdi32.dll. 所有基于NT内核(包括XP到Win

win32 api

Microsoft在StrSafe.h定义了新的安全字符串函数 为了防止缓冲区溢出,在将一个可写缓冲区作为参数传递时,必须传递它的大小,大小可以有_countof获得, _countof获取字符数, sizeof获取字节数 for example: int a[10]; // _countof(a) == 10 _CrtSetReportMode(_CRT_ASSERT, 0); // in head of program 可以禁止C run time可能触发的debug assertion f

使用win32 API 实现串行通信 (一)

本文基于wince平台,使用win32 API实现串行通信 1.打开和关闭串行端口 串行端口设备使用CreateFile函数打开,所使用的名称要遵循特定的格式,即3个字符 COM后紧跟要打开的COM端口号,再加个冒号,冒号是Windows CE所必需的. 如,hser=CreateFile(TEXT(“COM1:”),GENERIC_READ|GENERIC_WRITE,0, NULL,OPEN_EXISTING,0,NULL),为以可读可写的方式打开COM1端口. 调用CloseHandle函