Mouse wheel scrolling Toolstrip menu items - c#

I have some menus that contain many menuitems. Mouse wheel doesn't scroll them. I have to use the keyboard arrows or click the arrows at top and bottom.
Is it possible to use the mouse wheel to scroll toolstrip menu items?
Thanks

You can enable it application wide with this class:
public class DropDownMenuScrollWheelHandler : System.Windows.Forms.IMessageFilter
{
private static DropDownMenuScrollWheelHandler Instance;
public static void Enable(bool enabled)
{
if (enabled)
{
if (Instance == null)
{
Instance = new DropDownMenuScrollWheelHandler();
Application.AddMessageFilter(Instance);
}
}
else
{
if (Instance != null)
{
Application.RemoveMessageFilter(Instance);
Instance = null;
}
}
}
private IntPtr activeHwnd;
private ToolStripDropDown activeMenu;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x200 && activeHwnd != m.HWnd) // WM_MOUSEMOVE
{
activeHwnd = m.HWnd;
this.activeMenu = Control.FromHandle(m.HWnd) as ToolStripDropDown;
}
else if (m.Msg == 0x20A && this.activeMenu != null) // WM_MOUSEWHEEL
{
int delta = (short)(ushort)(((uint)(ulong)m.WParam) >> 16);
handleDelta(this.activeMenu, delta);
return true;
}
return false;
}
private static readonly Action<ToolStrip, int> ScrollInternal
= (Action<ToolStrip, int>)Delegate.CreateDelegate(typeof(Action<ToolStrip, int>),
typeof(ToolStrip).GetMethod("ScrollInternal",
System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance));
private void handleDelta(ToolStripDropDown ts, int delta)
{
if (ts.Items.Count == 0)
return;
var firstItem = ts.Items[0];
var lastItem = ts.Items[ts.Items.Count - 1];
if (lastItem.Bounds.Bottom < ts.Height && firstItem.Bounds.Top > 0)
return;
delta = delta / -4;
if (delta < 0 && firstItem.Bounds.Top - delta > 9)
{
delta = firstItem.Bounds.Top - 9;
}
else if (delta > 0 && delta > lastItem.Bounds.Bottom - ts.Height + 9)
{
delta = lastItem.Bounds.Bottom - owner.Height + 9;
}
if (delta != 0)
ScrollInternal(ts, delta);
}
}

