2012-11-09 55 views
3

我正在製作疊加層。我這裏這個代碼將窗體的父窗體設置爲「FindWindow」

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; 


    namespace HyperBox 
    { 

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 

     this.TopMost = true; // make the form always on top 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border 
     this.WindowState = FormWindowState.Maximized; // maximized 
     this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized 
     this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized 
     this.TransparencyKey = this.BackColor = Color.Red; // the color key to transparent, choose a color that you don't use 

     // Set the form click-through 
     int initialStyle = GetWindowLong(this.Handle, -20); 
     SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20); 
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] 
    static extern int GetWindowLong(IntPtr hWnd, int nIndex); 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha,   uint dwFlags); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern int SetParent(int hWndChild, int hWndNewParent); 


    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     // draw what you want 
     e.Graphics.FillEllipse(Brushes.Blue, 30, 30, 100, 100); 

    } 
    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 

    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 



} 

    } 

它繪製橢圓到窗體上,其是透明的且始終ontop的。問題是它不能在全屏模式下工作。

我一直在使用這個

SetParent(this.handle, FindWindow(null, "<parent window title here>")); 

除了我得到的錯誤嘗試。有人可以幫忙嗎?

+0

我很好奇你得到的錯誤... –

+0

的錯誤是: 參數1:不能轉換從「System.IntPtr」到「INT ' 參數2:無法從'System.IntPtr'轉換爲'int' 關於此行: SetParent(this.Handle,FindWindow(null,「」)); –

回答

3

我相信你的錯誤就在這裏

[System.Runtime.InteropServices.DllImport("user32.dll")] 
static extern int SetParent(int hWndChild, int hWndNewParent); 

該公司預計IntPtr類型的兩個參數不int並返回一個IntPtr不是int

MSDN提供了更多信息。查看用戶對底層C++示例的貢獻。

請注意,externDllImport一起使用時,是對非託管代碼的引用。 user32.dll中的方法稱爲SetParent()沒有定義接受兩個int s作爲參數。

所以該塊應改爲:

[System.Runtime.InteropServices.DllImport("user32.dll")] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);