C# 窗体操作汇总
来源:互联网 发布:阿里云短信群发接口 编辑:程序博客网 时间:2024/06/05 06:29
将程序嵌入到自己的窗体
c#嵌入窗体 经测试OK
C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】
[分享]外部exe窗体嵌入winform
窗口显示在顶层 底层
遍历桌面所有可见窗体
所谓"绑架"就是把其他Win32程序的窗体嵌入到我们托管的WinForm中.网上已经用很多java版和Delphi版还有WPF的.我在这里补充C#版的.
嵌入记事本
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Threading;using System.Diagnostics; namespace WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } [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; public 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 void Form1_Load(object sender, EventArgs e) { Process p = new Process(); // 需要启动的程序 p.StartInfo.FileName = @"C:\Windows\System32\notepad.exe"; // 为了美观,启动的时候最小化程序 p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; // 启动 p.Start(); // 这里必须等待,否则启动程序的句柄还没有创建,不能控制程序 Thread.Sleep(1000); // 最大化启动的程序 ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_MAXIMIZE); // 设置被绑架程序的父窗口 SetParent(p.MainWindowHandle, this.Handle); // 改变尺寸 ResizeControl(p); } // 控制嵌入程序的位置和尺寸 private void ResizeControl(Process p) { SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0); PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0); SetWindowPos( p.MainWindowHandle, HWND_TOP, 0, // 设置偏移量,把原来窗口的菜单遮住 0, (int)this.Width, (int)this.Height, SWP_FRAMECHANGED); SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0); } }}嵌入计算器
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Diagnostics;
using
System.Threading;
namespace
WindowsFormsApplication1
{
public
partial
class
Form1 : Form
{
Process p;
public
Form1()
{
InitializeComponent();
}
#region API
[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;
public
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
}
#endregion
private
void
Form1_Load(
object
sender, EventArgs e)
{
p =
new
Process();
//需要启动的程序
p.StartInfo.FileName =
@"calc.exe"
;
//为了美观,启动的时候最小化程序
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
//启动
p.Start();
//这里必须等待,否则启动程序的句柄还没有创建,不能控制程序
Thread.Sleep(10000);
//最大化启动的程序
ShowWindow(p.MainWindowHandle, (
short
)ShowWindowStyles.SW_MAXIMIZE);
//设置被绑架程序的父窗口
SetParent(p.MainWindowHandle,
this
.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 - 10,
//设置偏移量,把原来窗口的菜单遮住
0 - 32,
(
int
)
this
.Width + 32,
(
int
)
this
.Height + 32,
SWP_FRAMECHANGED);
SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
}
private
void
Form1_SizeChanged(
object
sender, EventArgs e)
{
ResizeControl();
}
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
{
p.Kill();
p.Dispose();
}
}
}
阅读全文
0 0
- C# 窗体操作汇总
- c# 窗体传值汇总
- C# 线程操作窗体
- C#跨窗体操作
- c#窗体相关操作
- C# GridView 操作汇总
- C#目录操作汇总
- C#目录操作汇总
- C#系统操作汇总
- C# 常见操作汇总
- C#注册表操作汇总
- C# 字符串操作汇总
- C# 字符串操作汇总
- C# 窗体间传值方法大汇总
- C# 窗体间传值方法大汇总
- C# 窗体间传值方法大汇总
- C# 窗体间传值方法大汇总(转)
- C# 窗体间传值方法大汇总
- 安卓架构师之路
- 上拉跳转界面,仿淘宝
- 工作感悟
- 连接mysql数据库的配置备忘
- 字符串查找—练习一
- C# 窗体操作汇总
- java.exe和javaw.exe有什么区别?
- AIDL
- SSM多表联合查询
- DatabaseMetaData,ResultSet,ResultSetMetaData三种类型的区别
- caffe win10 ssd训练和预测(CPU)
- mfcs100ud.lib(dllmodul.obj) : error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
- JEESZ RestFul服务介绍
- 逆转字符串