I'm working on some homework and need to create an application that draws 3 shapes when the form is clicked. The objects are suppose to be drawn on specified surface area within the form. I have the surface area being created but cannot seem to get a shape to be drawn onto it when clicked. I'm thinking that there is a problem with my event handling of the mouse click but I cannot seem to figure out how to fix it. I have included my code and would appreciate any help or direction that you guys could provide me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace William_Cappoli_IT466_Unit_6_Project
{
public partial class frmMain : Form
{
private DrawImage imgDraw;
public frmMain()
{
imgDraw = new DrawImage();
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs paintEvnt)
{
// Get the graphics object
Graphics gfx = paintEvnt.Graphics;
// calls draw method from DrawImage class and passes it the graphics object
imgDraw.DrawBoundingBox(gfx);
}
private void frmMain_Click(PaintEventArgs e)
{
Graphics imgObj = e.Graphics;
imgDraw.Draw(imgObj);
}
} // end partial class frmMain class
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class DrawImage
{
// private members that determine where drawing surface starts, how big it is and the color of it
private float xCoordinate = 0;
private float yCoordinate = 60;
private float width = 521;
private float height = 560;
private Color backcolor = Color.White;
// creates surface for drawing objects onto
public void DrawBoundingBox(Graphics g)
{
// Create a new pen that we shall use for drawing the line
Pen myPen = new Pen(Color.Black);
SolidBrush myBrush = new SolidBrush(backcolor);
g.DrawRectangle(myPen, xCoordinate, yCoordinate, width, height);
g.FillRectangle(myBrush, xCoordinate, yCoordinate, width, height);
} // end DrawBoundingBox method
// draws rectangle when form is clicked
public void Draw(Graphics o)
{
Pen imgPen = new Pen(Color.Black);
o.DrawRectangle(imgPen, 90, 100, 45, 75);
}
} // end DrawImage class
Thanks in advance
Loading
Bechir BejaouiPosted Dec 17, 2008, 5:12 PM
thanks
Bill CappoliPosted Dec 16, 2008, 11:17 PM
Bechir BejaouiPosted Dec 16, 2008, 7:09 PM
private void Form1_Load(object sender, EventArgs e)
{
Form1.ActiveForm.Paint += new PaintEventHandler(Form1_Paint);
}
Graphics myGraphics;
private void Form1_Paint(object sender, PaintEventArgs e)
{
myGraphics = e.Graphics;
Rectangle rec = new Rectangle(new Point(10,10),new Size(250,150));
Brush brush = new SolidBrush(Color.Red);
myGraphics.FillRectangle(brush, rec);
}