I have a double buffered panel which has a background image. Now when I draw line on panel its good but when I move that line in panel area it takes a snapshot or background with it where it was first located. I thought double buffered panel will solve this but its not working and I also try to add refresh panel code in mouse down event but then line constantly flickers so please guys help me.
public void LineMouseDown(object sender, MouseEventArgs e)
{
var l = (Line)sender;
if (GetDistance(e.X, e.Y, l.StartPoint.X, l.StartPoint.Y) < 30 ||
GetDistance(e.X, e.Y, l.EndPoint.X, l.EndPoint.Y) < 30)
{
_rotating = true;
}
else
{
_dragging = true;
panel5.Refresh();
}
_clickX = e.X;
_clickY = e.Y;
panel5.Refresh();
_clickCoords = new Point(l.Left + e.X, l.Top + e.Y);
UpdateCircle(l);
UpdateMiddle(l);
}
public void LineMouseUp(object sender, MouseEventArgs e)
{
_rotating = false;
_dragging = false;
panel5.Refresh();
}
public void LineMouseMove(object sender, MouseEventArgs e)
{
var l = (Line)sender;
if (_rotating)
{
if ((DateTime.Now - lastRotate).TotalMilliseconds > 60)
{
try
{
var angle = GetAngle(l.Left + e.X, l.Top + e.Y, _centerX, 0, _centerX, _centerY);
var newStartPoint = new Point(
(int)Math.Round(_middleX + _radius * Math.Sin(angle)),
(int)Math.Round(_middleY + _radius * Math.Cos(angle))
);
var newEndPoint = new Point(2 * _middleX - newStartPoint.X, 2 * _middleY - newStartPoint.Y);
l.SetPoints(newStartPoint, newEndPoint);
UpdateMiddle(l);
lastRotate = DateTime.Now;
}
catch
{
//ignored
}
}
}
else if (_dragging)
{
l.Left = l.Left + e.X - _clickX;
l.Top = l.Top + e.Y - _clickY;
}
}
Here what it looks like when I move line
public class DoubleBufferPanel : Panel
{
public DoubleBufferPanel()
{
// Set the value of the double-buffering style bits to true.
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw , true);
this.UpdateStyles();
}
}
Related
I am trying to move and resize a drawn rectangle using mouse events, the new starting location of x, y is calculated but the rectangle position doesn't update:
My form source code:
public partial class Form3 : Form
{
Rectangle rect;
Point StartXY;
Point EndXY;
int x = 0;
int y = 0;
int height = 0;
int width = 0;
bool m_mouseDown = false;
bool m_movingRect = false;
Pen rectPen = new Pen(Color.Red, 1);
public Form3()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics gObj = e.Graphics;
rect = new Rectangle(x, y, height, width);
rectPen.DashStyle = DashStyle.Dash;
gObj.DrawRectangle(rectPen, rect);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
m_mouseDown = true;
// Moving rectangle
if (rect.Contains(e.Location))
{
m_movingRect = true;
Console.WriteLine("m_mouseDown");
}
StartXY = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (rect.Contains(e.Location))
this.Cursor = Cursors.SizeAll;
else
this.Cursor = Cursors.Default;
if (m_mouseDown && rect.Contains(e.Location))
{
rect.X += e.X - StartXY.X; // <---- it is calculated correctly but doesn't update rectangle position
rect.Y += e.Y - StartXY.Y;
StartXY = e.Location;
Console.WriteLine("pictureBox1_MouseMove StartXY " + StartXY.X);
Console.WriteLine("pictureBox1_MouseMove StartXY " + StartXY.Y);
Console.WriteLine("pictureBox1_MouseMove rect " + rect.X);
Console.WriteLine("pictureBox1_MouseMove rect " + rect.Y);
}
if (m_mouseDown && !rect.Contains(e.Location))
{
EndXY = e.Location;
x = Math.Min(StartXY.X, EndXY.X);
y = Math.Min(StartXY.Y, EndXY.Y);
height = Math.Abs(StartXY.X - EndXY.X);
width = Math.Abs(StartXY.Y - EndXY.Y);
//Console.WriteLine("pictureBox1_MouseMove XXXX: " + " --- " + e.X.ToString());
//Console.WriteLine("pictureBox1_MouseMove YYYY: " + " --- " + e.Y.ToString());
}
Invalidate(true);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (!m_movingRect)
{
EndXY = e.Location;
}
m_mouseDown = false;
m_movingRect = false;
Invalidate();
}
}
I created a button with my mouse events like going from one point A and sizing with the mouse.
Now when my Mouse input is released i want to draw the 4 points of the Button(TopLeftCorner,TopRighTCorner,ButtomLeft, Buttomright) and when clicked on one point adjust the size or move the button
Here's my code for drawing the button but I can't draw the 4 points when my mouse is released.
private Point startPoint;
private Button rectangle;
private void PaintSurface_MouseDown(object sender, MouseButtonEventArgs e)
{
//Recupere la position de la souris
startPoint = e.GetPosition(paintSurface);
//Instantie un nouveau Button
rectangle = new Button
{
BorderBrush = Brushes.LightBlue,
//TO be given an name for the zone
Content = "Zone",
Name = "Zone",
};
Canvas.SetLeft(rectangle, startPoint.X);
Canvas.SetTop(rectangle, startPoint.X);
paintSurface.Children.Add(rectangle);
}
private void PaintSurface_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released)
return;
// Point point = new Point();
// point.X = x;
//point.Y = y;
// rectangle.PointFromScreen(point);
// MessageBox.Show(point.ToString());
var pos = e.GetPosition(paintSurface);
var x = Math.Min(pos.X, startPoint.X);
var y = Math.Min(pos.Y, startPoint.Y);
var w = Math.Max(pos.X, startPoint.X) - x;
var h = Math.Max(pos.Y, startPoint.Y) - y;
rectangle.Width = w;
rectangle.Height = h;
Canvas.SetLeft(rectangle, x);
Canvas.SetTop(rectangle, y);
rectangle.MouseDown += Rectangle_MouseDown;
rectangle.MouseMove += Rectangle_MouseMove;
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
}
private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
var pos = e.GetPosition(rectangle);
var x = Math.Min(pos.X, startPoint.X);
var y = Math.Min(pos.Y, startPoint.Y);
var w = Math.Max(pos.X, startPoint.X) - x;
var h = Math.Max(pos.Y, startPoint.Y) - y;
rectangle.Width = w;
rectangle.Height = h;
Canvas.SetLeft(rectangle, x);
Canvas.SetTop(rectangle, y);
}
After Weeks of searching i have found a solution.My boss wanted me to use rectangle instead of button
so here's a solution of resizing a rectangle
private void MouseEventHandler(object sender, MouseButtonEventArgs e)
{
//Button is an instance of a Rectangle.
button = (sender as Rectangle);
Console.WriteLine("Dans le handler");
do
{
Console.WriteLine(Mouse.GetPosition(button));
//MessageBox.Show("a cliqué");
point = Mouse.GetPosition(button);
// Console.WriteLine(button.Width );
if (e.ChangedButton == MouseButton.Left)
{
Console.WriteLine("Cliqued for moving");
m_IsPressed = true;
isediting = false;
Mouse.OverrideCursor = Cursors.Hand;
}
else
{
m_IsPressed = false;
}
if (point.X > 0 && point.X < button.Width && point.Y <= button.Height
&& point.Y > button.Height - 10)
{
Console.WriteLine("Cliqued for editing");
m_IsPressed = true;
isediting = true;
Mouse.OverrideCursor = Cursors.SizeNWSE;
}
}
while (button.IsMouseCaptured);
}
I am currently working on a topdown game made in windows forms. In the game, your character rotates after your mouse and you can shoot a shot from your character in the direction of your mouse. To create multiple areas in the game, I chose to use groupboxes as separate areas in the game that you can switch between using buttons but I've run in to a problem. I can no longer rotate or move my character or even shoot within the groupboxes.
I've tried setting breakpoints and discovered that the keyup, keydown and mousemove methods are not being called but I don't know why. For some reason I get an error when i release space in the btnSave_Click method which i marked in the code.
static Image originalImage;
bool pRight, pLeft, pUp, pDown;
string[] Saves;
Save[] RSaves = new Save[4];
Save yoursave;
Save temp;
int slot;
NewGame newgame;
SavedGames savedgames;
string savedata, name = "";
int lvl = 1;
double exp = 0, money = 0;
int pSpeed = 5;
double deltaY, deltaX;
float interval = 7;
Point start;
Point nextStart;
Point cursor;
float radian;
const double Rad2Grad = (180 / Math.PI);
public MainGame()
{
InitializeComponent();
pbPlayer.BringToFront();
gbxTown.AllowDrop = true;
gbxQ1.AllowDrop = true;
pbShot.Location = pbPlayer.Location;
// newgame = new NewGame();
// savedgames = new SavedGames();
originalImage = pbPlayer.Image;
}
public void setSaves(string savedata, int slot)
{
Saves = savedata.Split('#');
string a1 = Saves[0];
string a2 = Saves[1];
string a3 = Saves[2];
string a4 = Saves[3];
RSaves[0] = temp.StringToSaves(temp, a1);
RSaves[1] = temp.StringToSaves(temp, a2);
RSaves[2] = temp.StringToSaves(temp, a3);
RSaves[3] = temp.StringToSaves(temp, a4);
yoursave = RSaves[slot - 1];
name = yoursave.getName();
this.slot = slot;
Controls.Add(pbPlayer);
Controls.Add(pbShot);
}
private void MainGame_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.A)
{
pRight = false;
pLeft = true;
}
else if (e.KeyData == Keys.D)
{
pRight = true;
pLeft = false;
}
else if (e.KeyData == Keys.S)
{
pUp = true;
pDown = false;
}
else if (e.KeyData == Keys.W)
{
pUp = false;
pDown = true;
}
if (e.KeyData == Keys.Space)
{
start = pbPlayer.Location;
cursor = Cursor.Position;
nextStart = start;
deltaY = cursor.Y - start.Y;
deltaX = cursor.X - start.X;
double test = Angle(start, cursor);
radian = (float)(Angle(start, cursor) - 175);
timer2.Enabled = true;
}
}
private void MainGame_KeyUp_1(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.A)
{
pLeft = false;
}
else if (e.KeyData == Keys.D)
{
pRight = false;
}
else if (e.KeyData == Keys.S)
{
pUp = false;
}
else if (e.KeyData == Keys.W)
{
pDown = false;
}
if (e.KeyData == Keys.Space)
{
timer2.Stop();
pbShot.Location = start;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
MovePlayer();
//checkCollision();
}
private void btnNextArea_Click(object sender, EventArgs e)
{
pbPlayer.Parent = gbxQ1;
pbShot.Parent = gbxQ1;
gbxQ1.Location = new Point(0, 0);
gbxTown.Location = new Point(605, 0);
pbPlayer.Location = new Point(100, 200);
pbShot.Location = pbPlayer.Location;
}
private void btnSave_Click(object sender, EventArgs e)
{
newgame = new NewGame();
savedgames = new SavedGames();
RSaves[slot - 1] = new Save(name, money, lvl, exp);
for (int i = 0; i < 4; i++) //When the Spacebar is released, a System.IndexOutOfRangeException error happens for i.
{
Saves[i] = RSaves[i].SavesToString();
}
savedata = Saves[0] + Saves[1] + Saves[2] + Saves[3];
System.IO.File.WriteAllLines(#"saves.txt", Saves);
newgame.setSaves(savedata);
savedgames.setSaves(savedata);
}
private void btnBack_Click(object sender, EventArgs e)
{
pbPlayer.Parent = gbxTown;
pbShot.Parent = gbxTown;
gbxTown.Location = new Point(0, 0);
gbxQ1.Location = new Point(605, 0);
pbPlayer.Location = new Point(100, 200);
pbShot.Location = pbPlayer.Location;
}
private void MainGame_MouseMove_1(object sender, MouseEventArgs e)
{
var y2 = e.Y;
var y1 = (this.pbPlayer.Location.Y + (this.pbPlayer.Height / 2));
var x2 = e.X;
var x1 = (this.pbPlayer.Location.X + (this.pbPlayer.Width / 2));
var angle = (float)Math.Atan2((y1 - y2), (x1 - x2));
pbPlayer.Image = RotateImage(originalImage, (angle * 57));
}
private double Angle(Point start, Point end)
{
return (float)Math.Atan2(end.Y - start.Y, end.X - start.X) * 57;
}
private void timer2_Tick_1(object sender, EventArgs e)
{
nextStart.X -= Convert.ToInt16(interval * ((float)Math.Cos(radian / Rad2Grad)));
nextStart.Y -= Convert.ToInt16(interval * ((float)Math.Sin(radian / Rad2Grad)));
pbShot.Location = nextStart;
}
public static Image RotateImage(Image img, float rotationAngle)
{
Bitmap bmp = new Bitmap(img.Width, img.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(img, new Point(0, 0));
gfx.Dispose();
return bmp;
}
private void MovePlayer()
{
if (pRight == true && pbPlayer.Left < 900)
{
pbPlayer.Left += pSpeed;
}
else if (pLeft == true && pbPlayer.Left > 0)
{
pbPlayer.Left -= pSpeed;
}
else if (pUp == true && pbPlayer.Top < 600)
{
pbPlayer.Top += pSpeed;
}
else if (pDown == true && pbPlayer.Top > 0)
{
pbPlayer.Top -= pSpeed;
}
}
The expected outcome is for the movement, aiming and firing to work as intended within the groupboxes but currently they do not execute in the code while in the groupboxes. It works fine in the form but not in the groupboxes. I would appriciate any help.
I've 4 panels, having same Y and different X, that are created at the program start on a picturebox. When I click on a panel, it sets the focus and is ready to get a keysDown event so i.e. if I click on up arrow key the panel moves up.
This is the code:
public partial class FormView : Form
{
List<CircleButton> myPanels = new List<CircleButton>(); // array of panels CircleButton
Point[] arrayPoints_milestones; // array of X,Y
int index;
public FormView()
{
InitializeComponent();
arrayPoints_milestones = new Point[4];
for (int i = 0; i < 4; i++ )
{
arrayPoints_milestones[i] = new Point { X = 20, Y = 20 };
}
test();
}
protected void panel_Click(object sender, EventArgs e)
{
myPanels[index].PreviewKeyDown -= new PreviewKeyDownEventHandler(panel_KeyDown);
CircleButton panel = sender as CircleButton;
index = (int)panel.Tag;
myPanels[index].Focus(); //panel.Focus();
myPanels[index].PreviewKeyDown += new PreviewKeyDownEventHandler(panel_KeyDown);
}
private void panel_KeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
myPanels[index].Centre = new Point(myPanels[index].Centre.X, myPanels[index].Centre.Y - 10);
MessageBox.Show("" + myPanels[index].Centre.Y);
Invalidate();
}
if (e.KeyCode == Keys.Down)
{
myPanels[index].Centre = new Point(myPanels[index].Centre.X, myPanels[index].Centre.Y + 10);
MessageBox.Show("" + myPanels[index].Centre.Y);
Invalidate();
}
}
private void test()
{
//for (int i = 0; i < 4; i++)
int i=0;
foreach(var value in arrayPoints_milestones)
{
CircleButton panel = new CircleButton();
panel.Tag = i;
panel.Centre = new Point(arrayPoints_milestones[i].X + i * 10, arrayPoints_milestones[i].Y);
panel.Radius = 10;
panel.BackColor = Color.Red;
panel.Message = "Index: " + panel.Tag.ToString();
myPanels.Add(panel); // qui aggiungo il pannello alla lista di pannelli myPanels
pictureBox1.Controls.Add(myPanels[i]);
myPanels[i].Click += new EventHandler(panel_Click);
i++;
}
}
}
and this is the custom panel class:
public class CircleButton : Panel
{
//Properties to draw circle
float radius;
public float Radius
{
get { return radius; }
set
{
radius = value;
this.Size = new Size((int)Radius, (int)Radius);
}
}
public string Name
{
get;
set;
}
Point centre;
public Point Centre
{
get { return centre; }
set
{
centre = value;
this.Location = Centre;
}
}
public string Message { get; set; }
public CircleButton()
{
//Default Values
Name = "panel_base";
this.BackColor = Color.Black;
Radius = 1;
Centre = new Point(0, 0);
this.DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Defines a graphic path and set it as the panel's region
//For custom region use different path's
if (centre != null)
{
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0, 0, radius, radius);
this.Region = new Region(path);
path.Dispose();
}
}
}
Each time you click a panel, a PreviewKeyDownEventHandler is added - so 3 clicks will trigger 3 (different) eventhandlers with the same invocation target, and each will move your panel for 10 pixels up/down:
protected void panel_Click(object sender, EventArgs e) {
CircleButton panel = sender as CircleButton;
index = (int)panel.Tag;
myPanels[index].Focus(); //panel.Focus();
myPanels[index].PreviewKeyDown += new PreviewKeyDownEventHandler(panel_KeyDown);
}
Updated code for FormView:
public partial class FormView : Form {
List<CircleButton> myPanels = new List<CircleButton>(); // local use only in my example
Point[] arrayPoints_milestones; //not needed anymore
int index; //not needed anymore
public FormView() {
InitializeComponent();
this.Load += FormView_Load;
}
void FormView_Load(object sender, EventArgs args) {
Point panelOffset = new Point(20, 20);
for (int i = 0; i < 4; i++) {
var panel = new CircleButton() {
Name = "panel" + i, //Attention! You have hidden the property "Name" in "Control" with a re-declaration in "CircleButton"
Tag = i, //not needed anymore, right?
Centre = new Point(panelOffset.X + i * 10, panelOffset.Y),
Radius = 10,
BackColor = Color.Red,
Message = "Index: " + i.ToString(),
};
panel.Click += (s, e) => {
panel.Focus();
};
panel.PreviewKeyDown += (s, e) => {
if(e.KeyCode == Keys.Up) {
Point centre = panel.Centre; //copy value
centre.Y -= 10;
panel.Centre = centre; //assign modified copy
Invalidate();
}
if(e.KeyCode == Keys.Down) {
Point centre = panel.Centre; //copy value
centre.Y += 10;
panel.Centre = centre; //assign modified copy
Invalidate();
}
};
myPanels.Add(panel);
pictureBox1.Controls.Add(panel);
}
}
}
I have a PictureBox image in a form which is moving with the mouse movement on the panel.
It's moving as I want it, however it's flickering all the time (like refreshes) and I learnt that it's a problem with forms.
I tried the following lines of code in the constructor of my form but no success:
SetStyle( ControlStyles.ResizeRedraw, true );
SetStyle( ControlStyles.UserPaint, true );
SetStyle( ControlStyles.AllPaintingInWmPaint, true );
SetStyle( ControlStyles.OptimizedDoubleBuffer, true );
This is the event handler for the mouse move if it helps to see all the picture. chipHolder is a panel and image is the image imported from file respectively.
private void grid_MouseMove(object sender, MouseEventArgs e)
{
columnPosition = e.X;
if (columnPosition != -1)
{
if (!(columnPosition < 35 || columnPosition > 610))
{
chipHolder.Controls.Clear();
PictureBox picBox = new PictureBox();
chipHolder.Controls.Add(picBox);
picBox.Image = image;
picBox.Width = image.Width;
picBox.Height = image.Height;
picBox.Location = new Point(columnPosition - 33, 0);
picBox.Show();
}
}
chipHolder.Update();
}
Any ideas?
Do not recreate the PictureBox, just move it.
Just tried this, and image moves without any flickering:
private void button1_Click(object sender, EventArgs e)
{
for (int iter = 0; iter < 500; iter++)
{
pictureBox1.Location = new Point(pictureBox1.Left + 1, pictureBox1.Top + 1);
Application.DoEvents();
System.Threading.Thread.Sleep(30);
}
}
For mouse movements:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Location = new Point(e.X, e.Y);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Location = new Point(e.X + pictureBox1.Left, e.Y + pictureBox1.Top);
}
What Igor said:
private void grid_MouseMove(object sender, MouseEventArgs e)
{
columnPosition = e.X;
if (columnPosition != -1)
{
if (!(columnPosition < 35 || columnPosition > 610))
{
PictureBox picBox = chipHolder.Controls[0] // whatever your picbox id is;
picBox.Location = new Point(columnPosition - 33, 0);
}
}
}