Simulating a Drag/Drop event in WPF - wpf

I want to simulate a drag/drop event in WPF.
For this I'll need to gain access to the data stored in the "Drag/Drop buffer" and also I'll need to create a DragEventArgs.
I noticed that the DragEventArgs is sealed and has no public ctor.
So my questions are:
1. how can I create an instance of DragEventArgs?
2. How can I gain access to the drag/drop buffer?

i recently do this! i simulated drag/drop with MouseDown, MouseMove and MouseUp events. for example for my application, i have some canvases that i want to drag and drop them. every canvas has an id. in MouseDown event, i buffer its id and use it in MouseMove and MouseUp event. Desktop_Canvas is my main Canvas that contains some canvases. these canvases are in my dictionary (dic).
here is my code:
private Dictionary<int, Win> dic = new Dictionary<int, Win>();
private Point downPoint_Drag = new Point(-1, -1);
private int id_Drag = -1;
private bool flag_Drag = false;
public class Win
{
public Canvas canvas = new Canvas();
public Point downpoint = new Point();
public Win()
{
canvas.Background = new SolidColorBrush(Colors.Gray);
}
}
private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
{
try
{
Point movePoint = e.GetPosition(Desktop_Canvas);
if (flag_Drag && downPoint_Drag != new Point(-1, -1))
{
double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
downPoint_Drag = movePoint;
if (x == -1)
x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
if (y == -1)
y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
Canvas.SetLeft(dic[id_Drag].canvas, x);
Canvas.SetTop(dic[id_Drag].canvas, y);
}
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
downPoint_Drag = new Point(-1, -1);
id_Drag =-1;
flag_Drag = false;
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
{
MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
Desktop_Canvas_MouseLeftButtonUp(null, ee);
}
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
downPoint_Drag = e.GetPosition(Desktop_Canvas);
int hoverId = HoverWin(downPoint_Drag);
flag_Drag = true;
id_Drag = hoverId;
dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
}
private int HoverWin(Point p)
{
foreach (int i in dic.Keys)
{
if (dic[i].canvas.IsMouseOver)
return i;
}
return -1;
}

Related

ListView Scrolling when Item Added to Collection on one machine but not another

I have an WPF app using MVVM that shows a log in a ListView control. I have it bound to an ObservableCollection and the control updates when items are added.
I have it coded so that when it starts, it automatically scrolls to the top when an item is inserted into the collection at position 0 so it always shows the latest log message. This works on all machines I have tested.
When a user does something on the ListView (clicks or scrolls), the automatic scrolling is turned off so the user can look at any part of the log they want. When they are finished looking at the log, they can click a button to turn the automatic scrolling back on. Everything works except on one of my test machines, the view changes as items are added. On my dev machine and another test machine, the ListView window does not change when things are added to the collection. Same code/config files are used for all systems.
For example:
The user scrolls to show "My Item" at the top of the ListView.
Another log message is added.
I want "My Item" to still show at the top of the ListView.
Dev machine and one Test machine: "My Item" shows at the top of the ListView.
Another Test Machine: "My Item" is now in the second row of the ListView.
The original change request was because this scrolling was not working on some machines but not others. After much time, there seems to be a difference in the machines themselves that is effecting this.
Is there some system setting that would control this?
XAML:
<ListView Grid.Row="1" Grid.Column="9" Name="messagesListView"
Grid.ColumnSpan="3" Margin="8,0,40,0"
ItemsSource="{Binding StatusMessagesList}"
SelectionChanged="messagesListView_SelectionChanged"
PreviewMouseWheel="messagesListView_PreviewMouseWheel"
MouseDoubleClick="batchesListView_MouseDoubleClick"
PreviewMouseDown="messagesListView_MouseDown"
HorizontalAlignment="Stretch">
</ListView>
Code Behind:
private void ScrollToTop()
{
scrollPos = 0;
ScrollSpot.Text = scrollPos.ToString();
ScrollToPos();
}
private void ScrollToPos()
{
ScrollViewer scrollViewer = GetScrollViewer(messagesListView) as ScrollViewer;
if (scrollViewer != null && _viewState)
scrollViewer.ScrollToVerticalOffset(scrollPos);
}
private void autoScrollButton_Click(object sender, RoutedEventArgs e)
{
messagesListView.SelectedItem = null;
autoScrollButton.Visibility = Visibility.Hidden;
_viewState = true;
ScrollToTop();
}
private void ListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count > 0)
{
ScrollViewer scrollViewer = GetScrollViewer(messagesListView) as ScrollViewer;
if (scrollViewer != null && _viewState)
{
ScrollToTop();
}
if(scrollViewer != null && !_viewState)
{
scrollPos += e.NewItems.Count;
ScrollSpot.Text = scrollPos.ToString();
ScrollToPos();
}
}
}
private void messagesListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_viewState = false;
autoScrollButton.Visibility = Visibility.Visible;
}
private void messagesListView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
_viewState = false;
autoScrollButton.Visibility = Visibility.Visible;
}
private void messagesListView_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
_viewState = false;
autoScrollButton.Visibility = Visibility.Visible;
}
private void messagesListView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource.GetType().ToString().IndexOf("Rectangle") >= 0)
{
_viewState = false;
autoScrollButton.Visibility = Visibility.Visible;
}
}
Code to Add to Collection:
{
if (_statusMessagesList == null)
StatusMessagesList = new ObservableCollection<string>();
string stMsg = string.Format("{0} {1} {2}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), message);
StatusMessagesList.Insert(0, stMsg);
Thanks,
Brad P.
Update: I never found out why one system worked and the other did not. However, the solution I found that worked on both was to set messagesListView.CanContentScroll = False
and then manually keep track of the Offset Position on the ScrollViewer:
{
SetUpManualScroll(0);
}
private void messagesListView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
double scrollAmt = (e.Delta / 120) * -48;
SetUpManualScroll(scrollAmt);
}
private void messagesListView_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
SetUpManualScroll(e.NewValue);
}
private void messagesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
SetUpManualScroll(0);
}
private void messagesListView_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource.GetType().ToString().IndexOf("TextBlock") < 0 && e.OriginalSource.GetType().ToString().IndexOf("Border") < 0)
{
if (_scrollviewer.VerticalOffset != 0)
SetUpManualScroll(0);
else
{
messagesListView.SelectedItem = null;
autoScrollButton.Visibility = Visibility.Hidden;
_viewState = true;
ScrollToTop();
}
}
}
private void SetUpManualScroll(double d)
{
_viewState = false;
autoScrollButton.Visibility = Visibility.Visible;
_scrollviewer.UpdateLayout();
double newPos = _scrollviewer.VerticalOffset + d;
if (newPos < 0)
newPos = 0;
ScrollPos = newPos;
}
private void ScrollToTop()
{
ScrollPos = 0;
ScrollToPos();
}
private void ScrollToPos()
{
if (_scrollviewer == null)
{
_scrollviewer = GetScrollViewer(messagesListView);
}
_scrollviewer.UpdateLayout();
_scrollviewer.ScrollToVerticalOffset(ScrollPos);
}

