2017-01-12 4 views
1

J'ai besoin de dessiner avec un graphique GDI un cercle sur mon formulaire dans WPF. Je ne peux pas le faire avec des formulaires Windows, donc j'ai ajouté un en utilisant. Je ne peux pas utiliser les contrôles Elipse de WPF. Mon professeur m'a dit de faire ça comme ça.Dessiner un cercle avec des graphismes GDI sur WPF

Ceci est mon code:

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

Et ceci est l'erreur:

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

"Je dois dessiner avec graphiques GDI un cercle sur ma forme WPF". Pour quelle raison? Pourquoi ne pouvez-vous pas utiliser un contrôle WPF Ellipse? – Clemens

+0

C'est l'une des exigences de ma mission. Je ne sais pas pourquoi mon professeur veut cela. @Clemens – Gigitex

+1

Je suppose que vous avez mal compris cette mission et vous êtes censé faire cela dans WinForms. – LarsTech

Répondre

2

Vous ne pouvez pas utiliser GDI au sein WPF directement, pour obtenir ce dont vous avez besoin, s'il vous plaît utiliser WindowsFormsHost. Ajouter des références à System.Windows.Forms et WindowsFormsIntegration, l'ajouter à XAML comme celui-ci (devrait avoir quelque chose à l'intérieur, comme panneau ou autre):

<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> 

Ensuite, votre code-behind ressemblera à ceci et vous serez 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: bonne idée d'utiliser la déclaration using ici:

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