Use current display scale in WPF ContextMenu - wpf

My WPF application is Per-Monitor Aware and normally scales well. But when the app window is on a second monitor with scale=150% and I open a context menu it uses scale=100% from the main display making menu items quite small:
(Interestingly the submenu items use correct 150% scale)
I open the context menu setting IsOpen = true:
private void Button_Click(object sender, RoutedEventArgs e)
{
ContextMenu menu = new ContextMenu();
menu.Items.Add("item 1");
menu.Items.Add("item 2");
MenuItem submenu = new MenuItem();
submenu.Header = "submenu";
submenu.Items.Add("more 1");
submenu.Items.Add("more 2");
menu.Items.Add(submenu);
menu.IsOpen = true;
}
How to configure context menu to use display settings it is opened on, not from the default main display?

You can RenderTransform to undo Scaling from PrimaryScreen and Scale to dpiScale from Visual:
var visual = e.Source as Visual;
if (visual != null)
{
var dpiScale = VisualTreeHelper.GetDpi(visual);
System.Windows.Forms.Screen.PrimaryScreen.
GetDpi(DpiType.Effective, out uint dpiX, out uint dpiY);
menu.RenderTransform =
new ScaleTransform(dpiScale.DpiScaleX / ((double)dpiX / 96),
dpiScale.DpiScaleY / ((double)dpiY / 96));
menu.IsOpen = true;
}

Related

Can I make a WPF ListBox change selection on left button press only

I have a WPF ListBox operating in single selection mode. I am adding drag and drop to move items around. Currently the ListBox selection responds to both left button pressed and then with mouse moves with left button down. So after I wait for the MinimumVerticalDragDistance to start a drag operation, a different item could be selected. Dragging either the unselected orginal item or dragging the new selected item is confusing. Adding 'e.Handled=true' in xxx_MouseMove or xxx_PreviewMouseMove does not do anything. Any ideas on suppressing this selection due to mouse moves with left button down?
The best kludge I came up with is to cancel the ListBox's "Selection by dragging" in the IsMouseCapturedChanged event.
public partial class MainWindow : Window
{
Rect? dragSourceGestureRect;
bool busy;
public MainWindow()
{
InitializeComponent();
listBox.ItemsSource = Enumerable.Range(1, 9);
listBox.PreviewMouseLeftButtonDown += listBox_PreviewMouseLeftButtonDown;
listBox.IsMouseCapturedChanged += listBox_IsMouseCapturedChanged;
listBox.MouseMove += listBox_MouseMove;
}
void listBox_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (busy)
return;
if (!listBox.IsMouseCaptured)
dragSourceGestureRect = null;
else if (dragSourceGestureRect.HasValue)
{
busy = true;
{
//tell the ListBox to cancel it's "Selection by dragging"
listBox.ReleaseMouseCapture();
//Now recapture the mouse for canceling my dragging
listBox.CaptureMouse();
}
busy = false;
}
}
void listBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var center = e.GetPosition(listBox);
dragSourceGestureRect = new Rect(
center.X - SystemParameters.MinimumHorizontalDragDistance / 2,
center.Y - SystemParameters.MinimumVerticalDragDistance / 2,
SystemParameters.MinimumHorizontalDragDistance,
SystemParameters.MinimumVerticalDragDistance);
}
void listBox_MouseMove(object sender, MouseEventArgs e)
{
if (!dragSourceGestureRect.HasValue || dragSourceGestureRect.Value.Contains(e.GetPosition(listBox)))
return;
dragSourceGestureRect = null;
var data = new DataObject(DataFormats.UnicodeText, "The Data");
DragDrop.DoDragDrop(listBox, data, DragDropEffects.Copy);
e.Handled = true;
}
}

Render WPF control on top of WindowsFormsHost

