Click to See Complete Forum and Search --> : Draw on form
code?
January 27th, 2009, 09:33 PM
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.
dglienna
January 27th, 2009, 11:50 PM
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:
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
toraj58
January 28th, 2009, 01:55 AM
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.
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.
code?
February 3rd, 2009, 03:17 PM
Toraj +1
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.