博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]将一个窗体置入另一窗体
阅读量:4945 次
发布时间:2019-06-11

本文共 6076 字,大约阅读时间需要 20 分钟。

http://bbs.csdn.net/topics/390768725
http://blog.163.com/kingmax_res/blog/static/7728244220112204185169/
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;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        Process process = null;        IntPtr appWin;        private string exeName = "";        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,            CharSet = CharSet.Unicode, ExactSpelling = true,            CallingConvention = CallingConvention.StdCall)]        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);        [DllImport("user32.dll", SetLastError = true)]        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        [DllImport("user32.dll", SetLastError = true)]        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);        [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]        private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);        //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);        [DllImport("user32.dll", SetLastError = true)]        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);        [DllImport("user32.dll", SetLastError = true)]        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]        private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);        private const int SWP_NOOWNERZORDER = 0x200;        private const int SWP_NOREDRAW = 0x8;        private const int SWP_NOZORDER = 0x4;        private const int SWP_SHOWWINDOW = 0x0040;        private const int WS_EX_MDICHILD = 0x40;        private const int SWP_FRAMECHANGED = 0x20;        private const int SWP_NOACTIVATE = 0x10;        private const int SWP_ASYNCWINDOWPOS = 0x4000;        private const int SWP_NOMOVE = 0x2;        private const int SWP_NOSIZE = 0x1;        private const int GWL_STYLE = (-16);        private const int WS_VISIBLE = 0x10000000;        private const int WM_CLOSE = 0x10;        private const int WS_CHILD = 0x40000000;        public string ExeName        {            get            {                return exeName;            }            set            {                exeName = value;            }        }        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            this.exeName = @"C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe";            try            {                // Start the process                 process = System.Diagnostics.Process.Start(this.exeName);                // Wait for process to be created and enter idle condition                 process.WaitForInputIdle();                // Get the main handle                appWin = process.MainWindowHandle;            }            catch (Exception ex)            {                MessageBox.Show(this, ex.Message, "Error");            }            // Put it into this form            SetParent(appWin, this.Handle);            // Remove border and whatnot            // SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);            // Move the window to overlay it on this window            MoveWindow(appWin, 0, 0, this.Width, this.Height, true);            SendKeys.Send("ABC");        }        private void Form2_FormClosed(object sender, FormClosedEventArgs e)        {            try            {                process.Kill();            }            catch { }        }        private void Form2_Resize(object sender, EventArgs e)        {            if (this.appWin != IntPtr.Zero)            {                MoveWindow(appWin, 0, 0, this.Width, this.Height, true);            }            //base.OnResize(e);        }        /*        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]        private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]        private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);        const uint BM_CLICK = 0xF5;        private void DoClick()        {            //下面的这些参数都可以用Spy++查到                    string lpszParentClass = "#000000"; //整个窗口的类名,需要Spy++查询对应的窗口            string lpszParentWindow = "使命召唤10 xxxxxxx"; //窗口标题,这也是要Spy++去查                   string lpszClass_Submit = "Button"; //需要查找的Button的类名,这里需要Spy++去查看是按钮还是图片什么的            string lpszName_Submit = "开始游戏"; //需要查找的Button的标题,这里需要Spy++去查看是按钮还是图片什么的                    IntPtr ParenthWnd = new IntPtr(0);            IntPtr EdithWnd = new IntPtr(0);            //查到窗体,得到整个窗体                    ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);            //判断这个窗体是否有效                    if (!ParenthWnd.Equals(IntPtr.Zero))            {                //得到Button,并触发它的Click事件                            EdithWnd = FindWindowEx(ParenthWnd, (uint)EdithWnd, lpszClass_Submit, lpszName_Submit);                if (!EdithWnd.Equals(IntPtr.Zero))                {                    SendMessage(EdithWnd, BM_CLICK, 0, 0);                }            }        }         * */    }}

 

转载于:https://www.cnblogs.com/z5337/p/4323056.html

你可能感兴趣的文章
操作引入xml文件的书包(定位到指定节点)
查看>>
操作系统学习笔记系列(一)- 导论
查看>>
CSS实例:图片导航块
查看>>
window的对象有哪些(笔记)
查看>>
Boolean Expressions
查看>>
They Are Everywhere
查看>>
数据结构--汉诺塔递归Java实现
查看>>
day14 多态与抽象
查看>>
Eclipse CDT 出现 launch failed Binary not found
查看>>
apache jmeter
查看>>
Linux 基本命令
查看>>
RedHat7.0 网络源的配置
查看>>
(Mark)JS中关于闭包
查看>>
流程结构图
查看>>
ios端web app在键盘升起后缩小view防止界面仍可上下滑动
查看>>
从service弹出系统级自定义提示框,可在任意页面弹出
查看>>
Bootstrap简单介绍
查看>>
iOS Touch ID 身份认证
查看>>
springboot 注解笔记
查看>>
图解HTTP---------------------------------------4
查看>>