How to enable and disable dragging a button?

I have a form. On this form there is a button and a panel. Both have the same parent: the form. I want to be able to enable or disable the dragging operation of the button. The code I am using is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DraggableControls
{
public partial class Form2 : Form
{
private bool isDragged;
private Point pointOffset;
public Form2()
{
InitializeComponent();
this.Size = new System.Drawing.Size(800, 800);
this.AllowDrop = false;
Button button1 = new Button();
button1.Location = new Point(50, 50);
button1.BackColor = Color.Red;
button1.Size = new System.Drawing.Size(80, 80);
button1.Parent = this;
button1.AllowDrop = false;
button1.Draggable(false);
this.Controls.Add(button1);
isDragged = false;
// For the object that will be dragged:
button1.MouseDown += new MouseEventHandler(button1_MouseDown);
button1.MouseMove += new MouseEventHandler(button1_MouseMove);
button1.MouseUp += new MouseEventHandler(button1_MouseUp);
Panel panel1 = new Panel();
panel1.SuspendLayout();
panel1.Location = new System.Drawing.Point(100, 100);
panel1.Name = "panel1";
panel1.TabIndex = 0;
panel1.Size = new Size(500, 500);
panel1.BackColor = Color.Green;
panel1.AllowDrop = true;
panel1.Parent = this;
this.Controls.Add(panel1);
panel1.ResumeLayout(false);
// For the receiving object:
panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(control_DragEnter);
this.DragEnter += new
System.Windows.Forms.DragEventHandler(control_DragEnter);
this.ResumeLayout(false);
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = senderButton.PointToScreen(new
Point(e.X, e.Y));
pointOffset = new Point();
pointOffset.X = senderButton.Location.X - ptStartPosition.X;
pointOffset.Y = senderButton.Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
if (isDragged)
{
Point newPoint = senderButton.PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(pointOffset);
senderButton.Location = newPoint;
}
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
Point newPoint = senderButton.PointToScreen(new Point(e.X,
e.Y));
isDragged = false;
}
}
private void control_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
}
/// <summary>
/// Prevents Window and any of the controls from being dragged by means
of the mouse.
/// </summary>
/// <param name="message"></param>
protected override void WndProc(ref Message message)
{
int WM_NCLBUTTONDOWN = 0xA1;
int WM_SYSCOMMAND = 0x112;
int HTCAPTION = 0x02;
int SC_MOVE = 0xF010;
if (message.Msg == WM_SYSCOMMAND && message.WParam.ToInt32() ==
SC_MOVE)
{
return;
}
if (message.Msg == WM_NCLBUTTONDOWN && message.WParam.ToInt32() ==
HTCAPTION)
{
return;
}
base.WndProc(ref message);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DraggableControls
{
public static class ControlDragExtension
{
private static Dictionary<Control, bool>
controlDraggabilityByControl = new Dictionary<Control, bool>(); // TKey
is
control to drag, TValue is a Boolean
flag used to determine the dragging state.
private static System.Drawing.Size mouseOffset;
/// <summary>
/// Enable/Disable control dragging.
/// </summary>
public static void Draggable(this Control control, bool enable)
{
if (enable) // Enable control drag feature.
{
if (controlDraggabilityByControl.ContainsKey(control))
{
return; // Return if control is already draggable.
}
controlDraggabilityByControl.Add(control, false); // 'false' -
Initial state is 'control not draggable'.
// Assign required event handlers.
control.MouseDown += new MouseEventHandler(Control_MouseDown);
control.MouseUp += new MouseEventHandler(Control_MouseUp);
control.MouseMove += new MouseEventHandler(Control_MouseMove);
}
else // Disable control drag feature.
{
if (!controlDraggabilityByControl.ContainsKey(control))
{
return; // Return if control is not draggable.
}
// Remove event handlers.
control.MouseDown -= Control_MouseDown;
control.MouseUp -= Control_MouseUp;
control.MouseMove -= Control_MouseMove;
controlDraggabilityByControl.Remove(control);
}
}
static void Control_MouseDown(object sender, MouseEventArgs e)
{
mouseOffset = new System.Drawing.Size(e.Location);
controlDraggabilityByControl[(Control)sender] = true; // Turn
control dragging feature on.
}
static void Control_MouseUp(object sender, MouseEventArgs e)
{
controlDraggabilityByControl[(Control)sender] = false; // Turn
control dragging feature off.
}
static void Control_MouseMove(object sender, MouseEventArgs e)
{
if (controlDraggabilityByControl[(Control)sender] == true) // Only
if dragging is turned on.
{
// Calculation of control's new position.
System.Drawing.Point newLocationOffset = e.Location -
mouseOffset;
((Control)sender).Left += newLocationOffset.X;
((Control)sender).Top += newLocationOffset.Y;
}
}
}
}
Even though I have the statements: this.AllowDrop = false; , panel1.AllowDrop = false; and button1.Draggable(false); and also the override void WndProc method that should disable dragging,
the button1 is always draggable.
I changed the event handlers thus:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
if (enableDragOperations)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = senderButton.PointToScreen(new Point(e.X, e.Y));
pointOffset = new Point();
pointOffset.X = senderButton.Location.X - ptStartPosition.X;
pointOffset.Y = senderButton.Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
if (enableDragOperations)
{
if (isDragged)
{
Point newPoint = senderButton.PointToScreen(new Point(e.X, e.Y));
newPoint.Offset(pointOffset);
senderButton.Location = newPoint;
}
}
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (sender is Button)
{
Button senderButton = (Button)sender;
if (enableDragOperations)
{
Point newPoint = senderButton.PointToScreen(new Point(e.X, e.Y));
isDragged = false;
}
}
}
I defined a global field: bool enableDragOperations and set it to false.