I know that default WPF behavior is to render WPF controls and then on top render WinForms, but are there any way to render WPF on top of WindowsFormsHost?
Edit: I have found a temp hack as well. When wpf control overlaps WindowsFormsHost, I change the size of the WindowsFormsHost (This only works when you have rectangular object which overlaps, doesn't work for other shapes.)
Late to the party, I know, but I recently came across this issue using a WebBrowser control.
The final fix was to create a screenshot of the web browser whenever I hosted a modal dialog over the top. Since this was a little fiddly, I turned it into a Github project, hopefully this helps a little -
https://github.com/chris84948/AirspaceFixer
(It's on Nuget too, under AirspaceFixer)
Once you have the project all you need to do is this
xmlns:asf="clr-namespace:AirspaceFixer;assembly=AirspaceFixer"
<asf:AirspacePanel FixAirspace="{Binding FixAirspace}">
<WebBrowser x:Name="Browser" />
</asf:AirspacePanel>
Where FixAirspace is the dependency property that switches from the "real" view of the content, to the screenshot or "fake" view.
This "airspace" issue is suppose to be fixed in WPF vNext. There are a couple solutions out there, such as here, here, and here.
One way to do this is to host the WPF content in a transparent Popup or Window, which overlays the Interop content.
Try this on for size:
<hacks:AirspaceOverlay>
<hacks:AirspaceOverlay.OverlayChild>
<Canvas ToolTip = "A tooltip over a DirectX surface" Background="#01000000" Name="Overlay" />
</hacks:AirspaceOverlay.OverlayChild>
<controls:OpenGLControlWrappingWindowsFormsHost />
</hacks:AirspaceOverlay>
// Adapted from http://blogs.msdn.com/b/pantal/archive/2007/07/31/managed-directx-interop-with-wpf-part-2.aspx & http://www.4mghc.com/topics/69774/1/in-wpf-how-can-you-draw-a-line-over-a-windowsformshost
public class AirspaceOverlay : Decorator
{
private readonly Window _transparentInputWindow;
private Window _parentWindow;
public AirspaceOverlay()
{
_transparentInputWindow = CreateTransparentWindow();
_transparentInputWindow.PreviewMouseDown += TransparentInputWindow_PreviewMouseDown;
}
public object OverlayChild
{
get { return _transparentInputWindow.Content; }
set { _transparentInputWindow.Content = value; }
}
private static Window CreateTransparentWindow()
{
var transparentInputWindow = new Window();
//Make the window itself transparent, with no style.
transparentInputWindow.Background = Brushes.Transparent;
transparentInputWindow.AllowsTransparency = true;
transparentInputWindow.WindowStyle = WindowStyle.None;
//Hide from taskbar until it becomes a child
transparentInputWindow.ShowInTaskbar = false;
//HACK: This window and it's child controls should never have focus, as window styling of an invisible window
//will confuse user.
transparentInputWindow.Focusable = false;
return transparentInputWindow;
}
void TransparentInputWindow_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
_parentWindow.Focus();
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
UpdateOverlaySize();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (_transparentInputWindow.Visibility != Visibility.Visible)
{
UpdateOverlaySize();
_transparentInputWindow.Show();
_parentWindow = GetParentWindow(this);
_transparentInputWindow.Owner = _parentWindow;
_parentWindow.LocationChanged += ParentWindow_LocationChanged;
_parentWindow.SizeChanged += ParentWindow_SizeChanged;
}
}
private static Window GetParentWindow(DependencyObject o)
{
var parent = VisualTreeHelper.GetParent(o);
if (parent != null)
return GetParentWindow(parent);
var fe = o as FrameworkElement;
if (fe is Window)
return fe as Window;
if (fe != null && fe.Parent != null)
return GetParentWindow(fe.Parent);
throw new ApplicationException("A window parent could not be found for " + o);
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdateOverlaySize();
}
private void ParentWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateOverlaySize();
}
private void UpdateOverlaySize()
{
var hostTopLeft = PointToScreen(new Point(0, 0));
_transparentInputWindow.Left = hostTopLeft.X;
_transparentInputWindow.Top = hostTopLeft.Y;
_transparentInputWindow.Width = ActualWidth;
_transparentInputWindow.Height = ActualHeight;
}
}
Here's a link to the best answer I've seen on this subject so far:
Can I overlay a WPF window on top of another?
If anyone finds themselves unsatisfied with the hacks, setting the Visibility of the WindowsFormsHost to Collapsed or Hidden is always an option.
I came across this issue while trying to create a MDI style interface hosting WinForms controls while porting a win forms app to WPF.
I managed to solve it by doing something like this:
WindowsFormsHost -> ElementHost -> WindowsFormsHost -> my win forms controls.
Its super ugly, but it creates windows layers for the WPF content to be under the WinForms content.

WPF: Get specify image from touch

I was added picture as a children to layer called "canvas". By the following code:
if (addChild)
{
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(path, UriKind.Absolute);
src.EndInit();
i.Source = src;
i.Width = 200;
i.IsManipulationEnabled = true;
double rotAngle = Rand.GetRandomDouble(-3.14/4, 3.14/4);
i.RenderTransform = new MatrixTransform(Math.Cos(rotAngle), -Math.Sin(rotAngle),
Math.Sin(rotAngle), Math.Cos(rotAngle), Rand.GetRandomDouble(0, this.Width - i.Width), Rand.GetRandomDouble(0, this.Height - i.Width));
canvasImages.Add(i);
canvas.Children.Add(i);
Canvas.SetZIndex(i, canvas.Children.Count-1);
addedFiles.Add(path);
maxZ++;
}
Here is the problem. I'm trying to make an event called "canvas_TouchDown" which can detect the specify picture when I touched it so that it will get the center of that image object.
List<Image> canvasImages = new List<Image>();
private void canvas_TouchDown(object sender, TouchEventArgs e)
{
foreach (Image canvasImage in canvasImages)
{
if (canvasImage.AreAnyTouchesCaptured == true)
{
System.Diagnostics.Debug.WriteLine("I found image that you touch");
}
}
}
However, there is nothing happened. I also try to use PersistId property but it doesn't work. Have any suggestion?
Regard,
C.Porawat
If you are adding the image to the canvas, touching it and expecting the canvas to receive the touch you will be disappointed. You should either listen to "touch down" on the image or "preview touch down" on the canvas.

