首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将窗体的父级设置为"FindWindow“

将窗体的父级设置为"FindWindow“
EN

Stack Overflow用户
提问于 2012-11-10 07:35:44
回答 1查看 1.6K关注 0票数 3

我正在制作一个覆盖图。我在这里有这个代码

代码语言:javascript
复制
    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)
    {

    }



}

    }

它在窗体上绘制一个椭圆,该窗体是透明的并且始终在顶部。问题是,它不能在全屏上工作。

我试过用这个

代码语言:javascript
复制
    SetParent(this.handle, FindWindow(null, "<parent window title here>"));

除了我得到了错误。有没有人能帮帮忙?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-10 11:39:24

我相信你的错误就在这里

代码语言:javascript
复制
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);

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

MSDN提供了更多信息。有关一些好的C#示例,请参阅底部的用户贡献。

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

因此该块应为:

代码语言:javascript
复制
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13317585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档