-3

在 C# 中使用方法时如何显示控件(例如RadioButton等)?可以在构造函数中创建自定义控件,但我需要使用 C# 的常用控件吗?请建议。ButtonOnPaint

我正在尝试显示常用控件但没有用。请找到下面的代码。

private void PaintPanelOrButton(object sender, PaintEventArgs e)
    {

        Point pt1 = new Point(radioButton1.Left + (radioButton1.Width / 2), radioButton1.Top + (radioButton1.Height / 2));
        Point pt2 = new Point(radioButton2.Left + (radioButton2.Width / 2), radioButton2.Top + (radioButton2.Height / 2));

        if (sender is Button)
        {
            Button btn = (Button)sender;
            pt1.X -= btn.Left;
            pt1.Y -= btn.Top;
            pt2.X -= btn.Left;
            pt2.Y -= btn.Top;
        }

        e.Graphics.DrawLine(new Pen(Color.Red, 4.0F), pt1, pt2);
    }

    public GlassForm()
    {
        TextBox t = new TextBox();
        t.Left = 1000;
        t.Top = 900;
        t.Name = "txt1";
        this.Controls.Add(t);

        this.SuspendLayout();
        Button buttonOK = new Button();
        buttonOK.Location = new Point(10, 10);
        buttonOK.Size = new Size(75, 25);
        buttonOK.Text = "OK";

        Button buttonCancel = new Button();
        buttonCancel.Location = new Point(90, 10);
        buttonCancel.Size = new Size(75, 25);
        buttonCancel.Text = "Cancel";

        this.Controls.AddRange(new Control[] { buttonOK, buttonCancel });
        this.ResumeLayout();
        this.TransparencyKey = transparentColor;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        try
        {

            base.OnPaint(e);


            Rectangle r1 = new Rectangle(10, 10, 50, 50);
            Rectangle r2 = new Rectangle(40, 40, 50, 50);
            Region r = new Region(r1);
            r.Union(r2);

            GraphicsPath path = new GraphicsPath(new Point[] {new Point(45, 45),
                                                    new Point(145, 55),
                                                    new Point(200, 150),
                                                    new Point(75, 150),
                                                    new Point(45, 45)
                                                   }, new byte[] {  (byte)PathPointType.Start,
                                                                    (byte)PathPointType.Bezier,
                                                                    (byte)PathPointType.Bezier,
                                                                    (byte)PathPointType.Bezier,
                                                                    (byte)PathPointType.Line
                                                                 });
            r.Union(path);

            using (Brush transparentBrush = new SolidBrush(transparentColor))
            {
                try
                {
                    BlurBehindWindowEnabled = true;
                    ExtendFrameEnabled = false;
                    if (ExtendFrameEnabled)
                    {
                        var glassMargins = this.GlassMargins;

                        NativeMethods.DwmExtendFrameIntoClientArea(this.Handle, ref glassMargins);

                        marginRegion = new Region(new Rectangle(10, 10, 600, 100));

                        e.Graphics.FillRegion(transparentBrush, marginRegion);
                    }
                    else
                    {
                        var glassMargins = new NativeMethods.MARGINS(-1);
                        NativeMethods.DwmExtendFrameIntoClientArea(this.Handle,
                           ref glassMargins);
                    }

                    if (BlurBehindWindowEnabled)
                    {
                        ResetDwmBlurBehind(true, e.Graphics);
                        e.Graphics.FillRegion(transparentBrush, r);
                    }
                    else
                    {
                        ResetDwmBlurBehind(false, null);
                    }
                }
                catch (Exception ex)
                {
                    lbAeroGlassStyleSupported.Text = "Error";
                    demoForm.Show();
                }
            }
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.WindowState = FormWindowState.Maximized;
            clsTaskbar.Show();
        }
        catch (Exception ex)
        {
        }
    }

}

4

2 回答 2

2

如果我理解正确,您想要的东西看起来与“正常”控件完全一样,但实际上只是图像。

如果是这样,你可以使用ControlPaint类:它有很多方法来绘制按钮、checkBoxex、单选按钮等。

它们中的大多数都需要 Graphics 参数,因此您可以轻松地从 OnPaint 事件处理程序中调用它们

于 2016-06-06T09:48:18.080 回答
0

如果你想自己绘制控件,你可以在.net框架中搜索一个xxxRenderer类。通过使用这些,您可以绘制复选框单选按钮普通按钮等。

但请注意,要重现与内置控件相同的行为(单击、悬停、聚焦、键盘快捷键等),需要做一些工作。

于 2016-06-06T13:29:51.400 回答