Hi, I'm trying to figure out how to get, and manipulate mouse clicks and keystrokes within a Windows Applet in order to paint something on a canvas. This is what I have so far:
using
System;using
System.Windows.Forms;using
System.Drawing;namespace
WindowsApplication1{
public class Program : Form1
{
public Program()
{
this.Paint += new PaintEventHandler (f1_Paint);
}
private void f1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush Back = new SolidBrush(Color.LightYellow);
g.FillRectangle(Back, 0, 0, 350, 550);
}
static void Main()
{
Application.Run(new Program());
}
}
}
This creates a default background of light yellow with the defined borders. What I'm trying to find is:
1) How to set the form size to a defined state bigger than the background, because at the moment I'm clicking and dragging things around to make it the size I'd like it to be.
2) How to read a mouseclick and mouseheld event so that (for now) it draws a black brush type based on the x and y coordinates that I obtain from the click.
I also intend to use this base background as a sort of set border, where I'll have a predrawn object circle, where if arrow keys are pressed, it will move the circle around.
I'm also using C# Express 2005 Edition, and have very little experience programming. Thank you in advance for any help.
J ElasiguePosted Mar 25, 2008, 2:27 PM
Paul LoughridgePosted Mar 20, 2008, 4:57 PM
Try this. Put a button on your form name btnExit text Exit
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
GDIPlus{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
//Form size this.ClientSize = new System.Drawing.Size(800, 800); // width ^^^ ^^^ height //Form location and settings this.Top = 20; this.Left = 20; this.Height = 600; this.Width = 550; this.MaximizeBox = false; this.MinimizeBox = false; this.BackColor = System.Drawing.Color.Aquamarine; //Size buttons 80 x30btnExit.Height = 30;
btnExit.Width = 80;
//Locate buttonsbtnExit.Left =
this.Width - 100;btnExit.Top =
this.Height - 74;}
private void btnExit_Click(object sender, EventArgs e){
Close();
}
//This runs as soon as you start. private void Form1_Paint(object sender, PaintEventArgs e){
//Define variables top, bottom, left, width, height, right and linelength int itop = 20; int ibottom = 520; int ileft = 20; int iwidth = 500; int iheight = iwidth; //width and height are the same int iright = ileft + iwidth; int ilinelength = iwidth + 20; Graphics g = e.Graphics; //create pens. 4 and 8 are the width of the pen Pen myPen = new Pen(Color.Black, 4); Pen myPen2 = new Pen(Color.Black, 8); //Rectangleg.FillRectangle(
Brushes.BurlyWood, ileft, itop, iwidth, iheight); //Lines are all the same length since they are the borders of a square //Line on leftg.DrawLine(myPen, ileft, itop, ileft, ilinelength);
//Line on rightg.DrawLine(myPen, iright, itop, iright, ilinelength);
//Line on topg.DrawLine(myPen, ileft, itop, ilinelength, itop);
//Line on bottomg.DrawLine(myPen, ileft, ibottom, ilinelength, ibottom);
//Other shapes. Use a different color brush for the Fill //Ellipse //Draw a wide black border around the ellipseg.DrawEllipse(myPen2, 120, 70, 300, 150);
g.DrawEllipse(
Pens.Black, 120, 70, 300, 150);g.FillEllipse(
Brushes.Chartreuse, 120, 70, 300, 150); //left, top, width, height //create a new solid brush. Dispose of when you it has been used. SolidBrush myBrush = new SolidBrush(Color.OrangeRed); Font fnt = new Font("Verdana", 16); //font, size Font fnt1 = new Font("TimesNewRoman", 24); //font, size //Draw text inside the Ellipse defining a SolidBrushg.DrawString(
"GDI+ World", fnt, new SolidBrush(Color.Red), 200, 120); // left,top //Rectangle //Draw a wide black border around the Rectangleg.DrawRectangle(myPen2, 70, 300, 140, 140);
g.DrawRectangle(
Pens.Red, 70, 300, 140, 140);g.FillRectangle(
Brushes.Khaki, 70, 300, 140, 140); // left, top, width, height //Draw text inside Rectangle using fnt youdefined. Notice the effect of spaces in the string.g.DrawString(
"Orange" + (char)13 + (char)10 + " Red Text", fnt, myBrush, 80, 320); // left,top //Circle //Draw a wide black border around the Circleg.DrawEllipse(myPen2, 315, 300, 140, 140);
g.DrawEllipse(
Pens.Black, 315, 300, 140, 140);g.FillEllipse(
Brushes.Aqua, 315, 300, 140, 140); //left, top, width, height //Draw text inside the Circle. Notice the effect of spaces in the string.g.DrawString(
" This is a" + (char)13 + (char)10 + " Circle", fnt, new SolidBrush(Color.Navy), 330, 340); // left,top //you are done with the objects you created so DISPOSE of themmyBrush.Dispose();
myPen.Dispose();
}
}
}