在 C# 中使用方法时如何显示控件(例如RadioButton
等)?可以在构造函数中创建自定义控件,但我需要使用 C# 的常用控件吗?请建议。Button
OnPaint
我正在尝试显示常用控件但没有用。请找到下面的代码。
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)
{
}
}
}