Click here to Skip to main content
15,885,435 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: paint application question Pin
J. Dunlap4-May-03 18:57
J. Dunlap4-May-03 18:57 
GeneralRe: paint application question Pin
lfsong4-May-03 22:52
lfsong4-May-03 22:52 
GeneralRe: paint application question Pin
James T. Johnson4-May-03 19:05
James T. Johnson4-May-03 19:05 
GeneralRe: paint application question Pin
J. Dunlap4-May-03 19:36
J. Dunlap4-May-03 19:36 
GeneralRe: paint application question Pin
James T. Johnson4-May-03 20:24
James T. Johnson4-May-03 20:24 
GeneralRe: paint application question Pin
J. Dunlap4-May-03 20:29
J. Dunlap4-May-03 20:29 
GeneralRe: paint application question Pin
lfsong4-May-03 23:36
lfsong4-May-03 23:36 
GeneralRe: paint application question Pin
James T. Johnson5-May-03 1:01
James T. Johnson5-May-03 1:01 
LynnSong wrote:
I am now implementin that by update the buffer bitmap every onmousemove calls.

How I would set it up requires that you maintain a Bitmap for each layer of the image.

Then you also need a collection of Point (or PointF) which represents the location of the mouse each time MouseMove is called.

Maybe some (psuedo) code will explain it better? Smile | :)

The code is uncompiled and thus untested but the premise is there.

private BitmapCollection layers;
private int currentLayer;
private Size size;
 
private PointCollection points;
 
private Pen currentPen;
 
OnMouseDown(MouseEventArgs e)
{
  // left mouse button down
  if( 0 != (e.Button & MouseButtons.Left) ) 
  {
    points.Clear();
    points.Add( new Point(e.X, e.Y) );
  
    Rectangle rectToInvalidate = new Rectangle(
      e.X - (int) currentPen.Width, e.Y - (int) currentPen.Width,
      ((int) currentPen.Width) * 2, ((int) currentPen.Width) * 2
    );
  
    Invalidate( rectToInvalidate );
  }
}
 
OnMouseMove( MouseEventArgs e )
{
  // left mouse button down
  if( 0 != (e.Button & MouseButtons.Left) ) 
  {
    Point newPoint = new Point(e.X, e.Y);
    points.Add( newPoint );
 
    Rectangle rectToInvalidate = RectFromTwoPoints( 
      newPoint, points[points.Count - 2], (int) currentPen.Width);
 
    Invalidate( rectToInvalidate );
  }
}
 
OnMouseUp( MouseEventArgs e )
{
  // left mouse button down
  if( 0 != (e.Button & MouseButtons.Left) ) 
  {
    using(Graphics g = Graphics.FromBitmap( layers[currentLayer] );
    {
      Point [] pointsArray = points.ToArray(typeof(Point[]));
  
      g.DrawLines( currentPen, pointsArray );
      
      points.Clear();
    }
  }
}
 
OnPaint( PaintEventArgs e )
{
  for(int = 0; i < layers.Count; i++ )
  {
    // Draw layer i, where 0 is the bottom layer
    // and layers.Count -1 is the top layer
 
    if( i == currentLayer && points.Count > 0 )
    {
      Point [] pointsArray = points.ToArray(typeof(Point[]));
  
      g.DrawLines( currentPen, pointsArray );
    }
  }
}
 
Rectangle RectFromTwoPoints( Point p1, Point p2, int penWidth )
{
  int left, top, right, bottom;
 
  if( p1.X < p2.X )
  {
    left = p1.X;
    right = p2.X;
  }
  else if( p1.X > p2.X )
  {
    left = p2.X;
    right = p1.X;
  }
  else // p1.X == p2.X
  {
    left = right = p1.X;
  }
 
  if( p1.Y < p2.Y )
  {
    top = p1.Y;
    bottom = p2.Y;
  }
  else if( p1.Y > p2.Y )
  {
    top = p2.Y;
    bottom = p1.Y;
  }
  else // p1.Y == p2.Y
  {
    top = bottom = p1.Y;
  }
 
  top -= penWidth;
  left -= penWidth;
  right += penWidth;
  bottom += penWidth;
 
  return Rectangle.FromLTRB( left, top, right, bottom );
}
Sheesh, I do all that and I see that MSDN has a similar example, only one 'layer' but it shows you how you can draw the lines using a GraphicsPath object. You can find the example in the help topic for the Control.MouseDown event (it is probably copied for the other Mouse events too).

James

"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation

QuestionAm I running as a native image? Pin
solidstore29-Apr-03 1:20
solidstore29-Apr-03 1:20 
AnswerRe: Am I running as a native image? Pin
Stephane Rodriguez.29-Apr-03 2:18
Stephane Rodriguez.29-Apr-03 2:18 
GeneralLow level network application Pin
Andrei Matei28-Apr-03 22:29
Andrei Matei28-Apr-03 22:29 
GeneralRe: Low level network application Pin
Andy Davey29-Apr-03 16:16
Andy Davey29-Apr-03 16:16 
Generalcall to the main thread Pin
OmegaSupreme28-Apr-03 15:59
OmegaSupreme28-Apr-03 15:59 
GeneralRe: call to the main thread Pin
Andy Davey28-Apr-03 16:24
Andy Davey28-Apr-03 16:24 
GeneralRe: call to the main thread Pin
OmegaSupreme28-Apr-03 16:54
OmegaSupreme28-Apr-03 16:54 
GeneralRe: call to the main thread Pin
david.minor3-May-03 18:51
david.minor3-May-03 18:51 
GeneralRe: call to the main thread Pin
Andy Davey5-May-03 21:24
Andy Davey5-May-03 21:24 
QuestionWill my .Net C# programs work on other OSs...? Pin
eggie528-Apr-03 14:41
eggie528-Apr-03 14:41 
AnswerRe: Will my .Net C# programs work on other OSs...? Pin
J. Dunlap28-Apr-03 16:05
J. Dunlap28-Apr-03 16:05 
AnswerRe: Will my .Net C# programs work on other OSs...? Pin
OmegaSupreme28-Apr-03 16:11
OmegaSupreme28-Apr-03 16:11 
GeneralCustomize toolbox during setup Pin
Patrick Lassalle26-Apr-03 4:22
Patrick Lassalle26-Apr-03 4:22 
GeneralRe: Customize toolbox during setup Pin
Burt Harris4-May-03 17:42
Burt Harris4-May-03 17:42 
GeneralUpdating Visual Studio to .NET 1.1 Pin
Katalyst25-Apr-03 17:26
Katalyst25-Apr-03 17:26 
GeneralRe: Updating Visual Studio to .NET 1.1 Pin
Kevin McFarlane25-Apr-03 23:52
Kevin McFarlane25-Apr-03 23:52 
GeneralRe: Updating Visual Studio to .NET 1.1 Pin
gregs11-May-03 11:44
gregs11-May-03 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.