在透明表单上绘制不透明图形 - C#winforms

时间:2014-01-08 23:51:40

标签: c# winforms graphics drawing

我最近一直在制作一台屏幕录像机,我想实现一个区域录像机。我设置了绘制区域所需的代码,但问题是我无法摆脱表单。

  • 我尝试了TransparencyKey = BackColor;,但是点击了表单,不允许用户绘制区域

  • 我也尝试了Opacity = 0F;,但这使得表单不可见,当设置为非常低的值时,它甚至使矩形变得透明。

我的目标是使表单完全透明且可点击,而我绘制的图形仍然是不透明的。

编辑:这是我用来绘制区域的代码

private void frmDraw_MouseDown(object sender, MouseEventArgs e) //determines if the user has his finger on the mouse
    {
        if (e.Button != MouseButtons.Left) return;
        currentPos = startPos = e.Location;
        drawing = true;
    }

    private void frmDraw_MouseMove(object sender, MouseEventArgs e) //refreshes the form to redraw the rectangle
    {
        if (!drawing) return;
        currentPos = e.Location;
        Invalidate();
    }

    private void frmDraw_MouseUp(object sender, MouseEventArgs e) //disables drawing when the user releases the click
    {
        drawing = false;
        Invalidate();
        DialogResult d = MessageBox.Show(strings.frmDrawMessage, strings.frmDrawConfirmation, MessageBoxButtons.YesNoCancel);
        switch (d)
        {
            case DialogResult.Yes:
                finalRectangle = getRectangle();
                isNotCanceled = true;
                Close();
                break;
            case DialogResult.No:
                Close();
                break;
        }
    }

    public Rectangle getRectangle() //returns a rectangle from the coordinates selected
    {
        return new Rectangle(
            Math.Min(startPos.X, currentPos.X),
            Math.Min(startPos.Y, currentPos.Y),
            Math.Abs(startPos.X - currentPos.X) + (firstRectangle ? 0 : 1),
            Math.Abs(startPos.Y - currentPos.Y) + (firstRectangle ? 0 : 1)
                            );
    }

    private void frmDraw_Paint(object sender, PaintEventArgs e) //this block is responsible of drawing the rectangle
    {
        Pen p = new Pen(new HatchBrush(HatchStyle.SmallCheckerBoard, Color.Black,Color.White), 2F); //this is the pen used to draw the rectangle
        Rectangle r = getRectangle(); //this is the rectangle that needs to be drawn
        firstRectangle = false;

        string strToDraw = "{X=" + r.X + ", Y=" + r.Y + "} {W=" + r.Width + ", H=" + r.Height + "}"; //this string is going to be drawn to show the specifications of the rectangle
        Font f = new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel); //this is the font use to draw  the string
        Size size = e.Graphics.MeasureString(strToDraw, f).ToSize(); //this is the size it will take to draw the string
        int x = r.X > Width - size.Width ? r.X - (size.Width - r.Width) : r.X;              //      selects the x coordinate depending 
        int y = r.Y < ((int)f.Size + 3) ? 0 : r.Y - (int)f.Size - 3;    //     on the (0, 0) point of the rectangle
        Point loc = new Point(x, y); //creates a point from the previous coordinates where we draw the stats

        e.Graphics.DrawRectangle(p, r); //draws the main rectangle
        e.Graphics.FillRectangle(Brushes.White,new Rectangle(loc, size)); //draws the background rectangle for the string
        e.Graphics.DrawString(strToDraw,f,Brushes.Gray,loc); //and finally draws the string
        e.Graphics.Flush(); //flushes resources
    }

由于

0 个答案:

没有答案