Newbie - Hints

  • Thread starter Thread starter R Srinivasan
  • Start date Start date
R

R Srinivasan

I am trying to develop a simple control that can be moved around on the
parent form. I tried capturing MouseDown, MouseMove and MouseUp events and
respond by changing the location of my control. Does not seem to work very
well. when the control is moved, it seems to leave a shadow from where it
was moved.

Well, I am looking for a simple way to implement a "movable" control. kind
of like a chess piece on a chess board for example. i will grab the object
with mouse down and when the mouse moves the control should move.

a sample would be fantastic.

regards
srini
 
I have not tried what you are suggesting but it sounds like you may need to
call the Repaint method of your parent control.

Dan
 
Here is some sample code that will accomplish what you asked in C#. It sounds like you are doing everything except maybe calling the refresh() method on the button after moving it

If you want to drag content between control look into the DoDragDrop method. If you are trying to manage docking behavior then check out the docking properties on teh contorl object

// Place this code in the InitializeComponent metho
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown)
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp)
this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove)


// Place this code wher eyou have your function declarations for the form class. The MoveFlag variable is a global variable that I declared at the top of the clas
// Set some flag to indicate that you are in drag mode on the
// mouse down even
private void button1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e

MoveFlag = 1

// Set some flag to indicate that you are no longer in drag mode on the
// mouse up even
private void button1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e

MoveFlag = 0

// Reset the location of the button object to equal its current locatio
// plus the new location offset. Since the MouseEventArg returns the clien
// coordinates you can just add these number
private void button1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e

if (MoveFlag == 1

button1.Location = (new Point(button1.Location.X+e.X
button1.Location.Y+e.Y))
button1.Refresh()



Here's the disclaimer

"Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm"
 
Didnt seem to do the trick.

the following is what i have attempted (still flickers) :
-------------------------pong.cs---------------------------------
using System ;

using System.Windows.Forms ;



class Paddle : System.Windows.Forms.UserControl

{

public int oldpos ;

public bool moving ;



protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e) ;

e.Graphics.FillRectangle( System.Drawing.Brushes.Green , 0 , 0 ,
Width , Height ) ;

}



/* i would have preferred to do it this way but also flickered.........



protected override void OnMouseDown(MouseEventArgs e)

{

base.OnMouseDown(e) ;

p = new System.Drawing.Point(e.X , e.Y) ;

moving = true ;

}



protected override void OnMouseMove(MouseEventArgs e)

{

base.OnMouseMove(e) ;

if (!moving) return ;

Top += e.Y - p.Y ;

p.Y = e.Y ;

}



protected override void OnMouseUp(MouseEventArgs e)

{

base.OnMouseUp(e) ;

moving = false ;

}



protected override void OnMouseLeave(EventArgs e)

{

base.OnMouseLeave(e) ;

moving = false ;

}

*/



}



class Pong : System.Windows.Forms.Form

{

const int UpdateTimeMS = 200 ;



Paddle left ;

Paddle right ;



public void OnTimedEvent(object o, System.Timers.ElapsedEventArgs args)

{

}



private void left_MouseDown(object sender ,
System.Windows.Forms.MouseEventArgs e)

{

left.moving = true ;

}



private void left_MouseUp(object sender ,
System.Windows.Forms.MouseEventArgs e)

{

left.moving = false ;

}



private void left_MouseMove(object sender ,
System.Windows.Forms.MouseEventArgs e)

{

if (left.moving)

{

left.Location = new System.Drawing.Point ( left.Location.X ,

left.Location.Y +
e.Y - left.oldpos );

left.oldpos = e.Y ;

}

}



Pong()

{

Text = "Ping Pong" ;

this.Show() ;



left = new Paddle() ;

left.Name = "Left" ;

left.ForeColor = System.Drawing.Color.Red ;

left.Location = new System.Drawing.Point( 20 , Height / 2 - 25 ) ;

left.Size = new System.Drawing.Size( 10 , 50 ) ;

left.MouseUp += new
System.Windows.Forms.MouseEventHandler(left_MouseUp) ;

left.MouseDown += new
System.Windows.Forms.MouseEventHandler(left_MouseDown) ;

left.MouseMove += new
System.Windows.Forms.MouseEventHandler(left_MouseMove) ;



right = new Paddle() ;

right.Name = "Right" ;

right.ForeColor = System.Drawing.Color.Red ;

right.Location = new System.Drawing.Point( Width - 30 , Height / 2 -
25 ) ;

right.Size = new System.Drawing.Size( 10 , 50 ) ;



Controls.Add(left) ;

Controls.Add(right) ;



}

static void Main()

{

Pong game = new Pong() ;

Application.Run( game ) ;

}

}















Bruce Benton said:
Here is some sample code that will accomplish what you asked in C#. It
sounds like you are doing everything except maybe calling the refresh()
method on the button after moving it.
If you want to drag content between control look into the DoDragDrop
method. If you are trying to manage docking behavior then check out the
docking properties on teh contorl object.
// Place this code in the InitializeComponent method
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);




// Place this code wher eyou have your function declarations for the form
class. The MoveFlag variable is a global variable that I declared at the top
of the class
 
My best guess at this point would be that you are seeing the painting of the rectangle fill and that is why you are seeing a flicker. I don't get a flicker on my code running on my machine (admittedly a fast machine). I think that is because I am dragging a button object and the refresh method has been optimized to avoid flicker. You might try implementing your paddle as a button with a bitmap and dragging that object. Alternatively, you may need to investigate lower level GDI methods which might provie a more efficient repaint method for your colored rectangle

"This posting is provided "AS IS" with no warranties, and confers no rights"
 
Don't know if you have tried this yet...
Place this in the Load section of the window (form) that is flickering.

SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
 
Back
Top