"Microsoft Blend"-like continuous Drag

Microsoft Blend allows changing numeric values of properties like Left, Top etc. through continuous drag. User clicks in the property value box, keeps the button down and drags left or right to decrease/increase the value. Simple.
The special thing about it is that if cursor reaches the left or right end of the screen and user still wants to drag more, they can continue dragging and the cursor will start over from the other end of the screen.
I'm trying to do this in one of my WPF applications using Thumb control. Using DragDetla event, if I find that the Thumb has reach the edge of the screen, I set its position to the far end. But this makes the value of e.HorizontalChange as big as the width of entire screen. How can I change Thumb's position during drag without affecting horizontal change value?
I have realized this in a WPF control by using a textbox and subscribing to events such as:
PreviewMouseDown
MouseUp and
MouseMove
MouseEnter
The drag until you reach screen limits requires a mouse capture or call to CaptureMouse method available on any UIElement. On the other side, you need to release the mouse at some point which requires a call of the ReleaseMouseCapture method. The solution could go like this:
Declare an enumeration to model the drag direction
internal enum MouseDirections
{
None,
LeftRight,
UpDown
}
Declare a class to keep trak of mouse origin (first location) and current location:
internal class MouseIncrementor
{
private MouseDirections _enumMouseDirection = MouseDirections.None;
private Point _objPoint;
private readonly Point _initialPoint;
public MouseIncrementor(Point objPoint, MouseDirections enumMouseDirection)
{
_objPoint = objPoint;
_initialPoint = _objPoint;
_enumMouseDirection = enumMouseDirection;
}
public MouseDirections MouseDirection
{
get
{
return _enumMouseDirection;
}
protected set
{
_enumMouseDirection = value;
}
}
public Point InitialPoint
{
get
{
return _initialPoint;
}
}
public Point Point
{
get
{
return _objPoint;
}
set
{
_objPoint = value;
}
}
internal MouseDirections SetMouseDirection(Point pos)
{
double deltaX = this.Point.X - pos.X;
double deltaY = this.Point.Y - pos.Y;
if (Math.Abs(deltaX) > Math.Abs(deltaY))
MouseDirection = MouseDirections.LeftRight;
else
{
if (Math.Abs(deltaX) < Math.Abs(deltaY))
MouseDirection = MouseDirections.UpDown;
}
return MouseDirection;
}
}
I have a custom control that contains a TextBox named _PART_TextBox:
TextBox _PART_TextBox;
...and field for the MouseIncrementor:
MouseIncrementor _objMouseIncr;
...these are wired up like this:
_PART_TextBox.MouseEnter += _PART_TextBox_MouseEnter;
_PART_TextBox.GotKeyboardFocus += _PART_TextBox_GotKeyboardFocus;
_PART_TextBox.LostKeyboardFocus += _PART_TextBox_LostKeyboardFocus;
_PART_TextBox.MouseMove += _PART_TextBox_MouseMove;
_PART_TextBox.MouseUp += _PART_TextBox_MouseUp;
_PART_TextBox.PreviewMouseDown += _PART_TextBox_PreviewMouseDown;
_PART_TextBox.LostMouseCapture += _PART_TextBox_LostMouseCapture;
and a number of event handlers are required to get this to work:
private void _PART_TextBox_LostMouseCapture(object sender, MouseEventArgs e)
{
_objMouseIncr = null;
}
private void _PART_TextBox_MouseUp(object sender, MouseButtonEventArgs e)
{
if (_objMouseIncr != null)
{
var mouseUpPosition = GetPositionFromThis(e);
if (_objMouseIncr.InitialPoint.Equals(mouseUpPosition))
{
_PART_TextBox.Focus();
}
}
_PART_TextBox.ReleaseMouseCapture();
_objMouseIncr = null;
}
private void _PART_TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (IsKeyboardFocusWithin == false)
{
_objMouseIncr = new MouseIncrementor(this.GetPositionFromThis(e), MouseDirections.None);
e.Handled = true;
}
}
private void _PART_TextBox_MouseMove(object sender, MouseEventArgs e)
{
// nothing to do here
if (_objMouseIncr == null)
return;
if (e.LeftButton != MouseButtonState.Pressed)
return;
if (CanIncreaseCommand() == false && CanDecreaseCommand() == false)
{
// since we can't parse the value, we are out of here, i.e. user put text in our number box
_objMouseIncr = null;
return;
}
var pos = GetPositionFromThis(e);
double deltaX = _objMouseIncr.Point.X - pos.X;
double deltaY = _objMouseIncr.Point.Y - pos.Y;
if (_objMouseIncr.MouseDirection == MouseDirections.None)
{
// this is our first time here, so we need to record if we are tracking x or y movements
if (_objMouseIncr.SetMouseDirection(pos) != MouseDirections.None)
_PART_TextBox.CaptureMouse();
}
if (_objMouseIncr.MouseDirection == MouseDirections.LeftRight)
{
if (deltaX > 0)
OnDecrement(LargeStepSize);
else
{
if (deltaX < 0)
OnIncrement(LargeStepSize);
}
}
else
{
if (_objMouseIncr.MouseDirection == MouseDirections.UpDown)
{
if (deltaY > 0)
{
if (CanIncreaseCommand() == true)
OnIncrease();
}
else
{
if (deltaY < 0)
{
if (CanDecreaseCommand() == true)
OnDecrease();
}
}
}
}
_objMouseIncr.Point = GetPositionFromThis(e);
}
private Point GetPositionFromThis(MouseEventArgs e)
{
return this.PointToScreen(e.GetPosition(this));
}
private void _PART_TextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
_objMouseIncr = null;
(sender as TextBox).Cursor = Cursors.ScrollAll;
}
private void _PART_TextBox_MouseEnter(object sender, MouseEventArgs e)
{
if (IsMouseDragEnabled == false)
return;
if (IsKeyboardFocusWithin)
(sender as TextBox).Cursor = Cursors.IBeam;
else
(sender as TextBox).Cursor = Cursors.ScrollAll;
}
private void _PART_TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
_objMouseIncr = null;
(sender as TextBox).Cursor = Cursors.IBeam;
}
The full project is located here: https://github.com/Dirkster99/NumericUpDownLib
Please let me know if I am missing something or if there are additional questions.

