【问题标题】:How to Display Controls when using OnPaint Method in C# [closed]在 C# 中使用 OnPaint 方法时如何显示控件 [关闭]
【发布时间】:2016-06-06 09:19:22
【问题描述】:

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

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

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)
        {
        }
    }

}

【问题讨论】:

  • 不确定你想要什么。如果您只想在您正在运行的系统中绘制看起来像常规控件的东西,您可以使用众多 Renderer 类,例如CheckBoxRenderer.DrawCheckBox(..) 或使用ControlPaint..
  • 我认为GlassForm 这个名字是一个提示。问题是黑色文本被渲染为透明,因此控件看起来不对?如果是这样,您应该在问题中描述这一点,而不是问一个不相关的问题(每个人都在回答的问题)。

标签: c# onpaint common-controls


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    如果你想自己绘制控件,你可以在.net框架中搜索一个xxxRenderer类。通过使用这些,您可以绘制check boxesradio buttonsnormal buttons 等。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多