Start another application on top most in tablet mode - c#

When I run another .exe from my application it starts in the background and does not show the application on top of the screen instead shows tablet mode home screen , it's working fine in normal desktop mode but when I run it in Windows 10 Tablet mode then it does not show on top it starts in the background.
I've used myWindow.TopMost = true; , but it does not work as intended in Windows 10 Tablet Mode.
Code used to start exe file
Process p = new Process();
p.StartInfo.RedirectStandardOutput= true;
p.RedirectStandardInput = true;
p = Process.Start("myApp.exe");
p.WaitForExit();
the exe I'm invoking(starting) is my own exe application(it's not system app), I'm running app on windows 10.
It's only not working on top in Tablet mode( and I'm targeting my application only for Tablets).
Any help is appreciated..!

As I faced a similar situation, (it is not tablet, or windows-10 related. Has similarities only by WPF and TopMost tags) I'll show you how I resolve it:
I would like to have the FilterWindow always TopMost (but only over my application, not over entire set of apps in my Operating System)
See my code. May it'll helps you.
private void OnFilter() {
var filterViewModel = ViewModelLocator.FilterViewModel;
/* ... */
var filterWindow = new FilterWindow {
DataContext = filterViewModel,
Owner = GetParentWindow()
};
filterWindow.ShowDialog();
SelectedIndex = 0;
}
private static Window GetParentWindow() {
Window parent = null;
var activeWindows = Application.Current.Windows.Cast<Window>().Where(item => (item).IsActive).ToList();
if (activeWindows.Any()) {
parent = activeWindows[activeWindows.Count - 1];
}
else {
foreach (var item in
Application.Current.Windows.Cast<object>().Where(item => item.GetType().Name == typeof(RibbonWindow).Name)) {
parent = item as Window;
}
}
return parent;
}
The magic is Owner = GetParentWindow().
Without setting the Owner the FilterWindow had a ridiculous behavior.
Hope it helps you. If no, I will remove the response. (it does not fit in a comment)

Moerfi's solution of using Owner = GetParentWindow() worked surperb, much thanks for this solution. It also solved another problem I had.
I am writing an application for Surface 3 that runs on Windows 10 Pro on tablet mode, whenever a MessageBox or custom dialog control box is closed, as opposed to going back to parent window, Win 10 goes to start menu.
As if once the dialog control is opened the parent window is being placed into background, so when dialog control is closed there is no active window for Win 10 to switch back.
Setting owner on child dialog control solved that problem. Thank you very much.

Related

Give focus to WPF application if other instances are running

I want to create a Spotify-like behavior for my WPF application. When the application is running, but minimized in the taskbar and the user tries to start a new instace, it should restore it from the taskbar, and give focus to the already running instance. The problem is stumble upon is to open to application from minimized state in the taskbar to be in focus. It works if the application is open, but other windows are placed over it.
var processHndl = FindWindow(null, "MyApplicationName!");
if (processHndl != IntPtr.Zero)
{
var isIconic = IsIconic(processHndl);
MessageBox.Show("Iconic: " + isIconic);
if (IsIconic(processHndl))
{
ShowWindow(processHndl, 9);
}
SetForegroundWindow(processHndl);
}
Any ideas?
Please try
SwitchToThisWindow
Windows Function Reference.

c# exe encapsulation in wpf not work

