如何在C#中使用RadioButton
方法时显示控件(例如Button
,OnPaint
等)?可以在构造函数中创建自定义控件,但我需要使用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)
{
}
}
}
答案 0 :(得分:2)
如果我理解正确,你想要的东西看起来很像"正常"控制,但实际上只是图像。
如果是这样,你可以使用ControlPaint
类:它有很多方法来绘制按钮,checkBoxex,radiobuttons等。
大多数都需要Graphics参数,因此您可以在OnPaint事件处理程序中轻松调用它们
答案 1 :(得分:0)
如果要自己绘制控件,可以在.net框架中搜索xxxRenderer类。通过使用这些,您可以绘制check boxes,radio buttons,normal buttons等。
但请注意,在一些非常重要的工作中,要重现与内置控件(点击,悬停,对焦,键盘快捷键等)相同的行为。