c# - 将表单的父级设置为 "FindWindow"

标签 c# c parent setparent

我正在制作叠加层。我这里有这段代码

    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)
    {

    }



}

    }

它在窗体上绘制一个椭圆,该窗体是透明的并且始终在最上面。问题是它无法在全屏模式下工作。

我试过用这个

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

除了我得到错误。有人可以帮忙吗?

最佳答案

我相信你的错误就在这里

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

它需要两个类型为 IntPtr 而非 int 的参数,并且返回一个 IntPtr 而不是 int

MSDN提供更多信息。查看底部的用户贡献,了解一些很好的 C# 示例。

请记住 extern ,当与 DllImport 一起使用时,是对非托管代码的引用。 user32.dll 中名为 SetParent() 的方法没有接受两个 int 作为参数的定义。

因此该 block 应为:

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

关于c# - 将表单的父级设置为 "FindWindow",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317585/

相关文章:

c# - 使用我的存储库时无法解析类型的服务

c# - .NET:强制调试器进入属性的属性

c - 帮助理解 Arduino Mega 上的串行通信 (SPI)

c - 使用 libJPEG 访问 DCT 系数和量化值

c - 制作模拟 UNIX shell

html - 如何使子 div 始终适合父 div?

jqueryparent.html 不是一个函数

c# - 如何获取网络接口(interface)的状态?

c# - 当需要该类 WPF 的多个实例时,模型内部的 INotifyPropertyChanged

PHP SimpleXML XPath 获取下一个父节点