2013-11-29 40 views
0

我需要在Windows窗體中使用C#創建一些形狀對象,如圓形,線等。 添加了對.Net選項卡和使用System.Windows.Shapes的PresentationFramework的引用。 但創建線對象後,它不顯示在Windows窗體中。線對象不顯示在Windows窗體應用程序

請參見下面的示例代碼: 代碼形式的Load事件:

Line myline = new Line(); 
myline.X1 = 100; 
myline.X2 = 300; 
myline.Y1 = 300; 
myline.Y2 = 300; 
myline.Stroke = System.Windows.Media.Brushes.LightSteelBlue; 
myline.StrokeThickness = 2; 

請讓我知道錯了,我在裏面做什麼。

+0

你的標籤顯示Winforms,但你使用的是WPF - 你實際使用了哪個框架? – NoChance

+0

僅在Windows窗體中使用Shape類時添加了對WPF的引用。我創建了Windows Form Application而不是WPF應用程序。 – user1291401

+1

您是否期望能夠在不使用'ElementHost'容器的情況下在Winform上使用Wpf? –

回答

0

你忘了更新圖紙的要求,你是剛剛定義的直線無圖紙,它實際上

編輯:

public void drawmyline() 
     { 
      System.Drawing.Graphics example; 

      // Create pen. 
      Pen blackPen = new Pen(Color.Black, 3); 

      // Create points that define line. 
      Point point1 = new Point(100, 100); 
      Point point2 = new Point(400, 400); 
      example = this.CreateGraphics(); 
      // Draw line to screen. 
      example.DrawLine(blackPen, point1, point2); 

      //e.Graphics.DrawLine(blackPen, point1, point2); 
     } 

那麼,你從你想要的任何地方調用你的函數,例如按一下按鈕

private void button4_Click_1(object sender, EventArgs e) 
     { 
      drawmyline(); 
     } 

請註明所接受,如果它幫你

+1

示例代碼plz – user1291401

+0

@ user1291401不要忘記'使用System.Drawing;' – chouaib

相關問題