[C#] MDI child et MDI parent : Laison

MickParadiseLost Messages postés 86 Date d'inscription samedi 26 février 2005 Statut Membre Dernière intervention 2 avril 2008 - 12 juil. 2005 à 11:23
aogie Messages postés 108 Date d'inscription vendredi 11 juillet 2003 Statut Membre Dernière intervention 27 novembre 2007 - 14 juil. 2005 à 17:08
Bonjour a tous,

J'ai une barre d'outils dans ma forme principale et elle contient un pinceau et une gomme.

J'aimerai pouvoir peindre avec mon pinceau dans la mdi child.

Comment on fait svp ?

5 réponses

aogie Messages postés 108 Date d'inscription vendredi 11 juillet 2003 Statut Membre Dernière intervention 27 novembre 2007
12 juil. 2005 à 12:07
Bonjour,



Quand ton bouton de pinceau est enfoncé, une variable de la mainform
indiquera donc qu'on se trouve en mode dessin avec pinceau (ex :
penDrawing = true) et une autre variable contiendra le pinceau
paramétré par l'utilisateur (configuration de la couleur et de
l'épaisseur de trait).



Ensuite, pour le dessin à main levée, il faut surcharger OnMouseMove sur la feuille enfant.

Vérifier que e.Button == MouseButtons.Left. Si c'est le cas, tu mets à
jour par la méthode SetPixel() un bitmap conservé en variable membre,
puis :

- si tu ne travailles pas avec un PictureBox : appel de la méthode
Invalidate(). Dans la surcharge OnPaint(), tu n'as plus qu'à dessiner
le dessin.

ou

- si tu travailles avec un PictureBox : mise à jour de la propriété Image.

-- AOGie --
0
MickParadiseLost Messages postés 86 Date d'inscription samedi 26 février 2005 Statut Membre Dernière intervention 2 avril 2008
12 juil. 2005 à 23:16
Oui je travaille sans picturebox parsque j'arrive toujours pas a le faire marcher sinon.

Mais euh je suis vraiment débutant en c# et réaliser ce que tu viens de
me dire c'est impossible pour moi car je comprend bien le principe mais
pour coder les controles utilisateurs, le fait de cliquer sur un bouton
sa provoque une variable, je comprend pas lol.

Parsque en fait, je veux que quand je clique sur le pinceau il se passe:

-la fleche de la souris se transforme en rond.

-dans un panel apparait un champ de saisie pour modifier le diametre du rond de la souris.

-dans ce panel apparait aussi une boite de couleurs (pas d'influence
sur le pointeur de souris mais la couleur du trait sera celle de la
couleur choisie)

-lorsque je clique sur la mdichild a l'endroit du dessin, le trait se
fait comme un vrai pinceau. (quand on a le bouton de souris enfoncé on
dessine, lorsqu'il ne l'est pas sa s'arréte).

si tu peux me faire un exemple sa serait génial et j'aurais pu qu'a le faire pour les autres outils de ma boite a outils.

Merci d'avance
0
aogie Messages postés 108 Date d'inscription vendredi 11 juillet 2003 Statut Membre Dernière intervention 27 novembre 2007
13 juil. 2005 à 09:51
Je viens de te faire un exemple pour le dessin à main levée. Pour
changer la couleur du trait, tu double cliques sur le carré de couleur :



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace TestPaint

{

/// <summary>

/// Description résumée de Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private Bitmap

_bmp = null;

private Pen


_pen = null;

private Point

_lastPt =
Point.Empty;



private System.Windows.Forms.CheckBox chk_Pen;

private System.Windows.Forms.Panel pan_Draw;

private System.Windows.Forms.ColorDialog colorDlg;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label lbl_Color;

private System.Windows.Forms.ComboBox cb_PenWidth;

private System.Windows.Forms.Label label2;

/// <summary>

/// Variable nécessaire au concepteur.

/// </summary>

private System.ComponentModel.Container components = null;



public Form1()

{

//

// Requis pour la prise en charge du Concepteur Windows Forms

//

InitializeComponent();



_bmp = new Bitmap(pan_Draw.Width, pan_Draw.Height);

_pen = new Pen(Color.Black);

lbl_Color.BackColor = Color.Black;



for (int i = 1; i < 5; i++)

cb_PenWidth.Items.Add(i);

cb_PenWidth.SelectedIndex = 0;

}



/// <summary>

/// Nettoyage des ressources utilisées.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (_bmp != null) _bmp.Dispose();

if (_pen != null) _pen.Dispose();



if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



#region Code généré par le Concepteur Windows Form

/// <summary>

/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas

/// le contenu de cette méthode avec l'éditeur de code.

/// </summary>

private void InitializeComponent()

{

this.chk_Pen = new System.Windows.Forms.CheckBox();

this.pan_Draw = new System.Windows.Forms.Panel();

this.colorDlg = new System.Windows.Forms.ColorDialog();

this.label1 = new System.Windows.Forms.Label();

this.lbl_Color = new System.Windows.Forms.Label();

this.cb_PenWidth = new System.Windows.Forms.ComboBox();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// chk_Pen

//

this.chk_Pen.Appearance = System.Windows.Forms.Appearance.Button;

this.chk_Pen.BackColor = System.Drawing.SystemColors.Control;

this.chk_Pen.Location = new System.Drawing.Point(8, 8);

this.chk_Pen.Name = "chk_Pen";

this.chk_Pen.Size = new System.Drawing.Size(64, 24);

this.chk_Pen.TabIndex = 0;

this.chk_Pen.Text = "Pinceau";

this.chk_Pen.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;


this.chk_Pen.CheckedChanged += new
System.EventHandler(this.chk_Pen_CheckedChanged);

//

// pan_Draw

//


this.pan_Draw.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.pan_Draw.BackColor = System.Drawing.Color.White;

this.pan_Draw.Location = new System.Drawing.Point(0, 37);

this.pan_Draw.Name = "pan_Draw";

this.pan_Draw.Size = new System.Drawing.Size(456, 280);

this.pan_Draw.TabIndex = 1;


this.pan_Draw.SizeChanged += new
System.EventHandler(this.pan_Draw_SizeChanged);


this.pan_Draw.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.pan_Draw_MouseUp);


this.pan_Draw.Paint += new
System.Windows.Forms.PaintEventHandler(this.pan_Draw_Paint);


this.pan_Draw.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.pan_Draw_MouseMove);

//

// label1

//

this.label1.Location = new System.Drawing.Point(88, 16);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(56, 16);

this.label1.TabIndex = 3;

this.label1.Text = "Couleur :";

//

// lbl_Color

//

this.lbl_Color.Location = new System.Drawing.Point(144, 8);

this.lbl_Color.Name = "lbl_Color";

this.lbl_Color.Size = new System.Drawing.Size(24, 23);

this.lbl_Color.TabIndex = 0;


this.lbl_Color.DoubleClick += new
System.EventHandler(this.lbl_Color_DoubleClick);

//

// cb_PenWidth

//


this.cb_PenWidth.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;

this.cb_PenWidth.Location = new System.Drawing.Point(256, 11);

this.cb_PenWidth.Name = "cb_PenWidth";

this.cb_PenWidth.Size = new System.Drawing.Size(48, 21);

this.cb_PenWidth.TabIndex = 4;


this.cb_PenWidth.SelectedIndexChanged += new
System.EventHandler(this.cb_PenWidth_SelectedIndexChanged);

//

// label2

//

this.label2.Location = new System.Drawing.Point(184, 16);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(64, 16);

this.label2.TabIndex = 5;

this.label2.Text = "Epaisseur :";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackColor = System.Drawing.SystemColors.Control;

this.ClientSize = new System.Drawing.Size(456, 317);

this.Controls.Add(this.label2);

this.Controls.Add(this.cb_PenWidth);

this.Controls.Add(this.lbl_Color);

this.Controls.Add(this.label1);

this.Controls.Add(this.pan_Draw);

this.Controls.Add(this.chk_Pen);

this.Name = "Form1";


this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Form1";

this.ResumeLayout(false);



}

