C# 實現視窗”綁架”

來源:互聯網
上載者:User
所謂"綁架"就是把其他Win32程式的表單嵌入到我們託管的WinForm中.網上已經用很多java版和Delphi版還有WPF的.我在這裡補充C#版的.
定義需要的Win32 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 public 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 void Form1_Load(object sender, EventArgs e)
{
    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();
}
//控制嵌入程式的位置和尺寸
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,
      (int)this.Width,
      (int)this.Height+36,
      SWP_FRAMECHANGED);

    SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
}

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

需要注意的是:ResizeControl()方法很重要,否則達不到我們預期的效果.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.