zoukankan      html  css  js  c++  java
  • 如何把其他Win32程序的窗体嵌入到我们托管的WinForm中

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;

    namespace TestParentChildForm
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            private static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);

            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

            [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
                        int X, int Y, int cx, int cy, uint uFlags);

            [DllImport("user32.dll")]
            private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern bool ShowWindow(IntPtr hWnd, short State);

            private const int HWND_TOP = 0x0;
            private const int WM_COMMAND = 0x0112;
            private const int WM_QT_PAINT = 0xC2DC;
            private const int WM_PAINT = 0x000F;
            private const int WM_SIZE = 0x0005;
            private const int SWP_FRAMECHANGED = 0x0020;
            private enum ShowWindowStyles : short
            {
                SW_HIDE = 0,
                SW_SHOWNORMAL = 1,
                SW_NORMAL = 1,
                SW_SHOWMINIMIZED = 2,
                SW_SHOWMAXIMIZED = 3,
                SW_MAXIMIZE = 3,
                SW_SHOWNOACTIVATE = 4,
                SW_SHOW = 5,
                SW_MINIMIZE = 6,
                SW_SHOWMINNOACTIVE = 7,
                SW_SHOWNA = 8,
                SW_RESTORE = 9,
                SW_SHOWDEFAULT = 10,
                SW_FORCEMINIMIZE = 11,
                SW_MAX = 11
            }
            private Process p = null;

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                p = new Process();
                //需要启动的程序
                p.StartInfo.FileName = textBox1.Text;
                //为了美观,启动的时候最小化程序
                p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
                //启动
                p.Start();

                //这里必须等待,否则启动程序的句柄还没有创建,不能控制程序
                Thread.Sleep(1000);
                //最大化启动的程序
                ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_NORMAL);
                //设置被绑架程序的父窗口
                SetParent(p.MainWindowHandle, this.panel1.Handle);
                //改变尺寸
                ResizeControl();
            }

            //控制嵌入程序的位置和尺寸
            private void ResizeControl()
            {
                SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0);
                PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0);
                SetWindowPos(p.MainWindowHandle, HWND_TOP, 0 - 5, 0 - 41, panel1.Width, panel1.Height + 36,SWP_FRAMECHANGED);
                SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
            }

            private void Form1_SizeChanged(object sender, EventArgs e)
            {
                ResizeControl();
            }
        }
    }

  • 相关阅读:
    Webform Session、Cookies传值,跳转页面方式
    webform 光棒效果,删除操作弹出确定取消窗口
    webform Repeater、地址栏传值、Response
    WebForm 控件
    WebForm 页面传值
    WebForm——IIS服务器、开发方式和简单基础
    身份证件号的验证与更改
    历届试题 带分数
    算法提高 学霸的迷宫
    算法提高 最大乘积
  • 原文地址:https://www.cnblogs.com/junior/p/2588130.html
Copyright © 2011-2022 走看看