Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Drawing;
- using System.Windows.Forms;
- /*
- Created by: CodeAssassin
- Date: 10/02/2016
- Use: To create a simple draw mechanics example using GDI and a panel
- */
- namespace SimpleDraw_Panel
- {
- internal class SimpleDraw
- {
- private Panel _p;
- private bool MouseIsDown = false;
- private Graphics G;
- public SimpleDraw(Panel p)
- {
- if (p != null)
- {
- _p = p;
- _p.MouseDown += _p_MouseDown;
- _p.MouseUp += _p_MouseUp;
- _p.MouseMove += _p_MouseMove;
- Clear();
- }
- }
- public void Clear()
- {
- if (_p != null)
- {
- G = _p.CreateGraphics();
- G.Clear(Color.White);
- G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
- G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
- G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- _p.Invalidate();
- }
- }
- private void Draw(MouseEventArgs e)
- {
- if (G != null)
- {
- G.DrawRectangle(Pens.Black, new Rectangle(e.X, e.Y, 1, 1));
- }
- }
- private void _p_MouseMove(object sender, MouseEventArgs e)
- {
- if (MouseIsDown)
- {
- Draw(e);
- }
- }
- private void _p_MouseUp(object sender, MouseEventArgs e)
- {
- MouseIsDown = false;
- }
- private void _p_MouseDown(object sender, MouseEventArgs e)
- {
- Draw(e);
- MouseIsDown = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement