WPF pop up window disappears from UI unexpectedly - wpf

I have a modal pop up window in my wpf application. After showing that window, in some (rare) situations the window disappeared from the UI. But we can find that using “alt + tab” and can’t make it active.
Please find below the code i used,
//WindowInteropHelper assists interoperation between Windows Presentation Foundation (WPF) and Win32 code.
private void OnControlClick(object sender, RoutedEventArgs e)
{
System.Drawing.Point p = System.Windows.Forms.Control.MousePosition;
MyPopup popup = new MyPopup();
_windowInteropHelper = new System.Windows.Interop.WindowInteropHelper(popup);
_windowInteropHelper.Owner = this._owner;
popup.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
popup.Left = p.X;
popup.Top = p.Y;
popup.Closing += new System.ComponentModel.CancelEventHandler(OnPopupClosing);
popup.ShowDialog();
}

Try to use the IsOpen property to show/hide the popup.

Related

WinForm Touch Events Cefsharp

I have an issue with Winforms and touch inputs via a Microsoft Surface tablet. I know that winforms does not support touch input, but maybe there is something there can be done about this. So in my Winforms application i have a Form with two controls. A simple List Box and CefSharp ChromiumWebBrowser Control.
When I open the application the HTML is loaded and everything works fine with normal mouse input. I can click the textboxes and I can type. But when i do the same thing with touch controls on a MS Surface it seems the the ChromiumWebBrowser Control dows not get focus. The HTML Textboxes inside the Browser have focus, but the ChromiumWebBrowser Control does not. So whem i type, no input is send to the textboxes.
I tried to set focus manually whit C# in winforms, but those click events do not get fired in touch mode except for the Enter event. But this is fired only once. So i dont know how to get around this.
The user needs to be able to click outside the Browser Controle and do something there and then get back to the Browser and type into those textboxes.
Maybe someone had a similar issue and knows a workaround. If other Info is needed, please tell me.
Here is a gif to illustrate, hope it helps: https://i.stack.imgur.com/8J0EZ.gif
Thanks
private void Form1_Load(object sender, EventArgs e)
{
var asForm = System.Windows.Automation.AutomationElement.FromHandle(this.Handle);
wbMap = initBrowser();
this.panel1.Enabled = true;
this.panel1.Controls.Add(wbMap);
wbMap.FrameLoadEnd += WbMap_FrameLoadEnd;
wbMap.Click += WbMap_Click;
wbMap.Enter += WbMap_Enter;
wbMap.MouseClick += WbMap_MouseClick;
wbMap.DoubleClick += WbMap_DoubleClick;
wbMap.Leave += WbMap_Leave;
wbMap.LostFocus += WbMap_LostFocus;
wbMap.GotFocus += WbMap_GotFocus;
wbMap.PreviewKeyDown += WbMap_PreviewKeyDown;
}
private ChromiumWebBrowser initBrowser()
{
ChromiumWebBrowser wbMap = new ChromiumWebBrowser();
BrowserSettings browserSettings = new BrowserSettings();
browserSettings.FileAccessFromFileUrls = CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
wbMap.BrowserSettings = browserSettings;
wbMap.LoadHtml("<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta charset=\"utf-8\" /><title></title></head><body><input type=\"text\"><input /></body></html>");
wbMap.Dock = DockStyle.Fill;
return wbMap;
}
private void WbMap_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
Console.WriteLine("QGIS_WbMap_FrameLoadEnd");
}

windows forms application - how to make the tabs in a tab control get full width?

So i'am working with tabControl in windows forms application and i want to make the tabs get full width regardless whether the application window is maximized or not.
When the window isn't maximized everything appears great:
But when the window gets maximized the tabs doesn't get the full width:
Is there any known way to fix this problem?
Thanks in advance
You can achieve this in some way by modifying the ItemSize property as described bellow, else you'd have to draw the tab page selectors yourself.
public Form1()
{
InitializeComponent();
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}
//Hook to form or parent container Resize event, either Resize or ResizeEnd.
private void Form1_ResizeEnd(object sender, EventArgs e)
{
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}

How to open a WPF window that has a Windows Form parent at mouse position?