A working solution:
Register for MouseWheel event of your form and DropDownClosed event of your root MenuStripItem (here, rootItem) in the Load event of the form
this.MouseWheel += Form3_MouseWheel;
rootItem.DropDownOpened += rootItem_DropDownOpened;
rootItem.DropDownClosed += rootItem_DropDownClosed;
Add the code for Keyboard class which simulate key presses
public static class Keyboard
{
[DllImport("user32.dll")]
static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
const byte VK_UP = 0x26; // Arrow Up key
const byte VK_DOWN = 0x28; // Arrow Down key
const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag, the key is going to be pressed
const int KEYEVENTF_KEYUP = 0x0002; //Key up flag, the key is going to be released
public static void KeyDown()
{
keybd_event(VK_DOWN, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
}
public static void KeyUp()
{
keybd_event(VK_UP, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
}
}
Add the code for DropDownOpened, DropDownClosed, MouseWheel events:
bool IsMenuStripOpen = false;
void rootItem_DropDownOpened(object sender, EventArgs e)
{
IsMenuStripOpen = true;
}
void rootItem_DropDownClosed(object sender, EventArgs e)
{
IsMenuStripOpen = false;
}
void Form3_MouseWheel(object sender, MouseEventArgs e)
{
if (IsMenuStripOpen)
{
if (e.Delta > 0)
{
Keyboard.KeyUp();
}
else
{
Keyboard.KeyDown();
}
}
}

This is very simply using a submenu (ToolStripMenuItem) of the context menu :
Assuming using a form1 (or UserControl) and a contextMenuStrip1 :
private void form1_Load( object sender , EventArgs e )
{
//this.MouseWheel -= When_MouseWheel;
this.MouseWheel += When_MouseWheel;
}
void When_MouseWheel( object sender , MouseEventArgs e )
{
if ( this.contextMenuStrip1.IsDropDown ) {
//this.Focus();
if ( e.Delta > 0 ) SendKeys.SendWait( "{UP}" );
else SendKeys.SendWait( "{DOWN}" );
}
}

I modified Mohsen Afshin's answer to click the up/down arrows instead of sending up/down key presses. My application had a ContextMenuStrip called menu. Here's the code.
In the initialization:
menu.VisibleChanged += (s, e) =>
{
if (menu.Visible)
{
MouseWheel += ScrollMenu;
menu.MouseWheel += ScrollMenu;
}
else
{
MouseWheel -= ScrollMenu;
menu.MouseWheel -= ScrollMenu;
}
};
The ScrollMenu function:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private void ScrollMenu(object sender, MouseEventArgs e)
{
Point origin = Cursor.Position;
int clicks;
if (e.Delta < 0)
{
Cursor.Position = menu.PointToScreen(new Point(menu.DisplayRectangle.Left + 5, menu.DisplayRectangle.Bottom + 5));
clicks = e.Delta / -40;
}
else
{
Cursor.Position = menu.PointToScreen(new Point(menu.DisplayRectangle.Left + 5, menu.DisplayRectangle.Top - 5));
clicks = e.Delta / 40;
}
for (int i = 0; i < clicks; i++)
mouse_event(0x0006, 0, 0, 0, 0);//Left mouse button up and down on cursor position
Cursor.Position = origin;
}
I was having trouble getting the mouse_event function to click a specific location, so I moved the cursor, clicked, and then moved the cursor back. It doesn't seem the cleanest, but it works.

Related

C# Event mouse left and move

Can someone help me complete my code , i don't know how to click and move the image
I found on google the code
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x000000002,
LEFTUP = 0x000000004,
MIDDLEDOWN = 0x000000020,
MIDDLEUP = 0x000000040,
MOVE = 0x000000001,
ABSOLUTE = 0x000008000,
RIGHTDOWN = 0x000000008,
RIGHTUP = 0x000000010,
}
public void mousedown(Point p)
{
Cursor.Position = p;
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
}
Code find image and move it
private void btntestcode_Click(object sender, EventArgs e)
{
var screen = CaptureHelper.CaptureScreen();
var subBitmap = ImageScanOpenCV.GetImage("testmousemove.PNG");
var resBitmap = ImageScanOpenCV.FindOutPoint((Bitmap)screen, subBitmap);
if (resBitmap != null)
{
mousedown((Point)resBitmap);
}
}
If you want to click on your picture and drag it and you want to move it everywhere, you can use this code.
Add a picture box in your form and set your desired image in it:
I loaded an online image
pictureBox1.Load("http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG");
Define two variables to save X and Y globally
private int oldX;
private int OldY;
Add a MouseDown event to the PictureBox that you added in step 1:
if(e.Button == MouseButtons.Left)
{
OldX = e.X;
OldY = e.Y;
}
In the end, add a MouseDown event to the PictureBox:
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += e.X - oldX;
pictureBox1.Top += e.Y - OldY;
}
Here you can see the whole code at once:
public partial class Form1 : Form
{
private int oldX;
private int OldY;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Load("http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG");
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
oldX = e.X;
OldY = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += e.X - oldX;
pictureBox1.Top += e.Y - OldY;
}
}
}

How to cancel left mouse down without click on C#?

