1. 程式人生 > >C# 窗體操作彙總

C# 窗體操作彙總



所謂"綁架"就是把其他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 { publicpartialclass Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll")] privatestaticexternint SetParent(IntPtr hWndChild, IntPtr hWndParent); [DllImport(
"user32.dll")] privatestaticexternbool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError =true)] privatestaticexternbool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", EntryPoint ="SetWindowPos")]
privatestaticexternbool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); [DllImport("user32.dll")] privatestaticexternint SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", SetLastError =true, CharSet = CharSet.Auto)] privatestaticexternuint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong); [DllImport("user32.dll", SetLastError =true, CharSet = CharSet.Auto)] privatestaticexternuint GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", CharSet = CharSet.Auto)] privatestaticexternbool ShowWindow(IntPtr hWnd, short State); privateconstint HWND_TOP =0x0; privateconstint WM_COMMAND =0x0112; privateconstint WM_QT_PAINT =0xC2DC; privateconstint WM_PAINT =0x000F; privateconstint WM_SIZE =0x0005; privateconstint SWP_FRAMECHANGED =0x0020; publicenum 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 } privatevoid 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); } // 控制嵌入程式的位置和尺寸privatevoid 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(); } } }