2017-01-12 21 views
1

WPFで自分のフォームに円を描く必要があります。 私はWindowsフォームでこれを行うことはできませんので、私はを使用して追加しました。 WPFからElipseコントロールを使用することはできません。私の先生はこう言ってくれました。WPFでGDIグラフィックスを使用して円を描く

これは私のコードです:

public void MakeLogo() 
{ 
    System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green); 
    System.Drawing.Graphics formGraphics = this.CreateGraphics(); 
    formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300)); 
    myBrush.Dispose(); 
    formGraphics.Dispose(); 
} 

そして、これは誤りです:

MainWindow' does not contain a definition for 'CreateGraphics' and no extension method 'CreateGraphics' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)

+0

"私はWPFでGDIグラフィックスと私のフォームに円を描く必要がある":良いアイデアはusingここに声明を利用することができます。いかなる理由で? WPF楕円コントロールを使用できないのはなぜですか? – Clemens

+0

私の割り当ての要件の1つです。なぜ私の先生がこれを望んでいるのかわかりません。 @Clemens – Gigitex

+1

私はあなたがこの課題を誤解していて、WinFormsでこれを行うことになっていると思います。 – LarsTech

答えて

2

あなたはWindowsFormsHostを使用してください、あなたが必要なものを達成するために、直接WPFの中にGDIを使用することはできません。 (パネルのようなまたは何、内部の何かを持っている必要がある)。このようにXAMLに追加し、のSystem.Windows.FormsとWindowsFormsIntegrationへの参照を追加します。

<Window x:Class="WpfApplication1.MainWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:local="clr-namespace:WpfApplication1" 
       mc:Ignorable="d" 
       xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
       Title="MainWindow" Height="350" Width="525"> 
     <!--whatever goes here--> 
     <WindowsFormsHost x:Name="someWindowsForm"> 
      <wf:Panel></wf:Panel> 
     </WindowsFormsHost> 
     <!--whatever goes here--> 
    </Window> 

次に、あなたのコードビハインドは次のようになります、あなたはなるだろうOK

SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green); 
    Graphics formGraphics = this.someWindowsForm.Child.CreateGraphics(); 
    formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300)); 
    myBrush.Dispose(); 
    formGraphics.Dispose(); 

UPD:

using (var myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green)) 
      { 
       using (var formGraphics = this.someForm.Child.CreateGraphics()) 
       { 
        formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300)); 
       } 
      } 
関連する問題