I'm try to wrote app, that will send right click, when user take long press left mouse button.
I found https://github.com/gmamaladze/globalmousekeyhook project and hook events with it.
When I hook left up, then send right click with mouse event, first fire left click, than right click fire.
Is any method to cancel first left mouse down?
using Gma.System.MouseKeyHook;
using System;
using System.Windows.Forms;
namespace MouseRClick
{
class ClassRightClick
{
// API
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
// Hook module
private IKeyboardMouseEvents _hook;
private bool _rclick_activated = false;
private int _down_cursor_x;
private int _down_cursor_y;
private Timer timer;
public ClassRightClick(int delay)
{
timer = new Timer();
timer.Interval = delay;
timer.Tick += timer_Tick;
}
public void Subscribe()
{
_hook = Hook.GlobalEvents();
_hook.MouseDownExt += onMouseDown;
_hook.MouseUpExt += onMouseUp;
}
public void Unsubscribe()
{
_hook.MouseDownExt -= onMouseDown;
_hook.MouseUpExt -= onMouseUp;
//It is recommened to dispose it
_hook.Dispose();
}
private void onMouseDown(object sender, MouseEventExtArgs e)
{
if (e.Button == MouseButtons.Left)
{
_down_cursor_x = e.Location.X;
_down_cursor_y = e.Location.Y;
_rclick_activated = false;
timer.Enabled = true;
}
}
private void onMouseUp(object sender, MouseEventExtArgs e)
{
if (e.Button == MouseButtons.Left)
{
timer.Enabled = false;
Unsubscribe();
if (_rclick_activated)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, _down_cursor_x, _down_cursor_y, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, _down_cursor_x, _down_cursor_y, 0, 0);
e.Handled = true;
}
_rclick_activated = false;
Subscribe();
}
}
private void timer_Tick(object sender, EventArgs e)
{
_rclick_activated = true;
}
}
}
Actually, you cannot do this. Problem is, left mouse click is already fired by the time you dispatch the right-mouse click. You can't just go back in time.
I'm try to wrote app, that will send right click, when user take long
press left mouse button.
You can start record the time when receive left mouse button down message by starting a timer. If the timer timeout a WM_TIMER message will be sent out and you can set the _rclick_activated to true to indicate the left button pressed long enough. When handle the left mouse button up message check the _rclick_activated, if it is true send right mouse button down event. After receive right mouse button down message send right mouse button up event.
The following code is Windows desktop API C++ implement as a simple example. You can use as a reference.
// Mouse hook
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0) // do not process the message
return CallNextHookEx(NULL, nCode,
wParam, lParam);
if (WM_LBUTTONDOWN == wParam)
{
OutputDebugString(L"\n Left button down \n");
_rclick_activated = false;
SetTimer(m_windowHandle, // handle to main window
IDT_TIMER1, // timer identifier
2000, // 2-second interval
(TIMERPROC)NULL); // no timer callback
}
else if (WM_LBUTTONUP == wParam)
{
OutputDebugString(L"\n Left button up \n");
if (_rclick_activated)
{
MOUSEINPUT mouseData = {};
mouseData.dx = GET_X_LPARAM(lParam);
mouseData.dy = GET_Y_LPARAM(lParam);
mouseData.dwFlags = MOUSEEVENTF_RIGHTDOWN;
INPUT inputData = {};
inputData.type = INPUT_MOUSE;
inputData.mi = mouseData;
UINT result = SendInput(1, &inputData, sizeof(INPUT));
if (result == 1)
{
OutputDebugString(L"\n successfully insert right button down \n");
}
}
}
else if (WM_RBUTTONDOWN == wParam)
{
OutputDebugString(L"\n Right button down \n");
if (_rclick_activated)
{
MOUSEINPUT mouseData = {};
mouseData.dx = GET_X_LPARAM(lParam);
mouseData.dy = GET_Y_LPARAM(lParam);
mouseData.dwFlags = MOUSEEVENTF_RIGHTUP;
INPUT inputData = {};
inputData.type = INPUT_MOUSE;
inputData.mi = mouseData;
UINT result = SendInput(1, &inputData, sizeof(INPUT));
if (result == 1)
{
OutputDebugString(L"\n successfully insert right button up \n");
}
_rclick_activated = false;
}
}
else if (WM_RBUTTONUP == wParam)
{
OutputDebugString(L"\n Right button up \n");
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
//...
// Rigister mouse hook
HHOOK m_msgHook = SetWindowsHookEx(WH_MOUSE, MouseProc, NULL, GetCurrentThreadId());
//...
//...
case WM_TIMER:
// process the 2-second timer
_rclick_activated = true;
KillTimer(hWnd, IDT_TIMER1);
return 0;
//...

Richtextbox convert a line number to scroll bar position

I'm able now to synchronize my two RichTextBox using this potion of code:
private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private const int SB_THUMBPOSITION = 4;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int GetScrollPos(int hWnd, int nBar);
[DllImport("user32.dll")]
private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
private static extern bool PostMessageA(IntPtr hWnd, int nBar, int wParam, int lParam);
internal int HScrollPos
{
private get { return GetScrollPos((int)this.Handle, SB_HORZ); }
set
{
SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true);
PostMessageA((IntPtr)this.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
}
}
internal int VScrollPos
{
get { return GetScrollPos((int)this.Handle, SB_VERT); }
set
{
SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true);
PostMessageA((IntPtr)this.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
}
}
I can synchronize the RichTextBoxes while key down ,up and Vscroll event.
Indeed this is not my goal, I want to synchronize my RichTextBoxes basing on the content,
What do I need:
Get the current line form non-selected RichTextBox.
Set Scroll bar position using line number in the other RichTextBox (without losing the focus from the current one).
Get line number using scroll bar position.
Note: you are welcome to ask if you need any more details.
Thanks in advance.
From what I understand, you need to synchronize scrolling on 2 RichTextBoxes based on the line number. Leave me a comment if I misunderstood it.
RichTextBox extended :
public class RichTextBoxEx : RichTextBox
{
// combination of multiple events that may cause focus(caret) to change
public event EventHandler FocusChanged;
public RichTextBoxEx()
{
this.KeyPress += (s, e) => RaiseFocusChanged();
this.KeyDown += (s, e) => RaiseFocusChanged();
this.KeyUp += (s, e) => RaiseFocusChanged();
this.MouseClick += (s, e) => RaiseFocusChanged();
}
private void RaiseFocusChanged()
{
var focusChanged = FocusChanged;
if (focusChanged != null)
{
focusChanged(this, null);
}
}
public int GetFirstSelectedLine()
{
var index = GetFirstCharIndexOfCurrentLine();
return GetLineFromCharIndex(index);
}
public int GetFirstVisibleLine()
{
var index = GetCharIndexFromPosition(new Point(1, 1));
return GetLineFromCharIndex(index);
}
public void ScrollToLine(int line)
{
if (line < 0)
throw new ArgumentOutOfRangeException("line cannot be less than 0");
// save the current selection to be restored later
var selection = new { SelectionStart, SelectionLength };
// select that line and scroll it to
Select(GetFirstCharIndexFromLine(line) + 1, 0);
ScrollToCaret();
// restore selection
Select(selection.SelectionStart, selection.SelectionLength);
}
}
Usage :
void Main()
{
var mainScreenArea = Screen.PrimaryScreen.WorkingArea;
var rich1 = new RichTextBoxEx() { Width = mainScreenArea.Width / 2 - 10, Dock = DockStyle.Left };
var rich2 = new RichTextBoxEx() { Width = mainScreenArea.Width / 2 - 10, Dock = DockStyle.Right };
rich1.LoadFile(__RTF_FILE_0__);
rich2.LoadFile(__RTF_FILE_1__);
// pick one :
// synchronize by focus
rich1.FocusChanged += (s, e) => rich2.ScrollToLine(rich1.GetFirstSelectedLine());
// synchronize by viewbox
// rich1.VScroll += (s, e) => rich2.ScrollToLine(rich1.GetFirstVisibleLine());
var form = new Form();
form.Controls.Add(rich1);
form.Controls.Add(rich2);
form.WindowState = FormWindowState.Maximized;
form.ShowDialog()
}

Click when mouse is in an area for some time

I am trying to do something like Kinect adventures with Kinect SDK i.e. when the mouse stays in a certain area for a specific period of time, the native click event is to be fired.
The problem is that I do not get the expected results, since I get random clicking most times. I tried to check with breakpoints etc.
Most often, when my hand is not visible, the cursor goes to the corner of the screen and starts clicking. This is most probably because Math.Abs(lastX - cursorX) < threshold sets to true.
I have tried changing the threshold values to 200, but it fires a click st the start, and afterwards I am not get expected left clicks, when I hold the cursor in a certain position for some time. Any suggestions would be greatly appreciated. Here's the code:
//SkeletonFrameReadyevent
foreach (SkeletonData sd in e.SkeletonFrame.Skeletons)
{
if (sd.TrackingState == SkeletonTrackingState.Tracked)
{
// make sure both hands are tracked
if (sd.Joints[JointID.HandLeft].TrackingState == JointTrackingState.Tracked &&
sd.Joints[JointID.HandRight].TrackingState == JointTrackingState.Tracked)
{
int cursorX, cursorY;
// get the left and right hand Joints
Joint jointRight = sd.Joints[JointID.HandRight];
Joint jointLeft = sd.Joints[JointID.HandLeft];
// scale those Joints to the primary screen width and height
Joint scaledRight = jointRight.ScaleTo((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, SkeletonMaxX, SkeletonMaxY);
Joint scaledLeft = jointLeft.ScaleTo((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, SkeletonMaxX, SkeletonMaxY);
// figure out the cursor position based on left/right handedness
cursorX = (int)scaledRight.Position.X;
cursorY = (int)scaledRight.Position.Y;
//default false, for mouse move, set to true for mouse click
bool leftClick;
if (lastY == 0)
{
lastX = cursorX;
lastY = cursorY;
}
leftClick = false;
if (Math.Abs(lastX - cursorX) < threshold && Math.Abs(lastY - cursorY) < threshold)
{
if (Math.Abs(lastClick.Subtract(DateTime.Now).TotalSeconds) > 1)
{
//Mouse click here
leftClick = true;
}
}
//Mouse click when leftDown is true, else Mousemove
NativeMethods.SendMouseInput(cursorX, cursorY, (int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, leftClick);
return;
}
}
}
NativeMthods.cs class has this function:
public static void SendMouseInput(int positionX, int positionY, int maxX, int maxY, bool leftDown)
{
if(positionX > int.MaxValue)
throw new ArgumentOutOfRangeException("positionX");
if(positionY > int.MaxValue)
throw new ArgumentOutOfRangeException("positionY");
Input[] i = new Input[2];
// move the mouse to the position specified
i[0] = new Input();
i[0].Type = InputMouse;
i[0].MouseInput.X = (positionX * 65535) / maxX;
i[0].MouseInput.Y = (positionY * 65535) / maxY;
i[0].MouseInput.Flags = MouseEventAbsolute | MouseEventMove;
// determine if we need to send a mouse down or mouse up event
if(leftDown)
{
i[1] = new Input();
i[1].Type = InputMouse;
i[1].MouseInput.Flags = MouseEventLeftDown;
i[1].MouseInput.Flags |= MouseEventLeftUp;
}
// send it off
uint result = SendInput(2, i, Marshal.SizeOf(i[0]));
if(result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Try something like this:
public class myMouseClass: IMessageFilter //Must implement IMessageFilter
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
//Constants
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private const int WM_MOUSEMOVE = 0x0200;
private int Threshold = 20; //how far the mouse should move to reset timer
private int StartLocationX = 0; //stores starting X value
private int StartLocationY = 0; //stores starting Y value
private Timer timerHold = new Timer(); //timer to trigger mouse click
//Start Mouse monitoring by calling this. This will also set the timer.
private void StartFilterMouseEvents()
{
timerHold.Interval = 1000; //how long mouse must be in threshold.
timerHold.Tick = new EventHandler(timerHold_Tick);
Application.AddMessageFilter((IMessageFilter) this);
}
//Stop Mouse monitoring by calling this and unset the timer.
private void StopFilterMouseEvents()
{
timerHold.Stop();
timerHold -= timerHold_Tick;
Application.RemoveMessageFilter((IMessageFilter) this);
}
//This will start when Application.AddMessageFilter() is called
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_MOUSEMOVE)
{
if((Cursor.Position.X > StartLocationX + 20)||(Cursor.Position.X > StartLocationX - 20))&&
((Cursor.Position.Y > StartLocationY + 20)||(Cursor.Position.Y > StartLocationY - 20))
{
timerHold.Stop(); //stops timer if running
timerHold.Start(); //starts it again with new position.
StartLocationX = Cursor.Position.X;
StartLocationY = Cursor.Position.Y;
}
}
return false;
}
//This event gets fired when the timer has not been reset for 1000ms
public void timerHold_Tick(object sender, EventArgs e)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}

make a WPF Popup not screen bound

The default behavior of a Popup is if it is placed where it would extend beyond the edge of the screen, the Popup will reposition itself. Is there a way to turn this behavior off?
I have a Popup that the user can drag around the screen. However, when it gets to the edges it gets stuck. It gets stuck on the edge and stays there until the mouse is dragged far from the edge. Also, I have two monitors and when the Popup is dragged to the edge the two monitors share I get flickering. The Popup flickers between the two monitors.
Just use interop to move your popups (drag them)
Here is code for Thumb which will track the drag process
region Thumb
private Thumb mThumb = null;
public Thumb Thumb
{
get { return mThumb; }
set
{
if (mThumb != value)
{
if (mThumb != null)
{
DetachThumb();
}
mThumb = value;
if (mThumb != null)
{
AttachThumb();
}
}
}
}
private void AttachThumb()
{
Thumb.DragStarted += Thumb_DragStarted;
Thumb.DragDelta += Thumb_DragDelta;
Thumb.DragCompleted += Thumb_DragCompleted;
}
private void DetachThumb()
{
Thumb.DragStarted -= Thumb_DragStarted;
Thumb.DragDelta -= Thumb_DragDelta;
Thumb.DragCompleted -= Thumb_DragCompleted;
}
private void Thumb_DragStarted(object sender, DragStartedEventArgs e)
{
mIsThumbDragging = true;
mPreviousDiffX = 0;
mPreviousDiffY = 0;
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
if (mIsMoving)
{
return;
}
mIsMoving = true;
try
{
if (mIsThumbDragging)
{
var doubleDetaX = e.HorizontalChange + mPreviousDiffX;
var doubleDetaY = e.VerticalChange + mPreviousDiffY;
var deltaX = (int)doubleDetaX;
var deltaY = (int)doubleDetaY;
mPreviousDiffX = (double)deltaX - doubleDetaX;
mPreviousDiffY = (double)deltaY - doubleDetaY;
HostPopup.Move(deltaX, deltaY);
}
}
finally
{
mIsMoving = false;
}
}
private void Thumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
mIsThumbDragging = false;
}
#endregion
The HostPopup class is subclass of Popup, and it hase the following methods using interop to move the window:
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
internal void Move(int deltaX, int deltaY)
{
if (mIsMoving)
{
return;
}
mIsMoving = true;
try
{
if (Child == null)
return;
var hwndSource = (PresentationSource.FromVisual(Child)) as HwndSource;
if (hwndSource == null)
return;
var hwnd = hwndSource.Handle;
RECT rect;
if (!GetWindowRect(hwnd, out rect))
return;
MoveWindow(hwnd, rect.Left + deltaX, rect.Top + deltaY, (int)Width, (int)Height, true);
}
finally
{
mIsMoving = false;
}
}
If you want the popup to behave more like a Window, I'd just make a Window instead of a Popup.
Having a popup that doesn't position itself like a standard popup, and allows you to drag it around the screen, just seems like a recipe for low usability and confusion.

Categories

Resources