I have a windows forms window application. Inside the application we want to open a WPF window that is positioned at a location relative to the mouse position. Let's say, that the window center may be displayed at the mouse coordinates, or another case, that the top left corner of the window may be set as being the mouse coordinates.
I've looked at posts like http://www.formatexception.com/2008/09/wpf-and-mouseposition/
but this does not help me, as I don't have a WPF control open before my window. I only have the windows forms, so the folllowing line is not usable in my case
Point mousePoint = Mouse.GetPosition(this);
since you have a winforms control available, you can use Control.MousePosition
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition.aspx
Are you showing the wpf window from a winforms form? I wrote a quick test that had a Form with a button. When clicking the button it opened a wpf window at the cursor location. This worked fine and i did not create or show any WPF controls before the button click.
I tried with and without setting WindowStartupLocation and they both worked, but it might be worth a try for you to add it. Here's an example:
private void button1_Click(object sender, EventArgs e)
{
Window w = new Window();
w.WindowStartupLocation = WindowStartupLocation.Manual;
w.Left = Control.MousePosition.X;
w.Top = Control.MousePosition.Y;
w.Show();
}
Though, if the above code sample doesn't work for you then perhaps you could describe your scenario a little bit further and include some code examples?
Building on Bill Tarbell's answer, you may need to account for DPI scaling:
private Point GetScalingFactor(Window window)
{
var zeroPoint = window.PointToScreen(new Point(0, 0));
var hundredPoint = window.PointToScreen(new Point(100, 100));
return new Point(
100.0 / (hundredPoint.X - zeroPoint.X),
100.0 / (hundredPoint.Y - zeroPoint.Y));
}
private void ShowAtCursor(Window parent, Window toShow)
{
var point = parent.PointToScreen(System.Windows.Input.Mouse.GetPosition(parent));
var scaling = GetScalingFactor(parent);
toShow.Left = point.X * scaling.X;
toShow.Top = point.Y * scaling.Y;
toShow.WindowStartupLocation = WindowStartupLocation.Manual;
toShow.Show();
}
Bill Tarbell's answer was not working on my WPF program.
I could not include "using System.Windows.Forms;" somehow...
Could be different project type..
But his answer was very helpful so I put one up vote to him.
and below is the modified code that worked for me.
private void button1_Click(object sender, EventArgs e)
{
Point pointToScreen = PointToScreen(Mouse.GetPosition(this));
Window w = new Window();
w.WindowStartupLocation = WindowStartupLocation.Manual;
w.Left = pointToScreen.X;
w.Top = pointToScreen.Y;
w.Show();
}

How to open a Xaml page in a separate window Other than MainPage.Xaml

All the below functionality should be worked in Out-of-Browser mode.
On start of my project there is login page after login in the MainPage.xaml there is a button,on click of this button a new page should be opened with WebBrowser control in the page there is grid control,as the grid selected item changes it should be reflected to the 1st window.
Please help me
Thanks
I think it's not possible in silverlight 4.
So I updated my project to silverlight 5 and use Window class for out-of-browser app. to achieve my requirement
WebBrower wb = new WebBrowser();
wb.ScriptNotify += new EventHandler<NotifyEventArgs>(b_ScriptNotify);
Window win = new Window();
win.Height = 870;
win.Width = 600;
win.Title = "Site Map";
win.Content = wb;
win.Visibility = Visibility.Visible;
void b_ScriptNotify(object sender, NotifyEventArgs e)
{
MessageBox.Show(Convert.ToInt32(e.Value).ToString());
}

How to autosize User control when it's shown

I have a Winforms project with a bunch of User controls. I'd like the user control to move itself to be at the 1,1 position relative to its container (whether it's a form or a Panel control). And resize itself to be able to fit half the container.
What event can I respond to in the UserControl to be able to do this without any code having to be written in the container (e.g. the form or panel).
You could use the basic Load event.
The code could be like this:
private void UserControl1_Load(object sender, EventArgs e)
{
Control parent = this.Parent;
if (parent != null)
{
this.Location = new Point(1, 1);
this.Width = (parent.Width / 2);
this.Height = (parent.Height / 2);
}
}
The Paint event should work, unless I'm missing something.

Resources