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