I need to encapsulate exe on my wpf application.
My wpf application is very large and with many UserControls.
To do this, i've start the exe from my code, then get the handle and used the "setParent" to "bind" the exe to my application, but the only effect is to show the drop down menu of the exe, but not the main page. For example: i've tried to embedded notepad, but appear only the drop down menu when I click in the area (note that not appear the main menu bar).
var procInfo = new System.Diagnostics.ProcessStartInfo(this.exeName);
procInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(this.exeName);
// Start the process
_childp = System.Diagnostics.Process.Start(procInfo);
// Wait for process to be created and enter idle condition
_childp.WaitForInputIdle();
// Get the main handle
_appWin = _childp.MainWindowHandle;
// Get main window handle
var helper = new WindowInteropHelper(Window.GetWindow(this.AppContainer));
// Incapsulate
SetWindowLongA(_appWin, -20, 0x00000040 | 0x00000008);
SetParent(_appWin, helper.Handle);
Note that I've tried this piece of code in other c# application and work fine!
I think there is a problem of redraw/update the viewport.
In which way can i force this redraw of the external exe inside my application?
Can you help me, even to found an alternative solution to embedded the exe? Thanks
I've tried the solution to run the exe in a separate tab (here), but even this solution not work.
Can I resolve this with a "SendMessage" ???
Can you suggest me a test to do?
I ask you one thing: help me!!!
The below works for me, can provide you with example project if necessary. The missing piece seems to be that either you have a z index issue OR your initial window placement in your desktop co-oridinates is such that is it outside your 'outer window'.
this will bring it the the from and make it FILL your window:
SetWindowPos(_appWin, default(IntPtr), 0, 0, (int)Application.Current.MainWindow.Width, (int)Application.Current.MainWindow.Height, SetWindowPosFlags.FrameChanged);
The default(IntPtr) is for the ZIndex and says 'bring to front'
You can then make that smaller by passing in the offsets from your containing control, ie if this.grid was what you wanted notepad to appear over:
var desiredPos = this.grid.TranslatePoint(new Point(0, 0), Window.GetWindow(this.grid));
SetWindowPos(_appWin, default(IntPtr),
(int)desiredPos.X, (int)desiredPos.Y,
(int)this.grid.ActualWidth, (int)this.grid.ActualHeight, SetWindowPosFlags.FrameChanged);
Using AllowsTransparency="False" instead AllowsTransparency="True" inside the WPF of the Window I've been able to solve partially the problem
Now I've embedded the external exe (for example: "notepad.exe") using this approach (WindowsFormHost approach):
System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel();
System.Windows.Forms.Integration.WindowsFormsHost windowsFormsHost1 =
new System.Windows.Forms.Integration.WindowsFormsHost();
windowsFormsHost1.Child = _pnlSched;
_grid.Children.Add(windowsFormsHost1);
ProcessStartInfo psi = new ProcessStartInfo(#"notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Normal;
Process PR = Process.Start(psi);
PR.WaitForInputIdle();
SetParent(PR.MainWindowHandle, _pnlSched.Handle);
Now the new problem may be the Z-order of the user control. In fact, when another user contol move above the "notepad", it is below and not above...
enter image description here
Note that also the background of the WindowsFormHost not respect the 'z-order':
enter image description here
any suggestion is welcome
Thanks

C# & XAML Popup doesn't show in screenshot

I have many popups in a custom GUI application. These popups are window objects, not popup objects. The popups do not show up in a screenshot when using the Print Screen button on the keyboard. Instead, the disabled mainwindow below is all that shows in the screenshot. The popup never flickers or disappears, it just doesn't show in the screenshot.
WindowInstance.IsEnabled = true;
WindowInstance.Refresh();
DisplayPopUpWindow(WindowInstance);
The code in DisplayPopupWindow:
private void DisplayPopUpWindow(Window theWindow)
{
if (theWindow != null)
{
if (theWindow is MessagePopup)
{
// Only show one popup at a time (queue is handled elsewhere)
RemovePopUpsCommand.Execute(true, null);
}
ActiveWindow.IsEnabled = false; // main screen disabled
foreach (KeyValuePair<Window, int> aPopup in popUpWindows)
{
if ((aPopup.Key.IsVisible) && (aPopup.Key.Opacity == 1) && (aPopup.Key != theWindow))
{
// if window is a lower priority then disable it
if (aPopup.Value > displayPriority)
aPopup.Key.IsEnabled = false;
}
}
theWindow.Show();
theWindow.Opacity = 1;
}
}
Is there some property in the XAML that affects whether the window is visible for screenshots? This is a large issue as this also affects some remoting software we use in that popups do not display on the shared screen. Also affects our combobox implementation.
The "popups" are actually their own standalone windows. Some have instances created once during application startup and simply shown/hidden when needed, however, most are created on demand to be displayed. This problem affects both types.
The remoting software used is axeda access remote.
If I remember correctly I had the same problem and I think it was related to setting the popup windows parent to the main window that fixed it, I'd have to look at my code at home to confirm.
So make sure this is correctly set.
EDIT:
Try using this code when you create the Window object:
MainWindowVariable.Owner = Window.GetWindow(this)
You can read more here:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner(v=vs.110).aspx

How do I make a tray-icon-only C# application in MonoMac (no dock icon)?

I am trying to create an application that will have a tray icon only, and not appear in the taskbar. (similar to Dropbox) I need to create both Windows and Mac version of the application, so I tried using MonoMac to create the Mac front-end.
What is the best way to create a tray-only application in MonoMac?
All the resources I have found say to do one of two things:
Add <key>LSUIElement</key><string>1</string> to the Info.plist file.
Add the following code to the FinishedLaunching event in the AppDelegate class: NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Accessory;
I have tried all combinations of these two, but it seems that as soon as I try to instantiate a C# System.Timers.Timer, the icon reappears in the dock at the bottom of the screen. Am I missing something about how OSX handles background applications?
What am I doing wrong? Is there a better way to make a background application that has an upper tray icon but no bottom dock icon in OSX?
(This is very similar to this SO question, but that question was from a couple years ago and was never fully answered, so I'm hoping there might be a more complete answer out there.)
Here's the code I have so far:
public partial class AppDelegate : NSApplicationDelegate
{
MyServiceObject currentServiceObject;
public AppDelegate () { }
public override void FinishedLaunching (NSObject notification)
{
// Construct menu that will be displayed when tray icon is clicked
var notifyMenu = new NSMenu();
var exitMenuItem = new NSMenuItem("Quit My Application",
(a,b) => { System.Environment.Exit(0); }); // Just add 'Quit' command
notifyMenu.AddItem(exitMenuItem);
// Display tray icon in upper-right-hand corner of the screen
var sItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
sItem.Menu = notifyMenu;
sItem.Image = NSImage.FromStream(System.IO.File.OpenRead(
NSBundle.MainBundle.ResourcePath + #"/notify-icon.icns"));
sItem.HighlightMode = true;
// Remove the system tray icon from upper-right hand corner of the screen
// (works without adjusting the LSUIElement setting in Info.plist)
NSApplication.SharedApplication.ActivationPolicy =
NSApplicationActivationPolicy.Accessory;
// Start running the program -- If I comment out then no dock icon appears
currentServiceObject = new MyServiceObject();
}
}
I found the problem, and it wasn't related to the application settings at all. Evidently, there are some operations that MacOS does not allow an 'Agent applications' to perform. As soon as one of those methods is called, the application is forced to appear in the dock. The code that was tripping up my application was a call to:
System.Windows.Forms.Cursor.Position.ToString()
Removing that line, and replacing it with the following MonoMac method allowed the application to remain hidden:
NSEvent.CurrentMouseLocation.ToString()
I was able to get this working by setting the value of "Application is agent (UIElement)" key to 1 in the info.plist file. Even though it should be a BOOL value, MonoDevelop makes it a string, but setting it to 1 seems to work. You can also set an empty string the for the "Icon file" but it's not necessary.

How do i set this window to topmost?

How do i set my window above all other? I need a bad but noticeable msg box that closes on its own.
Msg is a dummy form which is empty. All i want is its title.
The problems with the code is the window isnt created 0,0 (its just whereever windows feels like putting it). The width is correct but i notice if i click firefox or another app window my app doesnt pop up. I know it is being shown bc i can see it in the taskbar at the bottom for a brief second. So the bugs so far
Doesnt go topmost if i click another app
Isnt 0,0
How do i fix this?
{
var msg = new Msg();
msg.Text = (has ? "*" : "+") + args[0];
msg.TopMost = true;
msg.Width = 2000;
msg.Top = 0;
msg.Left = 0;
msg.Show();
System.Threading.Thread.Sleep(1000);
msg.Close();
}
Sounds like TopMost doesn't always do it; here's an answer to a similar question showing how to hook into Win32 for the call: Form top most?
Update: just read the rest of the answer; it might only fail running in Debug mode within Visual Studio (where your app is actually executed with vshost.exe, rather than running independently).

Categories