#endregion



/// <summary>

/// Point d'entrée principal de l'application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}



private void chk_Pen_CheckedChanged(object sender, System.EventArgs e)

{

Cursor = (chk_Pen.Checked ? Cursors.Cross : Cursors.Default);

}



#region Evénements pan_Draw



#region pan_Draw_MouseMove

private void pan_Draw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if (e.Button == MouseButtons.Left && chk_Pen.Checked)

{

Point mouse = new Point(e.X, e.Y);

if (_lastPt Point.Empty) _lastPt mouse;



float penWidth = _pen.Width;

using (Graphics g = Graphics.FromImage(_bmp))

{


g.DrawLine(_pen, _lastPt,
mouse);

}



//création du rectangle ajusté, de modification

Rectangle rect = new Rectangle(

Math.Min(_lastPt.X, mouse.X),

Math.Min(_lastPt.Y, mouse.Y),

Math.Abs(_lastPt.X - mouse.X),

Math.Abs(_lastPt.Y - mouse.Y)

);

rect.Inflate(5, 5);

pan_Draw.Invalidate(rect, false);



_lastPt = mouse;

}

}

#endregion



#region pan_Draw_SizeChanged

private void pan_Draw_SizeChanged(object sender, System.EventArgs e)

{

//modification du dessin interne

Bitmap bmp = new Bitmap(pan_Draw.Width, pan_Draw.Height);

using (Graphics g = Graphics.FromImage(bmp))

{

g.DrawImage(_bmp, 0, 0);

}

_bmp = bmp;

}

#endregion



#region pan_Draw_Paint

private void pan_Draw_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

//on dessine uniquement la partie modifiée

e.Graphics.DrawImage(

_bmp,

e.ClipRectangle,

e.ClipRectangle,

GraphicsUnit.Pixel

);

}

#endregion



#region pan_Draw_MouseUp

private void pan_Draw_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

//initialisation du dernier point

_lastPt = Point.Empty;

}

#endregion



#endregion



private void lbl_Color_DoubleClick(object sender, System.EventArgs e)

{

//couleur originale

colorDlg.Color = _pen.Color;

if (colorDlg.ShowDialog() == DialogResult.OK)

{

//assignation de la nouvelle couleur

_pen.Color = colorDlg.Color;

lbl_Color.BackColor = _pen.Color;

}

}



private void cb_PenWidth_SelectedIndexChanged(object sender, System.EventArgs e)

{

//décalage de 1 entre index et valeur affichée

_pen.Width = cb_PenWidth.SelectedIndex + 1;

}

}

}


-- AOGie --
0
MickParadiseLost Messages postés 86 Date d'inscription samedi 26 février 2005 Statut Membre Dernière intervention 2 avril 2008
13 juil. 2005 à 22:45
Oh merci beaucoup pour l'exemple Aogie.

Je vais éssayer d'adapter ceci a mon projet avec mon MDI child.

éh euh toi tu dessine dans un panel, mais moi je dessine directement sur ma fenetre. ça change grand chose ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
aogie Messages postés 108 Date d'inscription vendredi 11 juillet 2003 Statut Membre Dernière intervention 27 novembre 2007
14 juil. 2005 à 17:08
Non, c'est exactement pareil.
Bon code,

-- AOGie --
0
Rejoignez-nous