Change border color of Windows Forms Control on focus

Is there a way to change a border color of some common controls in Windows Forms (TextBox, ComboBox, MaskedTextBox, ...) when they are in focus? I would like to achieve that in my dialog, so when control is in focus it's border becomes blue?
I suggest to draw a rectangle around the active control as the following:
I need a method to get all the controls which in the form, even which they're in nested Panel or GroupBoxe.
The method:
// Get all controls that exist in the form.
public static List<Control> GetAllControls(IList controls)
{
List<Control> controlsCollectorList = new List<Control>();
foreach (Control control in controls)
{
controlsCollectorList.Add(control);
List<Control> SubControls = GetAllControls(control.Controls);
controlsCollectorList.AddRange(SubControls);
}
return controlsCollectorList;
}
Then.. Drawing functionality..
The code:
public Form1()
{
InitializeComponent();
// The parents that'll draw the borders for their children
HashSet<Control> parents = new HashSet<Control>();
// The controls' types that you want to apply the new border on them
var controlsThatHaveBorder = new Type[] { typeof(TextBox), typeof(ComboBox) };
foreach (Control item in GetAllControls(Controls))
{
// except the control if it's not in controlsThatHaveBorder
if (!controlsThatHaveBorder.Contains(item.GetType())) continue;
// Redraw the parent when it get or lose the focus
item.GotFocus += (s, e) => ((Control)s).Parent.Invalidate();
item.LostFocus += (s, e) => ((Control)s).Parent.Invalidate();
parents.Add(item.Parent);
}
foreach (var parent in parents)
{
parent.Paint += (sender, e) =>
{
// Don't draw anything if this is not the parent of the active control
if (ActiveControl.Parent != sender) return;
// Create the border's bounds
var bounds = ActiveControl.Bounds;
var activeCountrolBounds = new Rectangle(bounds.X - 1, bounds.Y - 1, bounds.Width + 1, bounds.Height + 1);
// Draw the border...
((Control)sender).CreateGraphics().DrawRectangle(Pens.Blue, activeCountrolBounds);
};
}
}
Good luck!

Using IVMRWindowlessControl to display video in a Winforms Control and allow for full screen toggle

I've recently switched from using the IVideoWindow interface to IVMRWindowlessControl in my custom Winforms control to display video.
The reason for this was to allow zoom capabilities on the video within the control.
However in switching over, I've found that the FullScreen mode from IVideoWindow is not available and I am currently trying to replicate this using the SetVideoWindow() method.
I'm finding that I size the video in my control to be at the same resolution as the screen however I can't get the control to position itself to the top/left of the screen and become the top most window.
Any ideas on how to achieve this since the IVideoWindow::put_FullScreenMode just did it all for you?
Resolved the FullScreen problem by hosting the video control in a fresh form which I resized to the size of the current screen, then handled the 'Escape' key press in the form, to toggle back to the normal size video. Here's an extract of the code:-
Members
private Rectangle fullScreenRectangle;
private bool fullScreen;
private Form fullScreenForm;
private Control fullScreenParent;
Toggle FullScreen code
/// <summary>
/// Toggle Full Screen Mode
/// </summary>
public bool FullScreen
{
get
{
return this.fullScreen;
}
set
{
this.fullScreen = value;
if (this.fullScreen)
{
// If switch to full screen, save the current size of the control
this.fullScreenRectangle = new Rectangle(this.Location, this.Size);
// Get the current screen resolution and set that to be the control's size
Rectangle screenRect = Screen.GetBounds(this);
// Create a new form on which to host the control whilst we go to full screen mode.
this.fullScreenForm = new Form();
this.fullScreenForm.Location = PointToScreen(new Point(0, 0));
this.fullScreenForm.Size = new Size(screenRect.Width, screenRect.Height);
this.fullScreenForm.BackColor = Color.Black;
this.fullScreenForm.ShowInTaskbar = false;
this.fullScreenForm.ShowIcon = false;
this.fullScreenForm.FormBorderStyle = FormBorderStyle.None;
this.fullScreenForm.KeyPreview = true;
this.fullScreenForm.PreviewKeyDown += new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenParent = this.Parent;
this.fullScreenForm.Controls.Add(this);
this.fullScreenForm.Show();
this.windowlessControl.SetVideoPosition(null, screenRect);
}
else
{
// Revert to the original control size
this.Location = PointToScreen(new Point(this.fullScreenRectangle.Left, this.fullScreenRectangle.Top));
this.Size = new Size(this.fullScreenRectangle.Width, this.fullScreenRectangle.Height);
this.windowlessControl.SetVideoPosition(null, this.fullScreenRectangle);
if (this.fullScreenForm != null)
{
this.fullScreenForm.Controls.Remove(this);
if (this.fullScreenParent != null)
this.Parent = this.fullScreenParent;
this.fullScreenForm.PreviewKeyDown -= new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenForm.Close();
}
}
}
}
void fullScreenForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
var viewer = this.Controls[0] as ViewerControl;
if (viewer != null)
viewer.FullScreen = false;
}
}

Resources