如何在C#中清除矩形的填充?

时间:2012-05-08 17:51:24

标签: c# graphics drawing gdi

如何清除矩形的填充?我只想保留边界。

g.FillRectangle(Brushes.Transparent, x, y, w, h);

没有用,没有带有alpha的rGB,我想删除填充,所以只留下边框。

3 个答案:

答案 0 :(得分:6)

所以你想要的是

g.DrawRectangle(Pens.Black,x,y,w,h);

我认为

编辑:由于OP要求的变化,这并不是他想要的答案,虽然这不是不正确的,所以我现在选择留在这里。

答案 1 :(得分:3)

设置剪辑后,必须为图形设置新剪辑,然后将剪辑恢复为正常。

g.SetClip(new Rectangle(x,y,w,h), CombineMode.Replace);
g.Clear(Color.Transparent);

答案 2 :(得分:1)

好的,所以你选择了一个工具,你可能想先告诉我们。

创建一个新的Windows窗体应用程序。

事件中的

使用mousedown,mouseup和mousemove

public Point MouseXY = new Point(0, 0);

private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        MouseXY = e.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int width = e.Location.X - MouseXY.X;
            int height = e.Location.Y-MouseXY.Y;
            this.Refresh();
            CreateGraphics().DrawRectangle(Pens.Blue, new Rectangle(MouseXY, new Size(width,height)));

        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        this.Refresh();
    }

此代码并不完美,我不会假装它。这将做的是绘制一个蓝色矩形,从你点击并跟随你的鼠标开始。它不会绘制负矩形,你必须确定你的鼠标当前是从你的起点向左还是向上,然后相应地绘制矩形,但我认为你可以自己解决这个问题。同样,矩形不是持久性的,但我不相信你会想要它。