如何绘制可以在 C# 中移动和调整大小的箭头形状
How can I draw an arrow shape which can be moved and resized in c#
我是 window 应用程序开发的新手,目前我正在处理流程图 winform 项目。我需要绘制一个如图所示的控件(从 MS word 复制,通过 Insert->Shapes->Block Arrows)。 [箭头形状]
[1]: https://i.stack.imgur.com/P7XBL.png
该箭头可以移动、调整大小和形状。
我已经寻找了一些解决方案,但大多数都是规则形状。我可以创建一个列表并将箭头的所有 7 个点都放到这个列表中,然后在这些点之间画线,一行一行地画线(Graphics::DrawImage)吗?如果是,那么下一个问题是,如何用纯色填充这个不规则形状?我怎样才能移动这个形状?如果重绘,旧形状怎么擦掉?
Current 我可以使用以下代码拖动和移动控件,但是对于这种不规则形状,如何开始?本项目为C++/CLR,基于.net,欢迎C#或C++/CLI idea
void MouseDown(Object^ sender, MouseEventArgs^ e)
{
selected_control = safe_cast<Control^>(sender);
is_draging = true;
drag_start_point = gcnew Point(e->X, e->Y);
selected_control->Capture = true;
}
void MouseUp(Object^ sender, MouseEventArgs^ e)
{
is_draging = false;
selected_control->Capture = false;
}
void MouseMove(Object^ sender, MouseEventArgs^ e)
{
if (is_draging)
{
selected_control->Left = Math::Max(0, e->X + selected_control->Left - drag_start_point->X);
selected_control->Top = Math::Max(0, e->Y + selected_control->Top - drag_start_point->Y);
}
}
好吧,搞砸了,CLI 不是命令行。
要在 Winforms 中绘制不规则形状,您将使用 GraphicsPath
。来自 MS Docs:
GraphicsPath path = new GraphicsPath();
Pen penJoin = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
path.StartFigure();
path.AddLine(new Point(50, 200), new Point(100, 200));
path.AddLine(new Point(100, 200), new Point(100, 250));
penJoin.LineJoin = LineJoin.Bevel;
e.Graphics.DrawPath(penJoin, path);
即您将使用 path.AddLine
绘制七条线来制作箭头形状。
GraphicsPath.FillPath
允许您使用画笔填充 GraphicsPath 的内部。
为了调整大小、重塑形状,研究各种 GraphicsPath“变换”方法。
选择时,会得到GraphicsPath的Region
,判断鼠标是否指向intersects。
我是 window 应用程序开发的新手,目前我正在处理流程图 winform 项目。我需要绘制一个如图所示的控件(从 MS word 复制,通过 Insert->Shapes->Block Arrows)。 [箭头形状] [1]: https://i.stack.imgur.com/P7XBL.png 该箭头可以移动、调整大小和形状。 我已经寻找了一些解决方案,但大多数都是规则形状。我可以创建一个列表并将箭头的所有 7 个点都放到这个列表中,然后在这些点之间画线,一行一行地画线(Graphics::DrawImage)吗?如果是,那么下一个问题是,如何用纯色填充这个不规则形状?我怎样才能移动这个形状?如果重绘,旧形状怎么擦掉?
Current 我可以使用以下代码拖动和移动控件,但是对于这种不规则形状,如何开始?本项目为C++/CLR,基于.net,欢迎C#或C++/CLI idea
void MouseDown(Object^ sender, MouseEventArgs^ e)
{
selected_control = safe_cast<Control^>(sender);
is_draging = true;
drag_start_point = gcnew Point(e->X, e->Y);
selected_control->Capture = true;
}
void MouseUp(Object^ sender, MouseEventArgs^ e)
{
is_draging = false;
selected_control->Capture = false;
}
void MouseMove(Object^ sender, MouseEventArgs^ e)
{
if (is_draging)
{
selected_control->Left = Math::Max(0, e->X + selected_control->Left - drag_start_point->X);
selected_control->Top = Math::Max(0, e->Y + selected_control->Top - drag_start_point->Y);
}
}
好吧,搞砸了,CLI 不是命令行。
要在 Winforms 中绘制不规则形状,您将使用 GraphicsPath
。来自 MS Docs:
GraphicsPath path = new GraphicsPath();
Pen penJoin = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
path.StartFigure();
path.AddLine(new Point(50, 200), new Point(100, 200));
path.AddLine(new Point(100, 200), new Point(100, 250));
penJoin.LineJoin = LineJoin.Bevel;
e.Graphics.DrawPath(penJoin, path);
即您将使用 path.AddLine
绘制七条线来制作箭头形状。
GraphicsPath.FillPath
允许您使用画笔填充 GraphicsPath 的内部。
为了调整大小、重塑形状,研究各种 GraphicsPath“变换”方法。
选择时,会得到GraphicsPath的Region
,判断鼠标是否指向intersects。