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.
Related
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()
}
I am trying to paint the split line that appears when you drag a splitter control:
As you can see from this image, the default splitter is a checkerboard.
...this doesn't work:
public partial class MockForm : Form
{
public MockForm()
{
InitializeComponent();
this.splitter1.Paint += splitter1_Paint;
}
private void splitter1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Red);
}
}
this just paints the background of the control but not the splitter when it's dragged.
Any ideas?
The answer posted by LarsTech is really good, But the handler flickers are somehow annoying. Instead of showing the control in Form, if you show a Form as splitter handler and show it above the Container of splitter, the flickers will be gone.
HighLight f = new HighLight() { BackColor = Color.Red };
private void splitter1_SplitterMoving(object sender, SplitterEventArgs e)
{
this.splitter1.Parent.Refresh();
f.Location = this.splitter1.Parent.PointToScreen(new Point(e.SplitX, e.SplitY));
f.Size = this.splitter1.Size;
if (!f.Visible)
f.ShowInactiveTopmost();
}
private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)
{
f.Hide();
}
Here is the form which I used as highlight:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class HighLight : Form
{
public HighLight()
{
Opacity = 0;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
}
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.Hide();
}
private const int SW_SHOWNOACTIVATE = 4;
private const int HWND_TOPMOST = -1;
private const uint SWP_NOACTIVATE = 0x0010;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(int hWnd, int hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public void ShowInactiveTopmost()
{
ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
SetWindowPos(this.Handle.ToInt32(), HWND_TOPMOST,
this.Left, this.Top, this.Width, this.Height,
SWP_NOACTIVATE);
this.Opacity = 1;
}
}
To see a custom splitter which supports transparent handler take a look at this related post. In the other post I created a new splitter control using source codes of original splitter, but changed rendering the highlight:
Change Splitter Highlighting/Resize Line
The old Splitter control uses a private painting method to produce that checkerboard effect, so there isn't any thing you can override to replace that.
You can fake it by dragging your own control in the space of the checkerboard control you see on the screen. This may produce some flicker:
Control draggingControl = new Control { BackColor = Color.Green, Visible = false };
public MockForm() {
InitializeComponent();
this.Controls.Add(draggingControl);
splitter1.SplitterMoving += splitter1_SplitterMoving;
splitter1.SplitterMoved += splitter1_SplitterMoved;
}
void splitter1_SplitterMoving(object sender, SplitterEventArgs e) {
draggingControl.Bounds = new Rectangle(new Point(e.X - (e.X - e.SplitX), 0),
splitter1.Size);
if (!draggingControl.Visible) {
draggingControl.Visible = true;
draggingControl.BringToFront();
}
this.Refresh();
}
void splitter1_SplitterMoved(object sender, SplitterEventArgs e) {
draggingControl.Visible = false;
this.Refresh();
}
The Splitter control was deprecated in favor of the SplitContainer control.
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.
My C# application consists of a taskbar icon (NotifyIcon) and an overhead window initially hidden. I want the user to be able to toggle the window visibility by clicking on the NotifyIcon (left, single click). Also the window is being hidden when loosing focus.
This is what I have so far, a subclassed System.Windows.Forms.Form:
Initialization:
this.ControlBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
// Instance variables: bool allowVisible;
// System.Windows.Forms.NotifyIcon notifyIcon;
this.allowVisible = false;
this.notifyIcon = new NotifyIcon();
this.notifyIcon.MouseUp += new MouseEventHandler(NotifyIconClicked);
this.Deactivate += new EventHandler(HideOnEvent);
Instance methods:
private void NotifyIconClicked(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this.Visible)
this.Hide();
else
this.Show();
}
}
new public void Show()
{
this.allowVisible = true;
this.Visible = true;
this.Activate();
}
new public void Hide()
{
this.allowVisible = false;
this.Visible = false;
}
private void HideOnEvent(object sender, EventArgs e)
{
this.Hide();
}
protected override void SetVisibleCore(bool visible)
{
base.SetVisibleCore(this.allowVisible ? visible : this.allowVisible);
}
Clicking the icon reveals the window like it should. But clicking it again hides it for as long as the mouse is being pressed, then resets it to visible.
My guess is that the mouse down event steals the focus from the window so it disappears. Then the mouse up event is triggered, showing the window as it is hidden.
My next idea was to read the window visibility at mouse down event, so I tested three events and logged the UNIX time as they are called:
notifyIcon.MouseDown
notifyIcon.MouseUp
this.LostFocus
The result is pretty weird: Let's say the window is visible. This happens when I click the icon: Focus lost is called immediately. Mouse down is called as soon as I release the mouse, right before the mouse up event.
1312372231 focus lost
1312372235 mouse down
1312372235 mouse up
Why is the mouse down event delayed?
How can I toggle the window?
I think this may work for you.
I found an expert exchange post which contains a class which provides a method for checking whether the cursor is currently over the tray.
NotifyIcon - Detect MouseOut
Using this class I modified your HideOnEvent method like so:
private void HideOnEvent(object sender, EventArgs e)
{
if (!WinAPI.GetTrayRectangle().Contains(Cursor.Position))
{
this.Hide();
}
}
Which seems to do what you need.
I have included the class below:
using System.Runtime.InteropServices;
using System.Drawing;
public class WinAPI
{
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return "(" + left + ", " + top + ") --> (" + right + ", " + bottom + ")";
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
public static IntPtr GetTrayHandle()
{
IntPtr taskBarHandle = WinAPI.FindWindow("Shell_TrayWnd", null);
if (!taskBarHandle.Equals(IntPtr.Zero))
{
return WinAPI.FindWindowEx(taskBarHandle, IntPtr.Zero, "TrayNotifyWnd", IntPtr.Zero);
}
return IntPtr.Zero;
}
public static Rectangle GetTrayRectangle()
{
WinAPI.RECT rect;
WinAPI.GetWindowRect(WinAPI.GetTrayHandle(), out rect);
return new Rectangle(new Point(rect.left, rect.top), new Size((rect.right - rect.left) + 1, (rect.bottom - rect.top) + 1));
}
}
It is not a perfect solution but I hope this helps.
Is it possible to change the style of a WinForm border? I know that if the border is removed, it takes away the functionality to resize the program. Therefore is there a way to change the style of it, but keep it resizable?
What you seek is not simple because the border is drawn by the operating system. However, there is a library on CodePlex that does make possible to do this very thing.
Drawing Custom Borders in Windows Forms
First write this in the InitializeComponent():
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_RIGHT = 0xB;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Resize_Form);
Then, use a method similar to this. In this case, my form is only resizable from the right side, but should be easy to make it resize from any side:
private void Resize_Form(object sender, MouseEventArgs e)
{
if ((e.Button == MouseButtons.Left) && (MousePosition.X >= this.Location.X + formWidth - 10))
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeWE;
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_RIGHT, 0);
formWidth = this.Width;
}
}
I dont think there is a direct way to do this.
But, you could set the form border style to None.
And implement the resizing in your form(which I don't think its difficult)
string position = String.Empty;
Point mouseDownPosition = new Point();
private void myForm_MouseDown(object sender, MouseEventArgs e)
{
position = (e.X == 0) ? "Left" : ((e.X == myForm.Width) ? "Right" : String.Empty;
position += (e.Y == 0) ? "Top" : ((e.Y == myForm.Height) ? "Bottom" : String.Empty;
if(position != String.Empty)
{
mouseDownPosition = e.Location;
}
}
private void myForm_MouseMove(object sender, MouseEventArgs e)
{
if(position != String.Empty)
{
Point movementOffset = new Point(e.Location.X - mouseDownPosition.X, e.Location.Y - mouseDownPosition.Y);
Switch(position)
{
Case "LeftTop":
myForm.Location.X += movementOffset.X;
myForm.Location.Y += movementOffset.Y;
myForm.Width -= movementOffset.X;
myForm.Height -= movementOffset.Y;
Case "Left":
myForm.Location.X += movementOffset.X;
myForm.Width -= movementOffset.X;
// Complete the remaining please :)
}
}
}
private void myForm_MouseUp(object sender, MouseEventArgs e)
{
position = String.Empty;
}
P.S: Have not yet tested it
Hope you've set FormBorderStyle to None