CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Draw on form

  1. #1
    Join Date
    May 2006
    Posts
    306

    Draw on form

    Simply, how do i draw on a form?

    I googled, but not what i wanted. I thought I had a link or sample project, but apparently I lost it.

    Like MS paint, but just straight onto the form. Not any line making or shapes, like free hand.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Draw on form

    You don't ever draw on the form. You do everything related to the screen in the Form_Refresh() Event of your Form, or PB if that's what you want to draw on.

    Once you get the hang of it, it works pretty well.

    To draw in real-time, you can use a timer:

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ' Add another row to the grid and update the screen.
            matrix.AddRow()
            matrix.Draw(Me.PictureBox1.CreateGraphics(), Me.PictureBox1.BackColor)
        End Sub
    D:\VisualStudio2008\VB RTM Samples\Application Samples\Game
    Last edited by dglienna; January 28th, 2009 at 01:04 AM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Draw on form

    i wrote a simple code for you using GDI+ that paint a blue line on the form by overriding OnPaint and you can draw curves with red color in the form also.

    here is the code.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace DrawLine
    {
        public partial class Form1 : Form
        {
            bool isDrawing = false;
            Point prevLoc;
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Graphics g = e.Graphics;
                Pen RedPen = new Pen(Color.Blue, 3);
                Point[] p = { new Point(20, 20), new Point(100, 100) };
                g.DrawLines(RedPen, p);
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                Pen p = new Pen(Color.Red, 2);
                if (isDrawing)
                {
                    using (Graphics g = this.CreateGraphics())
                    {
                        g.DrawLine(p, prevLoc, e.Location);
                        prevLoc = e.Location;
                    }
                }
                p.Dispose();
    
            }
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                isDrawing = true;
                prevLoc = e.Location;
            }
    
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                isDrawing = false;
            }
        }
    }
    i have attached a sample image.
    Note: my code is not an ultimate solution i spent a few minutes on it just to give you the clue.
    Attached Images Attached Images
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    May 2006
    Posts
    306

    Re: Draw on form

    Toraj +1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured