In a label docking on panel control I used this code:
(this is for move my panel with the mouse pointer)
Point m_pntPosPanel = new Point();
private void lblMove_MouseUp(object sender, MouseEventArgs e)
{
pnlCost.Location = new Point(e.X - m_pntPosPanel.X + pnlCosto.Location.X, e.Y - m_pntPosPanel.Y + pnlCosto.Location.Y);
Cursor.Current = Cursors.Default;
m_blnMouseDown = false;
}
private void lblMove_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
m_pntPosPanel.X = e.X;
m_pntPosPanel.Y = e.Y;
Cursor.Current = Cursors.Hand;
m_blnMouseDown = true;
}
}
how do I make to move that panel with directional keyboard?
I would register for the KeyUp event on the control and process as follows:
private void lblMove_KeyUp(object server, KeyEventArgs e)
{
Point location = button1.Location;
switch(e.KeyCode)
{
case Keys.Up:
location.Y = location.Y -1;
break;
case Keys.Down:
location.Y = location.Y + 1;
break;
case Keys.Right:
location.X = location.X + 1;
break;
case Keys.Left:
location.X = location.X - 1;
break;
}
button1.Location = location;
}
Related
I want an Image to move inside the picturebox. It shouldn't be possible that you can drag it out. I found an answer with the Padding and tried it out but it drags in the opposite direction. So I tried out to switch it with Right and down, but it is not getting dragged. Also I found an answer where the picturebox get moved but then it can be moved out of the form and isn't there anymore. So I need something that can just move the picture inside the picturebox or something that moves the picturebox but not out of the form.
private bool Dragging;
private Point lastLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
lastLocation = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Dragging == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
}
Do this
pictureBox1.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);
instead of this
pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);
I have done it by creating a panel and inserted image box inside of it.It's working in my side.Please find the code blow .
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
int moveLeftRight = e.X + pictureBox1.Left - MouseDownLocation.X;
int moveUpDown = e.Y + pictureBox1.Top - MouseDownLocation.Y;
int panlTopLocation = panel1.Location.Y;
int panlbottomLocation = panel1.Location.Y + panel1.Height - pictureBox1.Height;
int panlLeftLocation = panel1.Location.X;
int panlRightLocation = panel1.Location.X + panel1.Width - pictureBox1.Width ;
if (panlLeftLocation < moveLeftRight)
{
if (panlRightLocation > moveLeftRight)
{
pictureBox1.Left = moveLeftRight;
}
else
{
pictureBox1.Left = panlRightLocation;
}
}
else
{
pictureBox1.Left = panlLeftLocation;
}
if (panlTopLocation < moveUpDown)
{
if (panlbottomLocation > moveUpDown)
{
pictureBox1.Top = moveUpDown;
}
else
{
pictureBox1.Top = panlbottomLocation;
}
}
else
{
pictureBox1.Top = panlTopLocation;
}
}
}
I don't get anything registered when i'm key down and key up
heres my code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.A:
MessageBox.Show("Hi");
Walking = true;
break;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
Walking = false;
}
i don't get anything :(
Heres the rest of my code if that helps
public partial class Form1 : Form
{
//Graphic Vars
Graphics G;
Bitmap BB;
Graphics BBG;
Rectangle sRect;
Rectangle dRect;
Bitmap bmpTile;
bool AllowGrid = false;
int allowgrid = 0;
//Color
Pen rainbowpen = Pens.Black;
int rainbowpenstate = 0;
//wh
int w;
int h;
//Other Vars
bool isRunning = true;
//Map Vars
int[,,] Map = new int[151, 151, 11];
int MapX = 20;
int MapY = 20;
//Mouse Vars
int mouseX;
int mouseY;
int mMapX;
int mMapY;
//Character (toons)
Bitmap bmpChar;
int xPos =0;
int yPos =0;
int MoveSpeed =8;
short MoveDir =0;
short LastDir =2;
bool Walking = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load
(object sender, EventArgs e)
{
this.KeyDown += Form1_KeyDown;
this.KeyUp += Form1_KeyUp;
this.Show();
this.Focus();
//wh
w = this.Width;
h = this.Height;
//Initialize Graphics
G = this.CreateGraphics();
BB = new Bitmap(w, h);
Textures t = new Textures();
bmpTile = new Bitmap(Gametextures.Image);
//Map[21, 21, 0] = 1;
//Prevent from changing sizes
this.FormBorderStyle = FormBorderStyle.FixedSingle;
//Check if played before
CheckGameFiles();
//Game Loop
StartGameLoop();
}
public void StartGameLoop()
{
while (isRunning)
{
Application.DoEvents();
//User Input (1)
//AI (2)
//Update Object Data (3)
//Check Triggers (4)
//Draw Graphics (5)
DrawGraphics();
//Music (6)
}
}
public void DrawGraphics()
{
//Fill Back Buffer
//Draw Tiles
for(int X = 0; X < 30; X++)
{
for(int Y = 0; Y < 30; Y++)
{
//sRect = new Rectangle(X * 32, Y * 32, 32, 32);
/*Fill Background*/
//G.FillRectangle(Brushes.Black, sRect);
//Grid
GetSourceRect(MapX + X, MapY + Y, 32, 32);
dRect = new Rectangle(X * 32, Y * 32, 32, 32);
G.DrawImage(bmpTile, dRect, sRect, GraphicsUnit.Pixel);
if (AllowGrid)
{
G.DrawRectangle(Pens.Red, sRect);
}
}
}
//Draw Final Layers
//Chars/Menus
G.DrawRectangle(rainbowpen, mouseX * 32, mouseY * 32, 32, 32);
//Copy BackBuffer To Graphics Object
G = Graphics.FromImage(BB);
//BB to screen BB = BackBuffer
BBG = this.CreateGraphics();
BBG.DrawImage(BB, 0, 0, this.Width, this.Height);
//Fix OverDraw
G.Clear(Color.Black);
}
public void HideStart()
{
mainscreen1.Hide();
button1.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists("GameFiles/player.pinfo") && !File.Exists("GameFiles/world1.winfo") && !File.Exists("GameFiles/world2.winfo") && !File.Exists("GameFiles/world3.winfo"))
{
File.CreateText("GameFiles/player.pinfo");
File.CreateText("GameFiles/world1.winfo");
File.CreateText("GameFiles/world2.winfo");
File.CreateText("GameFiles/world3.winfo");
}
HideStart();
}
private void Form1_MouseMove
(object sender, MouseEventArgs e)
{
mouseX = (int) Math.Floor(e.X / 32.0d);
mouseY = (int) Math.Floor(e.Y / 32.0d);
mMapX = MapX + mouseY;
mMapY = MapY + mouseY;
}
private void timer1_Tick
(object sender, EventArgs e)
{
rainbowpenstate += 1;
if(rainbowpenstate == 17)
{
rainbowpenstate = 1;
}
switch(rainbowpenstate)
{
case 1:
rainbowpen = Pens.Red;
break;
case 2:
rainbowpen = Pens.OrangeRed;
break;
case 3:
rainbowpen = Pens.Yellow;
break;
case 4:
rainbowpen = Pens.YellowGreen;
break;
case 5:
rainbowpen = Pens.Green;
break;
case 6:
rainbowpen = Pens.DarkGreen;
break;
case 7:
rainbowpen = Pens.Blue;
break;
case 8:
rainbowpen = Pens.DarkBlue;
break;
case 9:
rainbowpen = Pens.Purple;
break;
case 10:
rainbowpen = Pens.MediumPurple;
break;
case 11:
rainbowpen = Pens.Blue;
break;
case 12:
rainbowpen = Pens.DarkGreen;
break;
case 13:
rainbowpen = Pens.Green;
break;
case 14:
rainbowpen = Pens.YellowGreen;
break;
case 15:
rainbowpen = Pens.Yellow;
break;
case 16:
rainbowpen = Pens.OrangeRed;
break;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
}
public void GetSourceRect(int X, int Y, int W, int H)
{
try
{
switch (Map[X, Y, 0])
{
//Grass
case 0:
sRect = new Rectangle(0, 0, 32, 32);
break;
//
case 1:
sRect = new Rectangle(0, 32, 32, 32);
break;
//
case 2:
break;
}
}
catch (Exception error) { Console.WriteLine(error); }
}
public void CheckGameFiles()
{
if(File.Exists("GameFiles/player.pinfo") && File.Exists("GameFiles/world1.winfo") && File.Exists("GameFiles/world2.winfo") && File.Exists("GameFiles/world3.winfo"))
{
button1.Text = "Continue!";
}
else
{
button1.Text = "Start";
}
}
public void ChangeWorlds(string world)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.A:
MessageBox.Show("Hi");
Walking = true;
break;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
Walking = false;
}
private void walkinganimation_Tick(object sender, EventArgs e)
{
if(Walking)
{
}
}
Make sure your form's KeyPreview property is set to true so that the events you coded can work.
private void Form1_Load
(object sender, EventArgs e)
{
this.KeyDown += Form1_KeyDown;
this.KeyUp += Form1_KeyUp;
this.KeyPreview = true;
I don't really know what the issue is but this part of the code
private void walkinganimation_Tick(object sender, EventArgs e)
{
if(Walking)
{
}
}
Whenever I move the form in its maximum state and move the form, the form location will default (0, 0). I use a MouseDown, MouseMove, and MouseUp event.
private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
AdjustingTheForm = true;
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
if (AdjustingTheForm)
{
scr = Screen.FromControl(this).WorkingArea;
LastLocation = e.Location;
if (FormNormalSize == false)
{
FormNormalSize = true;
CustomForm_Resize(sender, e);
this.Location = new Point(e.X - 400, e.Y - 15);
this.Update();
}
AdjustingTheForm = false;
MovingTheForm = true;
Console.WriteLine("1. " + this.Location);
}
if(MovingTheForm)
{
this.Location = new Point((this.Location.X - LastLocation.X) + e.X, (this.Location.Y - LastLocation.Y) + e.Y);
this.Update();
Console.WriteLine("2. " + this.Location + " " + e.Location);
}
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
MovingTheForm = false;
Here is where I put the private instance members:
namespace CustomForm_Practice_1
{
public partial class CustomForm : Form
{
bool minimizeB1MouseDown, maximizeB1MouseDown, exitB1MouseDown;
bool FormNormalSize;
bool AdjustingTheForm, MovingTheForm;
Point LastLocation;
Rectangle scr;
.......
Here are the results when I move the form (UPDATED):
1. this.Location: {X=1100,Y=6}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}
1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=0,Y=0}
1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}
1. this.Location: {X=0,Y=2}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=8}
OLD I don't know why x jumps from 703 to 0 and y from 8 to 0. This problem, however, only occurs when the size of the form changes and the form is moved. When the form is at normal size (800, 600). Here is the form resize event:
New This time this.Location started at X = 1100 and Y = 6 and then it went to (0, 0). e.Location did the same thing. Here is the form resize event:
private void CustomForm_Resize(object sender, EventArgs e)
{
if (FormNormalSize == false)
//Maximized Window
{
scr = Screen.FromControl(this).WorkingArea;
this.Location = new Point(scr.X, scr.Y);
this.Size = new Size(scr.Width, scr.Height);
this.Update();
//Panel Heights
TopPanel.Height = 30;
BottomPanel.Height = scr.Height - 32;
//Panel Widths
TopPanel.Width = scr.Width - 2;
BottomPanel.Width = scr.Width - 2;
}
else if (FormNormalSize)
//Normal Window
{
this.Size = new Size(800, 600);
//Panel Heights
TopPanel.Height = 30;
BottomPanel.Height = this.Height - 32;
//Panel Widths
TopPanel.Width = this.Width - 2;
BottomPanel.Width = this.Width - 2;
}
//Panel Locations
TopPanel.Location = new Point(1, 1);
BottomPanel.Location = new Point(1, TopPanel.Height + 1);
The question is, why does the form location go to (0, 0) when this line
this.Location = new Point(e.X - 400, e.Y - 15); changes the location that was previously set this.Location = new Point(scr.X, scr.Y);?
Moving/resizing a form like this can be somewhat fiddly, because the mouse location in MouseEventArgs is given relative to the top left of the form rather than in screen coords.
A better way to track mouse coords when you need screen coords is to use the MousePosition class along with mouse capture via Control.Capture = true.
The easiest way for me to demonstrate this is through a sample app:
Create a default Windows Forms app. I'm going to assume that the main form is called Form1.
Drop onto the main form a Panel called panel1 and set its Dock
property to Fill.
Add to panel1 the following handlers: panel1_MouseDown, panel1_MouseMove and panel1_MouseUp.
Change the code in Form1.cs as follows:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool moving;
Point offset;
Point original;
void panel1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
panel1.Capture = true;
offset = MousePosition;
original = this.Location;
}
void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!moving)
return;
int x = original.X + MousePosition.X - offset.X;
int y = original.Y + MousePosition.Y - offset.Y;
this.Location = new Point(x, y);
}
void panel1_MouseUp(object sender, MouseEventArgs e)
{
moving = false;
panel1.Capture = false;
}
}
Compile and run the application then click and drag on the main window to move it around. It should follow the mouse as you move it around.
Once you have that working, you should be able to apply the same logic to your application.
I figured it out. PointToClient help me get the LastLocation value. Here is the new code:
private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
AdjustingTheForm = true;
}
else if (e.Button == MouseButtons.Right)
{
AdjustingTheForm = false;
}
LastLocation = PointToClient(e.Location);
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
if (AdjustingTheForm)
{
if (FormNormalSize == false)
{
FormNormalSize = true;
CustomForm_Resize(sender, e);
this.Location = new Point(Cursor.Position.X - 400, Cursor.Position.Y - 15);
this.Update();
LastLocation = PointToClient(e.Location);
MovingFormMaximized = true;
}
else
{
MovingFormMinimized = true;
}
AdjustingTheForm = false;
}
if ((MovingFormMinimized && this.Location.Y > 0) || (MovingFormMinimized && this.Location.Y <= 0 && e.Y > 32))
{
mouseHuggingWall = false;
this.Location = new Point(this.Location.X - LastLocation.X + e.X,
this.Location.Y - LastLocation.Y + e.Y);
this.Update();
}
else if (MovingFormMinimized && this.Location.Y <= 0)
{
mouseHuggingWall = true;
this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
this.Update();
}
if ((MovingFormMaximized && this.Location.Y > 0) || (MovingFormMaximized && this.Location.Y <= 0 && e.Y > 32))
{
mouseHuggingWall = false;
this.Location = new Point(this.Location.X -LastLocation.X + e.X,
this.Location.Y - LastLocation.Y + e.Y);
this.Update();
}
else if (MovingFormMaximized && this.Location.Y <= 0)
{
mouseHuggingWall = true;
this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
this.Update();
}
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
if(mouseHuggingWall == true)
{
FormNormalSize = false;
CustomForm_Resize(sender, e);
}
MovingFormMinimized = false;
MovingFormMaximized = false;
mouseHuggingWall = false;
}
I added more to the code but the main fix is in the code.
i am creating a game application in c#, there is panel and picturebox inside that panel. now i want to restrict that picture box within that panel boundary. i am moving picture box with keydown event.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
pictureBox1.Left -= 10;
//po = position.Left;
}
else if (e.KeyCode == Keys.Right)
{
pictureBox1.Left += 10;
// po = position.Right;
}
else if (e.KeyCode == Keys.Up)
{
pictureBox1.Top -= 10;
// po = position.Up;
}
else if (e.KeyCode == Keys.Down)
{
pictureBox1.Top +=10;
// po = position.Down;
}
}
It is preferred that you use a switch statement in your case.
The below code is tested and working. You just need to set the Max values calculated from the height/width of the PictureBox and Panel.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int borderMargin = 5;
int stepSize = 10;
switch (e.KeyCode)
{
case Keys.Left:
{
int newLeft = Math.Max(0, pictureBox1.Left - stepSize);
pictureBox1.Left = newLeft;
break;
}
case Keys.Right:
{
int maxVal = panel1.Width - pictureBox1.Width - borderMargin;
int newLeft = Math.Min(maxVal, pictureBox1.Left + stepSize);
pictureBox1.Left = newLeft;
break;
}
case Keys.Up:
{
int newTop = Math.Max(0, pictureBox1.Top - stepSize);
pictureBox1.Top = newTop;
break;
}
case Keys.Down:
{
int maxVal = panel1.Height - pictureBox1.Height - borderMargin;
int newTop = Math.Min(maxVal, pictureBox1.Top + stepSize);
pictureBox1.Top = newTop;
break;
}
}
}
Also set the borderMargin and stepSize according to your needs based on your Form configuration.
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(new PointF(e.X * xFactor, e.Y * yFactor));
pictureBox2.Invalidate();
label5.Visible = true;
label5.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
counter += 1;
label6.Visible = true;
label6.Text = counter.ToString();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label4.Visible = true;
label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
if (panning)
{
movingPoint = new Point(e.Location.X - startingPoint.X,
e.Location.Y - startingPoint.Y);
pictureBox1.Invalidate();
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Pen p;
p = new Pen(Brushes.Green);
foreach (PointF pt in points)
{
e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
}
for (int i = 0; i < points.Count - 1; i++)
{
if (points.Count > 1)
{
e.Graphics.DrawLine(p, points[i].X, points[i].Y, points[i+1].X, points[i+1].Y);
}
}
if (checkBox2.Checked)
{
}
}
I added a new checkBox2 and in the paint event of checkBox2 i want to make that if its checked if i just move the mouse over the pictureBox1 area without clicking anything it will draw a line on pictureBox2 a route of where the mouse is moving in pictureBox1.
And ever X pixels i move the mouse it will create a point in Green color on this line.
You can use a GraphicsPath to save all the points mouse is over, that's way the drawing can be better than using some List or Array to store the points (and loop through that drawing everytime paint is raised):
GraphicsPath gp = new GraphicsPath();
Point p;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(!label4.Visible) label4.Visible = true;
label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
if (panning) {
if(p == Point.Empty) {
p = e.Location;
return;
}
gp.AddLine(p,e.Location);
p = e.Location;
pictureBox2.Invalidate();
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e) {
using(Pen p = new Pen(Color.Green,2f)){
p.StartCap = p.EndCap = LineCap.Round;
p.LineJoin = LineJoin.Round;
e.Graphics.DrawPath(p,gp);
}
}
Note that you should add using System.Drawing.Drawing2D; before using the code.