When I give mouse potion to label, it appear much down and left to the current mouse position.
Label inside one group box and group box contain more then 50% of the form area.
Group Box is compulsory here.
I'm trying with this code.
int xX, yY = 0;
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
xX = e.X;
yY = e.Y;
lbl.Left = xX;
lbl.Top = yY;
}
It looks like you want to move the label within the boundaries of a panel, so you should add an event handler for the MouseMove event of the panel.
public Form1()
{
InitializeComponent();
panel1.MouseMove += panel1_MouseMove;
}
void panel1_MouseMove(object sender, MouseEventArgs e)
{
lbl.Location = e.Location;
}
public frmChequeFormat()
{
InitializeComponent();
gbCheque.MouseMove += gbCheque_MouseMove;
}
bool mDown = false;
private void gbCheque_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
label13.Location = e.Location;
}
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
Related
customBtn1 is the custom scrollbar. The below code sample works, but the scrolling action is jittery and stuttery. It does not scroll smoothly. Any idea why this could be happening and how to fix it?
int PreviousBarLoc;
private void MainForm_Load(object sender, EventArgs e)
{
PreviousBarLoc= customBtn1.Location.Y;
}
//move the scrollbar up and down when the user drags it
private void customBtn1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ MouseDownLocation = e.Location; }
}
private Point MouseDownLocation;
private void customBtn1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customBtn1.Top = e.Y + customBtn1.Top - MouseDownLocation.Y;
}
}
//scroll the panel
private void customBtn1_LocationChanged(object sender, EventArgs e)
{
int locDifference = customBtn1.Location.Y - PreviousBarLoc;
if (steamSrvListPnl.VerticalScroll.Value + locDifference <= 255 && steamSrvListPnl.VerticalScroll.Value + locDifference >= 0)
{
steamSrvListPnl.VerticalScroll.Value += locDifference;
}
PreviousBarLoc= customBtn1.Location.Y;
}
Your deltas are not quite right. The way it is currently written, if you drag the mouse down and then start dragging back up, it will not scroll back up because your current e.Y is still below the original MouseDownLocation.Y. I'm not entirely sure of your setup, but below is an example of how to get smooth scrolling to work when the left mouse button is pressed and dragged on a button:
private int _prevY = 0;
private bool _mouseDown = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_prevY = e.Y;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
panel1.VerticalScroll.Value = Math.Max(panel1.VerticalScroll.Minimum, Math.Min(panel1.VerticalScroll.Maximum, panel1.VerticalScroll.Value + e.Y - _prevY));
_prevY = e.Y;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
_prevY = 0;
}
}
private void Brick_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
}
private void Brick_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Brick1.Left = e.X;
Brick1.Top = e.Y;
}
}
public void Brick_MouseUp(object sender, MouseEventArgs m)
{
isDragging = false;
}
It kinda works but it glitches out when you move it (The picturebox teleports around the screen rapidly). https://www.youtube.com/watch?v=hWazyAnGNBE
You need to use the PointToClient method of the Form
var relativePoint = this.PointToClient(new Point(X, Y));
// Or
var relativePoint = this.PointToClient(Cursor.Position);
Control.PointToClient Method (Point)
Computes the location of the specified screen point into client
coordinates.
Update
private void Brick_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
var relativePoint = this.PointToClient(Cursor.Position);
Brick1.Left = relativePoint.X;
Brick1.Top = relativePoint.Y;
}
}
I tried to make drag and drop application . I drawn rectangle in run time and I want to detect if user try to move this rectangle or not
this is my code
private bool Mouse_Down = false;
Rectangle re = new Rectangle(100, 100, 60, 60);
private void DrawRegion_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.RoyalBlue), re);
}
private void DrawRegion_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse_Down == true)
{
re.Location = e.Location;
if (re.Right > DrawRegion.Width)
{
re.X = DrawRegion.Width - re.Width;
}
if (re.Top < 0)
{
re.Y = 0;
}
if (re.Left < 0)
{
re.X = 0;
}
if (re.Bottom > DrawRegion.Height)
{
re.Y = DrawRegion.Height - re.Height;
}
Refresh();
}
}
private void DrawRegion_MouseUp(object sender, MouseEventArgs e)
{
Mouse_Down = false;
}
private void DrawRegion_MouseDown(object sender, MouseEventArgs e)
{
Mouse_Down = true;
}
For more details now this rectangle move either user click on this rectangle or in any empty space so I want to detect if clicked location color pixel is rectangle color pixel or not before moving rectangle how to do that ?
Note:DrawRegion is a picturebox
Sorry for bad English
You can use Rect.Contains() to detect if your Rectaingle contain your current location
private void DrawRegion_MouseClick(object sender,MouseEventArgs e)
{
if (re.Contains(e.Location))
Mouse_Down = true;
else
Mouse_Down = false;
}
check this https://msdn.microsoft.com/en-us/library/ms557979(v=vs.110).aspx
I have this code for dragging on a Panel but its not doing the thing.
I have to choose if I will just Drag&drop or Resize.
I think theres something wrong with my code here in form load.
Anyway I have 5 labels here and a panel named as label1, label2, label3, label4, label5 on panel1.
private void form_Load(object sender, EventArgs e)
{
//for drag and drop
//this.panel1.AllowDrop = true; // or Allow drop in the panel.
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
//end of drag and drop
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
lblResizeAmtWord.Visible = false;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
//this.panel1.Controls.Add(c); //disable if already on the panel
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
I used the MouseDown, Up and Move Event of the Control I want to move. Let's say my control name is ctrlToMove.
private Point _Offset = Point.Empty;
private void ctrlToMove_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Offset = new Point(e.X, e.Y);
}
}
private void ctrlToMove_MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}
private void ctrlToMove_MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Point newlocation = ctrlToMove.Location;
newlocation.X += e.X - _Offset.X;
newlocation.Y += e.Y - _Offset.Y;
ctrlToMove.Location = newlocation;
}
}
I have a custom PictureBox which can zoom in using MouseWheel event. Now I want to add a panning feature to it. I mean when PictureBox is in zoomed state, if user left clicks and holds the click then move the mouse, the image would pan within the picturebox.
Here is my code but unfortunately it does not work! I don't know where to look anymore...
private Point _panStartingPoint = Point.Empty;
private bool _panIsActive;
private void CurveBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Focus();
_panIsActive = true;
_panStartingPoint = e.Location;
}
}
private void CurveBox_MouseUp(object sender, MouseEventArgs e)
{
_panIsActive = false;
}
private void CurveBox_MouseLeave(object sender, EventArgs e)
{
_panIsActive = false;
}
private void CurveBox_MouseMove(object sender, MouseEventArgs e)
{
if(_panIsActive && IsZoomed)
{
var g = CreateGraphics(); //Create graphics from PictureBox
var nx = _panStartingPoint.X + e.X;
var ny = _panStartingPoint.Y + e.Y;
var sourceRectangle = new Rectangle(nx, ny, Image.Width, Image.Height);
g.DrawImage(Image, nx, ny, sourceRectangle, GraphicsUnit.Pixel);
}
}
I am suspecting the MouseMove event...I am not sure if anything happens in this event and/or nx and ny does contain correct point.
Any helps/tips is really appriciated!
I think the math is backwards. Try it like this:
private Point startingPoint = Point.Empty;
private Point movingPoint = Point.Empty;
private bool panning = false;
void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
panning = true;
startingPoint = new Point(e.Location.X - movingPoint.X,
e.Location.Y - movingPoint.Y);
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
panning = false;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (panning) {
movingPoint = new Point(e.Location.X - startingPoint.X,
e.Location.Y - startingPoint.Y);
pictureBox1.Invalidate();
}
}
void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(Color.White);
e.Graphics.DrawImage(Image, movingPoint);
}
You aren't disposing your graphic object, and CreateGraphics is just a temporary drawing anyway (minimizing would erase it) so I moved the drawing code to the Paint event and am just invalidating as the user is panning.