winform绘制红点
时间: 2023-07-30 20:11:37 浏览: 47
你可以使用C#的Graphics类来绘制红点,以下是一个简单的示例代码:
```csharp
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen redPen = new Pen(Color.Red, 5);
SolidBrush redBrush = new SolidBrush(Color.Red);
int x = 50;
int y = 50;
int radius = 10;
g.DrawEllipse(redPen, x - radius, y - radius, 2 * radius, 2 * radius);
g.FillEllipse(redBrush, x - radius, y - radius, 2 * radius, 2 * radius);
}
```
在这个示例中,我们在窗体的Paint事件中使用Graphics类来绘制一个红色的圆点。首先,我们创建了一个红色的画笔和画刷,然后通过DrawEllipse方法绘制圆圈的轮廓,最后用FillEllipse方法填充圆圈的内部。通过这种方式,我们可以轻松地在WinForm应用程序中绘制一个红点。
相关问题
winform绘制标题栏
在WinForm中绘制标题栏可以通过以下步骤实现:
1. 设置窗体的属性为无边框属性,这可以通过设置窗体的`FormBorderStyle`属性为`None`来实现。
2. 在窗体的`OnPaint`事件中绘制标题栏部分。可以使用`Graphics`对象来绘制标题栏的背景、文字和按钮等元素。
3. 在绘制标题栏时,可以使用`ControlPaint`类提供的方法来绘制标准的标题栏背景和按钮。
4. 实现标题栏的基本功能,例如拖拽窗体、最小化、最大化和关闭等操作。可以通过处理鼠标事件来实现这些功能。
以下是一个示例代码,演示了如何在WinForm中绘制标题栏:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
public class CustomForm : Form
{
private const int CaptionHeight = 30; // 标题栏高度
private bool isDragging = false; // 是否正在拖拽窗体
private Point dragStartPoint; // 拖拽起始点
public CustomForm()
{
// 设置窗体的属性为无边框属性
FormBorderStyle = FormBorderStyle.None;
// 设置窗体的大小和位置
Size = new Size(400, 300);
Location = new Point(100, 100);
// 注册鼠标事件
MouseDown += CustomForm_MouseDown; MouseMove += CustomForm_MouseMove;
MouseUp += CustomForm_MouseUp;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 绘制标题栏背景
e.Graphics.FillRectangle(Brushes.LightBlue, 0, 0, Width, CaptionHeight);
// 绘制标题文字
string caption = Text;
Font captionFont = new Font(Font.FontFamily, 12, FontStyle.Bold);
SizeF captionSize = e.Graphics.MeasureString(caption, captionFont);
float captionX = (Width - captionSize.Width) / 2;
float captionY = (CaptionHeight - captionSize.Height) / 2;
e.Graphics.DrawString(caption, captionFont, Brushes.Black, captionX, captionY);
// 绘制关闭按钮
Rectangle closeButtonRect = new Rectangle(Width - 30, 0, 30, CaptionHeight);
ControlPaint.DrawCaptionButton(e.Graphics, closeButtonRect, CaptionButton.Close, ButtonState.Normal);
}
private void CustomForm_MouseDown(object sender, MouseEventArgs e)
{
// 判断鼠标是否按下在标题栏范围内
if (e.Y <= CaptionHeight)
{
isDragging = true;
dragStartPoint = e.Location;
}
}
private void CustomForm_MouseMove(object sender, MouseEventArgs e)
{
// 如果正在拖拽窗体,则移动窗体的位置
if (isDragging)
{
Point dragOffset = new Point(e.X - dragStartPoint.X, e.Y - dragStartPoint.Y);
Location = new Point(Location.X + dragOffset.X, Location.Y + dragOffset.Y);
}
}
private void CustomForm_MouseUp(object sender, MouseEventArgs e)
{
// 停止拖拽窗体
isDragging = false;
}
}
public class Program
{
[STAThread]
public static void Main()
{
Application.Run(new CustomForm());
}
}
```
这段代码创建了一个自定义的窗体类`CustomForm`,继承自`Form`类。在`CustomForm`类中,重写了`OnPaint`方法来绘制标题栏的背景和文字,并处理了鼠标事件来实现窗体的拖拽功能。
winform绘制多边形
在WinForms中绘制多边形可以使用System.Drawing命名空间中的Graphics对象的DrawPolygon方法。
以下是一个简单的示例代码,展示如何在WinForms中绘制一个五边形:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormsPolygonExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 创建Graphics对象
Graphics graphics = e.Graphics;
// 创建画笔和填充颜色
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(Color.Red);
// 定义多边形的顶点
Point[] points = new Point[]
{
new Point(100, 100),
new Point(200, 50),
new Point(300, 100),
new Point(250, 200),
new Point(150, 200)
};
// 绘制多边形
graphics.DrawPolygon(pen, points);
graphics.FillPolygon(brush, points);
// 释放资源
pen.Dispose();
brush.Dispose();
graphics.Dispose();
}
}
}
```
你可以将上述代码添加到WinForms应用程序的窗体类中,然后在窗体的OnPaint事件处理方法中进行绘制操作。这个示例绘制了一个红色的五边形,并使用黑色画笔进行描边。
请注意,这只是一个简单的示例,你可以根据需要自定义多边形的顶点和颜色等属性。