How do I hittest for a TabControl tab?

Given a point relative to a Page, how do I hittest for a TabControl's tab? VisualTreeHelper.HitTest gives me the contents, but when I go up the visual tree I see nothing that would tell me that I have actually hit a tab. I don't even see the tab control itself.
public class ViewManipulationAgent : IDisposable
{
private const int _limit = 125;
private INavigationService _navigationService;
private FrameworkElement _container;
private FrameworkElement _element;
private TranslateTransform _translate;
private IInputElement _touchTarget;
// When I use this object,
// a_container is the main Frame control in my application.
// a_element is a page within that frame.
public ViewManipulationAgent(FrameworkElement a_container, FrameworkElement a_element)
{
_navigationService = a_navigationService;
_container = a_container;
_element = a_element;
// Since I set IsManipulationEnabled to true all touch commands are suspended
// for all commands on the page (a_element) unless I specifically cancel (see below)
_element.IsManipulationEnabled = true;
_element.PreviewTouchDown += OnElementPreviewTouchDown;
_element.ManipulationStarting += OnElementManipulationStarting;
_element.ManipulationDelta += OnElementManipulationDelta;
_element.ManipulationCompleted += OnElementManipulationCompleted;
_translate = new TranslateTransform(0.0, 0.0);
_element.RenderTransform = _translate;
}
// Since the ManipulationStarting doesn't provide position I capture the position
// here and then hit test elements to find any controls for which I want to bypass
// manipulation.
private void OnElementPreviewTouchDown(object sender, TouchEventArgs e)
{
var position = e.GetTouchPoint(_element).Position;
_touchTarget = null;
HitTestResult result = VisualTreeHelper.HitTest(_element, position);
if (result.VisualHit == null)
return;
var button = VisualTreeHelperEx.FindAncestorByType<ButtonBase>(result.VisualHit) as ButtonBase;
if (button != null)
{
_touchTarget = button;
return;
}
var slider = VisualTreeHelperEx.FindAncestorByType<Slider>(result.VisualHit) as Slider;
if (slider != null)
{
_touchTarget = slider;
return;
}
}
// Here is where I cancel manipulation if a specific touch target was found in the
// above event.
private void OnElementManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
if (_touchTarget != null)
{
e.Cancel(); // <- I have to cancel manipulation or the buttons and other
// controls cannot be manipulated by the touch interface.
return;
}
e.ManipulationContainer = _container;
}
private void OnElementManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var element = e.Source as FrameworkElement;
if (element == null)
return;
var translate = _translate.X + e.DeltaManipulation.Translation.X;
if (translate > _limit)
{
GoBack();
translate = _limit;
}
if (translate < -_limit)
{
GoForward();
translate = -_limit;
}
_translate.X = translate;
}
private void GoForward()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
navigationService.GoForward();
}
private void GoBack()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
navigationService.GoBack();
}
private void OnElementManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
_touchTarget = null;
_translate.X = 0;
}
public void Dispose()
{
_element.PreviewTouchDown -= OnElementPreviewTouchDown;
_element.ManipulationStarting -= OnElementManipulationStarting;
_element.ManipulationDelta -= OnElementManipulationDelta;
_element.ManipulationCompleted -= OnElementManipulationCompleted;
}
}

Change the location of the window form in c#?

In window application, using c# i created one form and put visible false minimize, maximize button and formborder to none, i place one panel at top of the form, in that panel i place close, minimize buttons. Now how can i drag the window form. Any reference please. my code is
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.None;
Thank you.
Simply register the MouseDown, MouseMove and MoueUp events for your Panel
bool MouseDownFlag = false;
Point start = new Point(0, 0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
MouseDownFlag = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlag)
{
Point newPoint = new Point();
newPoint.X = this.Location.X - (start.X - e.X);
newPoint.Y = this.Location.Y - (start.Y - e.Y);
this.Location = newPoint;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
MouseDownFlag = false;
}

Resources