Relocate image on mouseover - c#

I am trying to recreate a mac style menu bar is c# on visual studio 2012. I can get the image to move on mouseEnter and relocate back to the original place on mouseLeave. I do this by setting the location of the picturebox.
The problem I have is when I mouseEnter the image if I leave the mouse in the area at the bottom of the image between the bottom of the old image location and the bottom of the new image location the image will jump constantly between the two location.
Can anyone advise how to stop this or avoid it.
private void pic1_MouseEnter(object sender, EventArgs e)
{
pic1.Location = new Point(328, 300);
}
private void pic1_MouseLeave(object sender, EventArgs e)
{
pic1.Location = new Point(328, 316);
}

Try to detach event handler before moving image location, then attach it back after. Something like this :
private void pic1_MouseEnter(object sender, EventArgs e)
{
pic1.MouseEnter -= pic1_MouseEnter;
pic1.MouseLeave -= pic1_MouseLeave;
pic1.Location = new Point(328, 300);
pic1.MouseEnter += pic1_MouseEnter;
pic1.MouseLeave += pic1_MouseLeave;
}

True, since moving the image triggers the MouseLeave event, Leave callback sends it to its original location, thus triggering MouseEnter and so on for ever and ever (unless you move the mouse away).
I would suggest you eliminate MouseLeave callback and keep a state of where the image is:
private bool retracted = false;
private void pic1_MouseEnter(object sender, EventArgs e)
{
if (retracted)
{
pic1.Location = new Point(328, 316);
}
else
{
pic1.Location = new Point(328, 300);
}
retracted = !retracted;
}

Thanks for all the solutions but none had the desired effect but I have come up with a solution using mouseHover and mouseLeave. On hover moves the image to the desired location ad the leave returns it to the original spot.
private void pic1_MouseLeave(object sender, EventArgs e)
{
pic1.Location = new Point(328, 316);
}
private void pic1_MouseHover(object sender, EventArgs e)
{
pic1.Location = new Point(328, 310);
}

Related

C# Draw polyline(s) on mouse move

I want to draw something on my canvas whenever drawing_mode and is_drawing booleans are on. Right now I am using a polyline list and BackgroundWorker for threading. My main problem is that my code only creates one polyline and the dots are ALWAYS connected. In other words I can stop drawing for a while but then wherever I click a new line connection is made with the previous one. The end result is that my canvas.Children only has one polyline element with all the points. Could anyone help me solve this?
P.S. I am not very good with threading yet...
private BackgroundWorker drawing_worker;
private void drawing_worker_ProgressChanged(object sender, ProgressChangedEventArgs eventargs)
{
Polyline polyline = new Polyline();
polyline.Points = last_polyline.Points;
canvas.Children.Remove(last_polyline);
var pos = canvas_relative_pos;
polyline.Points.Add(pos);
polyline.Stroke = new SolidColorBrush(Colors.Black);
polyline.StrokeThickness = 1;
canvas.Children.Add(polyline);
last_polyline = polyline;
}
private void drawing_worker_DoWork(object sender, DoWorkEventArgs eventargs)
{
while (drawing_mode_enabled && is_drawing)
{
drawing_worker.ReportProgress(0);
Thread.Sleep(5);
if (drawing_worker.CancellationPending) break;
}
}
private void ContentControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (drawing_mode_enabled)
{
is_drawing = true;
drawing_worker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
drawing_worker.ProgressChanged += drawing_worker_ProgressChanged;
drawing_worker.DoWork += drawing_worker_DoWork;
drawing_worker.RunWorkerAsync();
}
}
You don't need a BackgroundWorker or any other asynchronous stuff to draw polylines on Canvas.
Just create a Canvas with a Background (so that it gets input events) and handlers for the MouseLeftButtonDown, MouseLeftButtonUp and MouseMove events:
<Canvas Background="Transparent"
MouseLeftButtonDown="CanvasMouseLeftButtonDown"
MouseLeftButtonUp="CanvasMouseLeftButtonUp"
MouseMove="CanvasMouseMove"/>
In the mouse down handler create a new Polyline and add it to the Canvas. Also capture the mouse so that you get mouse move events even when the mouse cursor leaves the Canvas. In the mouse up handler release the mouse capture. Finally, in the mouse move handler, add points to the last polyline child of the Canvas.
private void CanvasMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var panel = (Panel)sender;
var polyline = new Polyline
{
Stroke = Brushes.Black,
StrokeThickness = 3
};
panel.Children.Add(polyline);
panel.CaptureMouse();
}
private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
((UIElement)sender).ReleaseMouseCapture();
}
private void CanvasMouseMove(object sender, MouseEventArgs e)
{
var panel = (Panel)sender;
if (panel.IsMouseCaptured)
{
var polyline = panel.Children.OfType<Polyline>().Last();
polyline.Points.Add(e.GetPosition(panel));
}
}

