I have a picturebox and movement just right and to the left, but when moving it to the left or right edge of the picturebox appears in the middle of the form , as the picture below
This thus
what I want is when moving left or right when the board of picturebox were in the same position of the board form the image can not be more moves , just to the other lado.Conforme the image below
Follows the script:
private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
x = e.X;
//y = e.Y;
}
}
private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
imagemPictureBox.Left += (e.X - x);
//imagemPictureBox.Top += (e.Y - y);
}
}
Try something like this:
// the point in the PB where we grab it:
Point mDown;
void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
// grab the pb
mDown = e.Location;
}
void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
// new position:
int x = imagemPictureBox.Left + e.X - mDown.X;
int y = imagemPictureBox.Top + e.Y - mDown.Y;
// limit to form size:
x = Math.Min(Math.Max(x, 0), this.ClientSize.Width - imagemPictureBox.Width);
y = Math.Min(Math.Max(y, 0), this.ClientSize.Height - imagemPictureBox.Height);
// move the pb:
imagemPictureBox.Location = new Point(x, y);
}
void imagemPictureBox_MouseUp(object sender, MouseEventArgs e)
{
//release it (optional)
mDown = Point.Empty;
}
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 have a PictureBox1 draggable with this code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + pictureBox1.Width> panel1.Width)
pictureBox1.Left += (e.X - x);
}
}
But I can't get bound restrictions,
I just want move the Picture inside a Panel, like this:
Example
Any ideas?
Thanks
Try this:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int left = 0;
if (pictureBox1.Left >= 0 && pictureBox1.Right <= panel1.Width)
left = pictureBox1.Left + (e.X - x);
left = Math.Min(panel1.Width - pictureBox1.Width , left);
left = Math.Max(0, left);
pictureBox1.Left = left;
}
}
I'm using WinForms. In my form i have a picturebox with an image. How can i paint the picturebox but not the area inside the expanding square. Here is my code. Currently i could create the expanding square but i don't know how to paint the picturebox white outside of that square.
int _cropX, _cropY, _cropWidth, _cropHeight;
private State _currentState;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
Cursor = Cursors.Cross;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//X and Y are the coordinates of Crop
pictureBox1.Refresh();
_cropWidth = e.X - _cropX;
_cropHeight = e.Y - _cropY;
pictureBox1.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _cropWidth, _cropHeight);
}
}
}
else
{
Cursor = Cursors.Default;
}
}
private void Crop_Checkbox_CheckedChanged(object sender, EventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
this.Cursor = Cursors.Cross;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
if (_currentState == State.Crop)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
_cropX = e.X;
_cropY = e.Y;
_cropPen = new Pen(Color.FromArgb(153, 180, 209), 3); //2 is Thickness of line
_cropPen.DashStyle = DashStyle.DashDotDot;
pictureBox1.Refresh();
}
}
}
else
{
Cursor = Cursors.Default;
}
}
public Pen _cropPen;
private enum State
{
Crop
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (Crop_Checkbox.Checked == true)
{
//Paint picturebox...
}
else
{
Cursor = Cursors.Default;
}
}
This is best done with a GraphicsPath:
using System.Drawing.Drawing2D;
..
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle r1 = pictureBox1.ClientRectangle; // note I don't use width or height!
Rectangle r2 = new Rectangle(50, 30, 80, 40);
GraphicsPath gp = new GraphicsPath(FillMode.Alternate);
gp.AddRectangle(r1); // first the big one
gp.AddRectangle(r2); // now the one to exclude
e.Graphics.FillPath( Brushes.Gold, gp);
}
Note that I..
..use the Paint event for persistent grphics
..only paint onto the surface of the PictureBox, not into its image. See here for the difference!
You can add more rectangles or other shapes to exclude.
If you want image and surface combined, either draw into the image or ask the PictureBox to DrawToBitmap..
I need to make a graph of lines from a csv file and draw it with GDI got a Windows form but now I want to change the scale and move.
I started with something more simple as moving a rectangle with the mouse but to start drawing the rectangle always from the origin.
Can anyone help?
My code is:
public partial class Form1 : Form {
int origenX;
int origenY;
bool transformar = false;
public Form1() {
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
dibujar(e.Graphics);
}
private void Form1_MouseMove(object sender, MouseEventArgs e) {
int deltaX = e.X - origenX;
int deltaY = e.Y - origenY;
if (transformar) {
System.Drawing.Graphics g = this.CreateGraphics();
Matrix mAux = new Matrix();
mAux.Translate(deltaX, deltaY);
g.Transform = mAux;
dibujar(g);
g.Dispose();
}
this.Text = "x=" + deltaX.ToString() + ", y=" + deltaY.ToString();
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
transformar = true;
origenX = e.X;
origenY = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
transformar = false;
}
private void dibujar(System.Drawing.Graphics g) {
g.Clear(Color.White);
g.DrawEllipse(new Pen(Color.Blue, 2), new Rectangle(50, 50, 50, 50));
}
}
Already solved, thanks anyway, the code was as follows:
public partial class Form2 : Form {
private float dx=0;
private float dy=0;
private float X0=1;
private float Y0=1;
private bool trasladar = false;
public Form2() {
InitializeComponent();
}
private void Form2_Paint(object sender, PaintEventArgs e) {
e.Graphics.TranslateTransform(dx, dy, MatrixOrder.Append);
e.Graphics.Clear(Color.White);
e.Graphics.DrawEllipse(new Pen(Color.Blue), new Rectangle(50, 50, 50, 50));
}
private void Form2_MouseDown(object sender, MouseEventArgs e) {
Cursor = Cursors.SizeAll;
trasladar = true;
X0 = e.X;
Y0 = e.Y;
}
private void Form2_MouseUp(object sender, MouseEventArgs e) {
Cursor = Cursors.Default;
trasladar = false;
}
private void Form2_MouseMove(object sender, MouseEventArgs e) {
if (trasladar) {
dx += (e.X - X0);
dy += (e.Y - Y0);
X0 = e.X;
Y0 = e.Y;
Invalidate();
}
}
}
Use this.Invalidate(); when the form should redraw.
It will declare the form as invalid, so it will be forced to redraw. Only then the Form1_Paint will be called.
Test this behaivour by doing an Debug output in the Paint-Event.
Then you can start further debugging to get what you want.
Download the project
I am trying to make a panel with a background color which should be able to be drawn in runtime when the user holds down left mouse button and moves it around. All works find when the user is starting from top left and go to bottom right just like the image shows:
But I want the user to be able to make the panel from bottom right to top left. Just like when you select something on your computer with your mouse
Here is my code for now:
public void parent_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point tempLoc = e.Location;
this.Location = new Point
(
Math.Min(this.Location.X, tempLoc.X),
Math.Min(this.Location.Y, tempLoc.Y)
);
this.Size = new Size
(
Math.Abs(this.Location.X - tempLoc.X),
Math.Abs(this.Location.Y - tempLoc.Y)
);
this.Invalidate();
}
}
I think this is where I go wrong, and I simply can't find the right algorithm for it:
this.Size = new Size
(
Math.Abs(this.Location.X - tempLoc.X),
Math.Abs(this.Location.Y - tempLoc.Y)
);
But if I use a rectangle it works fine, but I want my panel to be able to do it as well.
You need to just check the minimums and maximums of your starting point versus your mousemoving point. The problem with the code is you are using the control location as a starting point, but if you move the mouse from bottom-right to top-left, your location needs to change. A control can't have a negative size.
Here is how I re-wrote it (I removed unnecessary stuff for testing):
public class SelectionTool : Panel {
Form parent;
Point _StartingPoint;
public SelectionTool(Form parent, Point startingPoint) {
this.DoubleBuffered = true;
this.Location = startingPoint;
//this.endingPoint = startingPoint;
_StartingPoint = startingPoint;
this.parent = parent;
this.parent.Controls.Add(this);
this.parent.MouseMove += new MouseEventHandler(parent_MouseMove);
this.BringToFront();
this.Size = new Size(0, 0);
}
public void parent_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
int minX = Math.Min(e.Location.X, _StartingPoint.X);
int minY = Math.Min(e.Location.Y, _StartingPoint.Y);
int maxX = Math.Max(e.Location.X, _StartingPoint.X);
int maxY = Math.Max(e.Location.Y, _StartingPoint.Y);
this.SetBounds(minX, minY, maxX - minX, maxY - minY);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
this.BackColor = Color.Blue;
}
}
Here is the code I used to test it on a form:
private SelectionTool _SelectPanel = null;
private void Form1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == System.Windows.Forms.MouseButtons.Left) {
if (_SelectPanel == null)
_SelectPanel = new SelectionTool(this, e.Location);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
if (_SelectPanel != null) {
_SelectPanel.Dispose();
_SelectPanel = null;
}
}