首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制图形不起作用

绘制图形不起作用
EN

Stack Overflow用户
提问于 2012-11-10 09:03:04
回答 1查看 1.8K关注 0票数 0

我有这个代码,它使表单始终在顶部,透明和点击。

代码语言: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;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

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


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(
     [MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
     [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
    [DllImport("user32.dll")]
    public static extern IntPtr SetParent(
     IntPtr hWndChild,      // handle to window
     IntPtr hWndNewParent   // new parent window
     );

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // draw what you want
        e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 4, (SystemInformation.WorkingArea.Height / 2) - 20, 8, 40));
        e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 20, (SystemInformation.WorkingArea.Height / 2) - 4, 40, 8));


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

    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

        IntPtr hwndf = this.Handle;
        IntPtr hwndParent = FindWindow("chrome.exe", null);
        SetParent(hwndf, hwndParent);



    }

}
    }

问题是,当我绘制图形时,它什么也没画。当坐标在100度左右时,它就起作用了。但当它执行上面的方法时,什么也不会发生。一点都没有,甚至连一个像素都没有。有没有人能解释一下为什么会发生这种情况,或者转发一个固定的代码片段,谢谢。莱恩

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-10 09:58:34

OnPaint为你的表单提供了一个图形对象,而不是屏幕。您正在基于系统的工作区填充矩形,而不是表单。您需要调整矩形坐标,并将窗体定位到要显示图形的位置。位置为(0, 0)的矩形是窗体工作区的左上角。您还应该能够通过调用在窗体基类上公开的ClientRectangle来访问该矩形。

看看这个在窗体外部绘制的问题:Draw / Paint Outside Form,如果您不想在窗体上绘制,它可以帮助您开始正确的方向,但根据需要重新定位和调整窗体大小可能会更容易。

编辑当你调试你的问题时,至少添加一些边框可能是明智的。这将帮助您查看窗体的位置以及它在哪个监视器上。然后,您可以在OnPaint中断点时检查您的数字,以确保您正确地创建了矩形,但是,确保您在窗体的工作区内绘制应该可以解决您的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13318194

复制
相关文章

相似问题

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