从WinForm手动启动后如何绘制矩形?

时间:2019-02-12 07:36:30

标签: c# gdi+ system.drawing drawing2d system.drawing.graphics

我将要创建一个在屏幕上绘制不同形状的程序。为此,我创建了两个WinForme。第一个(ActiveHandle)在那里选择要绘制的形状。为此,我使用了复选框,每个复选框描述设置文件中的值。到目前为止,该方法有效。但是,如果我现在打开第二个窗体(form1.Show())以在屏幕上绘制没有背景的涂鸦,那将无法正常工作。

我的问题。我忽略了什么或必须改进才能看到图形?当我将form1用作StartForm时,一切正常。

这里有两种格式的源代码。

ActiveHandle(控制form1和形状)(在form1中激活或停用bShapeDrawing)

public partial class ActiveHandle : Form
{
    public ActiveHandle()
    {
        InitializeComponent();
    }

    private void ActiveHandle_Load(object sender, EventArgs e)
    {

    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            Properties.Settings.Default.bShapeDrawing = true;
        }
        else
        {
            Properties.Settings.Default.bShapeDrawing = false;
        }            
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Visible = false;

        Form1 form1 = new Form1();
        form1.Show();
    }
}

Form1(在按下鼠标右键并且bShapeDrawing等于true时,在屏幕上绘制没有背景和边框的形状)

public partial class Form1 : Form
{
    public int iWidth;
    public int iHight;       

    public Form1()
    {
        InitializeComponent();            
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int iWidth = SystemInformation.VirtualScreen.Width;
        int iHight = SystemInformation.VirtualScreen.Height;

        Console.WriteLine("Form1 loading");            
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Form1 timer1 is ticking");
        if (Properties.Settings.Default.bShapeDrawing == true)
        {
            Console.WriteLine("Form1 trying to draw");
            Graphics g = this.CreateGraphics();
            Pen selPen = new Pen(Color.Red, 2);

            if (Control.MouseButtons == MouseButtons.Right)
            {
                g.DrawRectangle(selPen, iWidth / 2 - 5, iHight / 2 - 5, 5, 5);
                Console.WriteLine("Form1 drawing complete");
            }
            else
            {
                this.Invalidate();
            }
        }
        else
        {
            // Drawing deactivated
        }
    }
}

0 个答案:

没有答案