C# - Drag and Drop & Keep Control

So I am trying to make a drag and drop application that drags something on a panel. I did it before and I have forgot the code that I used for it. I would also like it to have an event too. Here is an example that failed to work:
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox flower1 = new PictureBox();
flower1.Image = pictureBox1.Image;
flower1.Location = new Point(panel1.Location.X, panel1.Location.Y);
flower1.Width = 100;
this.Controls.Add(flower1);
flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
}
void flower1_MouseDown(object sender, MouseEventArgs e)
{
//flower1.Location = new Point(MousePosition.X, MousePosition.Y);
}
I wanted me to click on a flower, then it would be placed onto the panel then, if the mouse is clicked over that control duplicated onto the panel, then make the location the mouse cursors location. How would I go about doing any of this? It does not even appear to duplicate.
EDIT: Just realised that the image is underneath the panel making it not able to be seen. That's one issue, now how do I get it to drag and drop?
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox flower1 = new PictureBox();
flower1.Image = pictureBox1.Image;
flower1.Location = Point.Empty;
flower1.Width = 100;
flower1.Parent = panel1;
flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
}

c# Superposed control doesn't fire events

My Windows Forms code has two superposed PictureBoxes: a small X on the top corner of a user-loaded image. I'll call them DelImage and Image, respectively. Like this:
http://imgur.com/fsW7R8i
The DelImage appears (Visible = true) on Image_MouseEnter and disappears on Image_MouseLeave. Now, I want the DelImage to have a custom behavior as well when the mouse enters it, but DelImage never fires events (nor MouseEnter, nor MouseLeave, nor Click). I've tried bringing it to front, but it already is in front of the Image.
Here's some code:
private void picImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
}
private void picImage_MouseLeave(object sender, EventArgs e)
{
delImage.Visible = false;
}
private void picDelImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
this.Cursor = Cursors.Hand;
}
private void picDelImage_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Arrow;
}
private void picDelImage_Click(object sender, EventArgs e)
{
Panel currentPanel = this.tlpImages.Controls["pnlImage"] as Panel;
currentPanel.Visible = false;
}
With this code, the picDelImage events are never reached. I never enter DelImage, apparently because I never leave Image. What can I do to make my PictureBoxes have the expected behavior?

Moving a control within another control's visible area

I have a PictureBox that is inside a TabPage, and of course this TabPage is part of a TabView and this TabView is inside a Form. I want users be able to move this picture box within the tab page. For this I am using the MouseDown, MouseMove and MouseUp events of the picture box:
private void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e)
{
if (!_mapPackageIsMoving)
{
_mapPackageIsMoving = true;
}
}
private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
if(_mapPackageIsMoving)
{
pictureBoxPackageView.Location = MousePosition; //This is not exact at all!
return;
}
//Some other code for some other stuff when picturebox is not moving...
}
private void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e)
{
if (_mapPackageIsMoving)
{
_mapPackageIsMoving = false; //Mouse button is up, end moving!
return;
}
}
But my problem lies in the MouseMove event. As soon as I move mouse after button down, the picture box jumps out of tab page's visible area.
I need to know how to handle the move only within the rectangle of the tab page, and if picture box is being dragged out of tab view's visible area, it shouldn't move anymore unless user brings the mouse inside the tab view's visible rectangle.
Any helps/tips will be appriciated!
You need a variable to hold the original position of the PictureBox:
Modified from a HansPassant answer:
private Point start = Point.Empty;
void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) {
_mapPackageIsMoving = false;
}
void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) {
if (_mapPackageIsMoving) {
pictureBoxPackageView.Location = new Point(
pictureBoxPackageView.Left + (e.X - start.X),
pictureBoxPackageView.Top + (e.Y - start.Y));
}
}
void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) {
start = e.Location;
_mapPackageIsMoving = true;
}

Drawing a Bitmap?

Hey guys i'm trying to figure out how to draw a bitmap that i have loaded onto my form.
I am using this following code to draw it
private void button1_Click(object sender, PaintEventArgs e)
{
open.ShowDialog();
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
e.Graphics.DrawImage(image, 85, 38);
}
Using this nothing gets drawen, am i not using the correct draw method?
I don't think you really want to paint anything yourself. Try adding a picturebox to your form, and in the button click event, simply set the image to that picturebox.
// In initializecomponent()
button1.Click += button1_Click;
// The click handler
private void button1_Click(object sender, EventArgs e)
{
if (open.ShowDialog() == DialogResult.OK)
{
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
pictureBox1.Image = image;
}
}

Categories

Resources