here is my code:
public partial class Form1 : Form
{
Graphics g;
bool mouseUP = false;
double dimensions = 4.0;
SolidBrush brush;
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
brush = new SolidBrush(Color.Black);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseUP = false;
backgroundWorker1.RunWorkerAsync(e);
}
private bool mouseIsUP()
{
return mouseUP;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseUP = true;
MessageBox.Show("UP");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(0,e.Argument);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
while (!mouseIsUP())
{
g.FillEllipse(brush, ((MouseEventArgs)e.UserState).X - (int)dimensions / 2, ((MouseEventArgs)e.UserState).Y - (int)dimensions / 2, (int)dimensions, (int)dimensions);
dimensions += 0.2;
Thread.Sleep(10);
}
}
}
Why the event mouseUP is never occurring???
If i remove the while I can see the MessageBox "UP"...
I'm trying to make bigger a ellipse while the mouse is pressed. When I release the mouse button the ellipse should not grow more.
Thanks in advance!
Because you are not binding your event handlers to the events (assuming you didn't just miss out that code).
For example, within Form1's constructor, you could add
MouseUp += Form1_MouseUp;
MouseDown += Form1_MouseDown;
It looks like the background worker is somehow blocking the event loop in your code. Where is DoWork being called on the worker?
I think a better solution might be to override the OnPaint method, check if the mouse is down there, then draw your circle.
bool isMouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (isMouseDown)
{
g.FillEllipse(brush, ((MouseEventArgs)e.UserState).X - (int)dimensions / 2, ((MouseEventArgs)e.UserState).Y - (int)dimensions / 2, (int)dimensions, (int)dimensions);
dimensions += 0.2;
Thread.Sleep(10);
}
}
Related
Let's assume the following Situation:
a Control (e.g. a Button) has an attached behavior to enable a Drag&Drop-Operation
<Button Content="test">
<i:Interaction.Behaviors>
<SimpleDragBehavior/>
</i:Interaction.Behaviors>
</Button>
And the SimpleDragBehavior
public class SimpleDragBehavior: Behavior<Button>
{
protected override void OnAttached ()
{
AssociatedObject.MouseLeftButtonDown += OnAssociatedObjectMouseLeftButtonDown;
AssociatedObject.MouseLeftButtonUp += OnAssociatedObjectMouseLeftButtonUp;
AssociatedObject.MouseMove += OnAssociatedObjectMouseMove;
mouseIsDown = false;
}
private bool mouseIsDown;
private void OnAssociatedObjectMouseMove (object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
AssociatedObject.Background = new SolidColorBrush(Colors.Red);
DragDrop.DoDragDrop((DependencyObject)sender,
AssociatedObject.Content,
DragDropEffects.Link);
}
}
private void OnAssociatedObjectMouseLeftButtonUp (object sender, MouseButtonEventArgs e)
{
mouseIsDown = false;
}
private void OnAssociatedObjectMouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
mouseIsDown = true;
}
}
The task now is to determine when the drag ends, to restore the orignal backgound of the button.
This is no problem when droped on an drop-target. But how do i recognize a drop on something which isn't a drop-target? In the worst case: outside the window?
DragDrop.DoDragDrop returns after drag-and-drop operation is completed.
Yes, "Initiates a drag-and-drop operation" is confusing, since it could be read as "start drag-and-drop and return":
private void OnAssociatedObjectMouseMove (object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
AssociatedObject.Background = new SolidColorBrush(Colors.Red);
var effects = DragDrop.DoDragDrop((DependencyObject)sender,
AssociatedObject.Content,
DragDropEffects.Link);
// this line will be executed, when drag/drop will complete:
AssociatedObject.Background = //restore color here;
if (effects == DragDropEffects.None)
{
// nothing was dragged
}
else
{
// inspect operation result here
}
}
}
I am trying to implement a horizontal swipe event handler in the following app. However, the gr_CrossSliding cross sliding event handler never fires.
What do I need to do to fire gr_CrossSliding?
public sealed partial class MainPage : Page
{
private GestureRecognizer gr;
public MainPage()
{
this.InitializeComponent();
gr = new GestureRecognizer();
gr.GestureSettings = GestureSettings.CrossSlide;
gr.CrossSlideHorizontally = true;
gr.CrossSliding += gr_CrossSliding;
}
void gr_CrossSliding(GestureRecognizer sender, CrossSlidingEventArgs args)
{
// handle swipe event
}
}
You need to set the GestureRecognizer on the handlers of the UI Element that is getting the gestures.
In this case, I'm using a Grid (GrdFoto).
public MainPage()
{
this.InitializeComponent();
gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.Drag;
gestureRecognizer.Dragging += gestureRecognizer_Dragging;
GrdFoto.PointerPressed += GrdFoto_PointerPressed;
GrdFoto.PointerMoved += GrdFoto_PointerMoved;
GrdFoto.PointerReleased += GrdFoto_PointerReleased;
GrdFoto.PointerCanceled += GrdFoto_PointerCanceled;
}
void GrdFoto_PointerPressed(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessDownEvent(e.GetCurrentPoint(this.GrdFoto));
this.GrdFoto.CapturePointer(e.Pointer);
e.Handled = true;
}
void GrdFoto_PointerMoved(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints(this.GrdFoto));
}
void GrdFoto_PointerReleased(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessUpEvent(e.GetCurrentPoint(this.GrdFoto));
e.Handled = true;
}
void GrdFoto_PointerCanceled(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.CompleteGesture();
e.Handled = true;
}
void gestureRecognizer_Dragging(GestureRecognizer sender, DraggingEventArgs args)
{
// Drag completed.
}
I have a class (which extends Framework Element) which contains within it a number of other Elements.
// Click event coverage area
private Rectangle connectorRectangle;
These shapes all have their event handlers, and when the user clicks on them its working well. Now what I want is to be able to 'handle' a right-click on my class from outside the scope of the class.
So I figured the best way to do it is to handle the event internally, and somehow bubble it to the top
private void connectorRectangle_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
MouseButtonEventArgs args = new MouseButtonEventArgs();
//???
e.Handled = true;
}
The problem is that I have no idea how to raise the event. this.OnMouseRightButtonUp doesn't exist, and all the tutorials I'm finding are for raising custom events.
I'm pretty new to silverlight, so bear with me if I missed something obvious.
Try it :
public Rectangle
{
this.Click += new System.EventHandler(Function);
}
private void Function(object sender, System.EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Right)
{
//Your code
}
}
Your "exteded Framework Element class" shouldn't handel the mouse event (or if they handel them, set e.Handled to false). Then the event should bubble up automatically (without reraise the event).
EDIT
public class ExtendedFrameworkElement : Grid
{
public ExtendedFrameworkElement()
{
Border b1 = new Border();
b1.Padding = new Thickness(20);
b1.Background = Brushes.Red;
b1.MouseRightButtonUp += b1_MouseRightButtonUp;
Border b2 = new Border();
b2.Padding = new Thickness(20);
b2.Background = Brushes.Green;
b2.MouseRightButtonUp += b2_MouseRightButtonUp;
b1.Child = b2;
this.Children.Add(b1);
}
private void b1_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
private void b2_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
}
Xaml:
<Window x:Class="WpfApplicationTest.MainWindow">
<wpfApplicationTest:ExtendedFrameworkElement MouseRightButtonUp="UIElement_OnMouseRightButtonUp"/>
</Window>
Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UIElement_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
//DoSomeThing
}
}
I'm trying to move the control named pictureBox1 by dragging it around. The problem is, when it moves, it keeps moving from a location to another location around the mouse, but it does follow it...
This is my code. and I would really appreciate it if you could help me
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool selected = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
selected = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (selected == true)
{
pictureBox1.Location = e.Location;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
selected = false;
}
}
All you need:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point MouseDownLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Left = e.X + pictureBox1.Left - MouseDownLocation.X;
pictureBox1.Top = e.Y + pictureBox1.Top - MouseDownLocation.Y;
}
}
}
You can also use the extension:
public static class CmponentsExtensions
{
//Management of mouse drag and drop
#region Menu and Mouse
private static bool mouseDown;
private static Point lastLocation;
/// <summary>
/// To enable control to be moved around with mouse
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="control"></param>
public static void moveItselfWithMouse<T>(this T control) where T: Control
{
control.MouseDown += (o, e)=> { mouseDown = true; lastLocation = e.Location; };
control.MouseMove += (o, e) =>
{
if (mouseDown)
{
control.Location = new Point((control.Location.X - lastLocation.X) + e.X, (control.Location.Y - lastLocation.Y) + e.Y);
control.Update();
}
};
control.MouseUp += (o, e) => { mouseDown = false; } ;
}
public static void moveOtherWithMouse<T>(this T control, Control movedObject) where T : Control
{
control.MouseDown += (o, e) => { mouseDown = true; lastLocation = e.Location; };
control.MouseMove += (o, e) =>
{
if (mouseDown)
{
movedObject.Location = new Point((movedObject.Location.X - lastLocation.X) + e.X, (movedObject.Location.Y - lastLocation.Y) + e.Y);
movedObject.Update();
}
};
control.MouseUp += (o, e) => { mouseDown = false; };
}
#endregion
}
Then you need to use it with some control:
In this case pictureBox1 moved the whole Form
pictureBox1.moveOtherWithMouse(this);
In this case you move only pictureBox:
pictureBox1.moveItselfWithMouse();
try this to move pictureBox control at runtime using mouse
private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
PictureBox p = sender as PictureBox;
if (p != null)
{
if (e.Button == MouseButtons.Left)
{
p.Top += (e.Y - yPos);
p.Left += (e.X - xPos);
}
}
}
I just wanna find out if there's a way I could minimize code clutter in my application.
I have written code/s similar to this:
private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 1;
}
private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 0.5;
}
private void btnShowAll_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 1;
}
private void btnShowAll_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 0.5;
}
private void btnPrev_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 1;
}
private void btnPrev_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 0.5;
}
private void btnSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 1;
}
private void btnSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 0.5;
}
private void btnSearchStore_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 1;
}
private void btnSearchStore_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 0.5;
}
private void btnCloseSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 1;
}
private void btnCloseSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 0.5;
}
private void btnHome_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 1;
}
private void btnHome_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 0.5;
}
and so on and so forth...
Do I need to create a 'function' that will run initially? Or do I have to create another class just so I can 'organize' them?
Any suggestions?
You could rewrite all those functions into 2:
private void FadeBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 1;
}
private void FadeBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 0.5;
}
And then point all of the buttons MouseEnter and MouseLeave events to those functions.
You need to have ChangeButtonOpacity method:
private void ChangeButtonOpacity(Button button, double newOpacity)
{
button.Opacity = newOpacity;
}
And you can implement your handlers as:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 1);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 0.5);
}
In this way you will need only two handlers.
Create a Mouse Enter Event and register all the buttons with it. Inside the method you will notice I cast the sender object as a button. So what ever button calls it you can perform this opacity action on.
private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button button = (Button) sender;
button.Opacity = 1;
}
As far I can see, in your case you can shorten to:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 1;
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
In the designer, you can choose these event handlers then instead of creating new ones for each button.
Perhaps you can use the Tag property of the button if your not using it for anything else, Then you can do the following.
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = (double)((sender as Button).Tag);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
This would allow you to setup different values for different buttons using only two handlers.