2013-07-30 39 views
0

我使用面板創建UI(System.Windows.Forms.Panel),其中將包含矩形/橢圓,並且形狀的大小(寬度/高度)取決於水平和垂直滑塊。下面的代碼在某種程度上確實達到了期望的行爲。但是,表示沒有Style的面板中心的線條工作正常,而DashDotDash的樣式會在面板的對角線和邊線上繪製線條,而不是指定的點(startPoint,endPoint)。有沒有辦法根據面板的大小對矩形進行居中?而不是使用的WinForms ShapeContainers/RectangleShape/EllipseShape -Windows窗體中的DashDot /短劃線樣式繪製不正確,Panels與ShapeContainers

this.panel.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_Paint); 

此外,作爲新的C#,我應該使用Micorsoft.VisualBasic.Powerpack

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事件被註冊爲如下。面板?

+0

使用PaintEventArgs.ClipRectangle是*不正確。這是不可預知的,它告訴你客戶區域需要塗漆的部分。這與您想要顯示這些行的位置無關。目前還不清楚你希望他們在哪裏。 –

+1

我強烈建議WPF作爲默認的Windows桌面UI技術。它對任何事物都有更大的支持,並且更容易處理,如果你需要高級圖形的話,更是如此。 winforms是一種真正過時的技術,不再推薦用於任何新項目,僅用於維護傳統應用程序。 –

回答

0

這應該是正確的:

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); 
    } 
} 

這應該是如何使它與ClipRectangle工作。或者你可以忽略它們,正如漢斯指出的那樣,可能在這個簡單的應用程序中它不會造成任何減速。