mouseevents in c#

making a picturebox move with mouse

Page 1 of 1

1 Replies - 2228 Views - Last Post: 18 February 2009 - 05:35 PM Rate Topic: -----

#1 c#newbiefc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 11-April 08

mouseevents in c#

Post icon  Posted 18 February 2009 - 04:08 PM

hi
im trting to create a backgammon game but am stuck. i want player to click on a stone and move it using the mouse. i have tried several mouse event ideas but im not getting anywhere. can anyone help?

 public  class Mouseevent1 : System.Windows.Forms.Form
	{
		private IContainer components;
		PictureBox[] darkstone = new PictureBox[2];
		Button[] chipmove = new Button[24];
		int x,y,a=0;
		public Mouseevent1()
		{
			InitializeComponent();
			stones();
			chipmoves();
		}
   

		private void Mouseevent1_Paint(object sender, PaintEventArgs e)
		{
			Point[] points ={new Point(30,30),new Point(60,200),new Point(90,30),
						   new Point(90,30),new Point(120,200),new Point(150,30),
						   new Point(180,200),new Point(210,30),new Point(240,200),
						   new Point(270,30),new Point(300,200),new Point(330,30),
						   new Point(360,200),new Point(390,30),new Point(440,30),
						   new Point(470,200),new Point(500,30),new Point(530,200),
						   new Point(560,30),new Point(590,200),new Point(620,30),
						   new Point(650,200),new Point(680,30),new Point(710,200),
						   new Point(740,30),new Point(770,200),new Point(800,30)
						   
			};
			Point[] points2 ={new Point(30,576),new Point(60,376),new Point(90,576),
						   new Point(120,376),new Point(150,576),
						   new Point(180,376),new Point(210,576),new Point(240,376),
						   new Point(270,576),new Point(300,376),new Point(330,576),
						   new Point(360,376),new Point(390,576),new Point(440,576),
						   new Point(470,376),new Point(500,576),new Point(530,376),
						   new Point(560,576),new Point(590,376),new Point(620,576),
						   new Point(650,376),new Point(680,576),new Point(710,376),
						   new Point(740,576),new Point(770,376),new Point(800,576)
						   
			};

			SolidBrush brush = new SolidBrush(Color.Gold);
			SolidBrush brush2 = new SolidBrush(Color.SaddleBrown);
			Pen blackPen = new Pen(Color.SaddleBrown, 40);
			e.Graphics.DrawRectangle(blackPen, 10, 10, 810, 586);
			e.Graphics.FillRectangle(brush, 30, 30, 390, 546);
			e.Graphics.FillRectangle(brush, 400, 30, 400, 546);
			e.Graphics.DrawRectangle(blackPen, 410, 10, 10, 586);
			Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
			//   e.Graphics.DrawLine(pen, 30, 30,60,200);
			// e.Graphics.DrawLine(pen, 90, 30, 60, 200);
			System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
			path.AddLines(points);
			e.Graphics.DrawPath(pen, path);
			e.Graphics.FillPath(brush2, path);
			path.Reset(); path.AddLines(points2);
			e.Graphics.DrawPath(pen, path);
			e.Graphics.FillPath(brush2, path);

			brush.Dispose(); brush2.Dispose(); pen.Dispose();
		}
		private void chipmoves()
		{
			int x = 750; int y = 0; int no = 1;
			for (int i = 0; i < 6; i++)
			{
				chipmove[i] = new Button();
				this.Controls.Add(chipmove[i]);

				chipmove[i].Location = new Point(x, y);
				chipmove[i].Size = new Size(30, 20);
				chipmove[i].BackColor = Color.Cornsilk;

				chipmove[i].Text = no.ToString();
				chipmove[i].Enabled = false;
			   // chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
				x -= 60; no++;
			}
			x = 340;
			for (int i = 6; i < 12; i++)
			{
				chipmove[i] = new Button();
				this.Controls.Add(chipmove[i]);

				chipmove[i].Location = new Point(x, y);
				chipmove[i].Size = new Size(30, 20);
				chipmove[i].BackColor = Color.Cornsilk;

				chipmove[i].Text = no.ToString();
				chipmove[i].Enabled = false;
			   // chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
				x -= 60; no++;
			}
			x = 40; y = 576;
			for (int i = 12; i < 18; i++)
			{
				chipmove[i] = new Button();
				this.Controls.Add(chipmove[i]);

				chipmove[i].Location = new Point(x, y);
				chipmove[i].Size = new Size(30, 20);
				chipmove[i].BackColor = Color.Cornsilk;

				chipmove[i].Text = no.ToString();
				chipmove[i].Enabled = false;
				//chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
				x += 60; no++;
			}
			x = 455;
			for (int i = 18; i < 24; i++)
			{
				chipmove[i] = new Button();
				this.Controls.Add(chipmove[i]);

				chipmove[i].Location = new Point(x, y);
				chipmove[i].Size = new Size(30, 20);
				chipmove[i].BackColor = Color.Cornsilk;

				chipmove[i].Text = no.ToString();
				chipmove[i].Enabled = false;
			   // chipmove[i].MouseClick += new System.Windows.Forms.MouseEventHandler(chip_MouseClick);
				x += 60; no++;
			}
  }
		private void stones()
		{
			x = 749; y = 30;
			for (int s = 0; s < 2; s++)
			{
				//darkstone[s] = new Panel();
				darkstone[s] = new PictureBox();
				this.Controls.Add(darkstone[s]);
				darkstone[s].Size = new Size(35,35);
				darkstone[s].Location = new Point(x, y);
				darkstone[s].BackColor = Color.Transparent;
				x -= 60;
				darkstone[s].Paint += new PaintEventHandler(stone_Paint);
			   // darkstone[s].MouseMove += new System.Windows.Forms.MouseEventHandler(stone_MouseMove);
				darkstone[s].MouseDown += new System.Windows.Forms.MouseEventHandler(stone_MouseDown);
			  
			}
		}   
		private void stone_Paint(object sender, PaintEventArgs e)
		{
			
			SolidBrush brush = new SolidBrush(Color.SandyBrown);
			e.Graphics.FillEllipse(brush, 0, 0, 35, 35);
			brush.Dispose();
		}
	 private void Mouseevent1_DragAllow(object sender, DragEventArgs e)
		{
		   // DragDropEffects.Move();
		}
	  //  private void stone_MouseMove(object sender, MouseEventArgs e)
	  //  {
		   
	   //	 darkstone[0].Location = new Point(e.X, e.Y);
			
	  //  }


		private void stone_MouseDown(object sender, MouseEventArgs e)
		{
			int psender;
			a++;
			psender = Array.IndexOf(darkstone, sender);
	 
		   switch(psender)
			{
			case 0:
			 
			  if(a == 1)
				{
				darkstone[psender].Location = new Point(340,30);
				}
			  
			 if(a==2)
			 {
				darkstone[psender].Location = new Point(340,65);
				}
					  if(a==3)
					  {
							darkstone[psender].Location = new Point(340,100);
				}
				break;
			
				case 1:
				
			  if(a == 1)
				{
				darkstone[psender].Location = new Point(340,30);
				}
			  
			 if(a==2)
			 {
				darkstone[psender].Location = new Point(340,65);
				}
					  if(a==3)
					  {
							darkstone[psender].Location = new Point(340,100);
				}
			 
				
			   
			   // darkstone[psender].Location = new Point(280,30);
				break;
			   
			}
	  
		}




		  protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// Mouseevent1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(692, 459);
			this.Name = "Mouseevent1";
			this.Text = "Form1";
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.Mouseevent1_Paint);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Mouseevent1_DragDrop);
			this.ResumeLayout(false);

		}

		#endregion
	 /// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
  static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new Mouseevent1());
		}

		private void Mouseevent1_DragDrop(object sender, DragEventArgs e)
		{
			int dragsender;
			a++;
			dragsender = Array.IndexOf(darkstone, sender);
		   // DragDropEffects.Move(darkstone[dragsender]);
		}
	  

}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: mouseevents in c#

#2 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: mouseevents in c#

Posted 18 February 2009 - 05:35 PM

you could try a when clicking a stone changing its tag to following mouse and then onmousemove you move the stone with the tag following mouse to the current position of the mouse then on click again change the tag back to null and run some code to decide where to accurately place the stone based on defined regions and where stones already are

goodluck :D

mmm you could try using this but for it to work with the mouse centered on the picture box instead of on the top left corner you need to disable to picture box and once that is done it wont let you renable it using a click event cos click events on the picturebox no longer occur and the picture box is between your mouse and the form so the form cant recieve click events so a hotkey perhaps would be necessary but it would be much simpler just to neglect the code to center the mouse on the picture box and drag it by the corner

		protected override void onmousemove(MouseEventArgs e)
		{
			if (pictureBox1.Tag == "Following Mouse")
			{
				pictureBox1.Top = e.Y - pictureBox1.Height / 2;
				pictureBox1.Left = e.X - pictureBox1.Width / 2; //NEGLECT!!!
			}
		}


This post has been edited by luigiplumbersinc: 18 February 2009 - 06:08 PM

Was This Post Helpful? 1

Page 1 of 1