C# Windows窗体中的虚线/虚线样式绘制错误,面板与ShapeContainers

C# Windows窗体中的虚线/虚线样式绘制错误,面板与ShapeContainers,c#,winforms,visual-studio-2010,C#,Winforms,Visual Studio 2010,我正在创建一个带有面板(System.Windows.Forms.panel)的UI,该面板将包含一个矩形/椭圆,形状的大小(宽度/高度)取决于水平和垂直滑块。下面的代码在某种程度上实现了所需的行为。但是,表示没有样式的面板中心的线可以正常工作,而DashDot或DashDot样式会导致在面板的对角线和侧面绘制线,而不是指定点(起点、终点)。有没有办法根据面板的大小将矩形居中 private void vScroll_Scroll(object sender, ScrollEventArgs e

我正在创建一个带有面板(
System.Windows.Forms.panel
)的UI,该面板将包含一个矩形/椭圆,形状的大小(宽度/高度)取决于水平和垂直滑块。下面的代码在某种程度上实现了所需的行为。但是,表示没有样式的面板中心的线可以正常工作,而
DashDot
DashDot
样式会导致在面板的对角线和侧面绘制线,而不是指定点(起点、终点)。有没有办法根据面板的大小将矩形居中

private void vScroll_Scroll(object sender, ScrollEventArgs e)
{
    this.vScrollValue.Text = vScrollBar.Value.ToString();
    panel.Invalidate(/*myRectangle*/);
}

private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    this.hScrollValue.Text = hScrollBar.Value.ToString();
    panel.Invalidate(/*myRectangle*/);
}

private void panel_Paint(object sender, PaintEventArgs e)
{
    myRectangle = new Rectangle(90, 90, hScrollBar.Value, vScrollBar.Value);
    System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
    messageBoxCS.AppendFormat("panel.Location.X = {0} panel.Location.Y = {1} (panel.Size.Height/2) = {2}", panel.Location.X, panel.Location.Y, e.ClipRectangle);
    //MessageBox.Show(messageBoxCS.ToString(), "Panel Paint");
    //Panel's midpoint (location(x,y) = 88,44, Size(x,y) = 182,184)
    Point startPoint = new Point(e.ClipRectangle.Location.X, e.ClipRectangle.Location.Y + (e.ClipRectangle.Size.Height / 2));
    Point endPoint = new Point(e.ClipRectangle.Location.X + e.ClipRectangle.Size.Width,     e.ClipRectangle.Location.Y + (e.ClipRectangle.Size.Height / 2));

    Pen dashRed = Pens.Red;
    e.Graphics.DrawRectangle(Pens.Black, myRectangle);
    //dashRed.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
    e.Graphics.DrawLine(Pens.Red, startPoint, endPoint); 
}
InitializeComponent()
方法中,paintHandler事件注册如下:

 this.panel.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_Paint);
另外,作为C#的新手,我是否应该使用Micorsoft.VisualBasic.Powerpack-ShapeContainers/RectangleShape/EllipseShape而不是使用WinForms.Panel?

这应该是正确的:

private void panel_Paint(object sender, PaintEventArgs e)
{
    myRectangle = new Rectangle(90, 90, hScrollBar.Value, vScrollBar.Value);
    System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
    messageBoxCS.AppendFormat("panel.Location.X = {0} panel.Location.Y = {1} (panel.Size.Height/2) = {2}", panel.Location.X, panel.Location.Y, e.ClipRectangle);
    //MessageBox.Show(messageBoxCS.ToString(), "Panel Paint");
    //Panel's midpoint (location(x,y) = 88,44, Size(x,y) = 182,184)
    e.Graphics.DrawRectangle(Pens.Black, myRectangle);
    if(e.ClipRectangle.Location.Y < this.Size.Height / 2 && e.ClipRectangle.Location.Y + e.ClipRectangle.Size.Height > this.Size.Height / 2)
    {
    Point startPoint = new Point(e.ClipRectangle.Location.X, this.Size.Height / 2);
    Point endPoint = new Point(e.ClipRectangle.Location.X + e.ClipRectangle.Size.Width,  this.Size.Height / 2);
    Pen dashRed = Pens.Red;
    dashRed.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
    e.Graphics.DrawLine(dashRed, startPoint, endPoint); 
    }  
}
private void panel\u Paint(对象发送器,PaintEventArgs e)
{
myRectangle=新矩形(90,90,hScrollBar.Value,vScrollBar.Value);
System.Text.StringBuilder messageBoxCS=新的System.Text.StringBuilder();
messageBoxCS.AppendFormat(“panel.Location.X={0}panel.Location.Y={1}(panel.Size.Height/2)={2}”,panel.Location.X,panel.Location.Y,e.ClipRectangle);
//Show(messageBoxCS.ToString(),“面板绘制”);
//面板中点(位置(x,y)=88,44,尺寸(x,y)=182184)
e、 Graphics.DrawRectangle(Pens.Black,myRectangle);
如果(e.ClipRectangle.Location.Ythis.Size.Height/2)
{
点开始点=新点(e.ClipRectangle.Location.X,this.Size.Height/2);
点端点=新点(e.ClipRectangle.Location.X+e.ClipRectangle.Size.Width,this.Size.Height/2);
Pen dashRed=Pens.Red;
dashRed.DashStyle=System.Drawing.Drawing2D.DashStyle.DashDot;
e、 图形。绘制线(虚线、起点、终点);
}  
}

这应该是如何使它与ClipRectangle一起工作。或者你可以忽略它们,正如Hans指出的,可能在这个简单的应用程序中,它不会导致任何减速。

使用PaintEventArgs.ClipRectangle是不正确的。它是不可预测的,它告诉您客户区域的哪一部分需要涂漆。这与你想让这些行出现在哪里无关。不清楚您希望它们位于何处。我强烈建议将WPF作为默认的Windows桌面UI技术。它对所有东西都有更大的支持,而且使用起来更容易,如果你需要高级的图形工具,就更容易了。winforms是一种非常过时的技术,不再推荐用于任何新项目,只用于维护遗留应用程序。