Bringing focused window to the front - c#

I have a wpf/.net 4.6.2 application. What I need to do is to open browser (IE) in full screen mode and after that, in 5 seconds for ex, bring my application window to the top and make it focused, so that when I click on the browser (which is apparently behind) the application would appear covered
window.Topmost = true;
window.Topmost = false;
window.Focus();
window.Activate();
On Windows 7 it works perfectly, but on Windows 10 it appears as topmost but non-focused, and as a consequence, when I click on the browser, the application stays on top, and I have to click the application first (to give it focus) and then the browser, and only after that, browser covers the application

Unfortunately I am unable to comment yet due to my low rep; however, I would like to suggest that you swap the focus and activate calls around.
window.Topmost = true;
window.Topmost = false;
window.Activate();
window.Focus();
Also, why are you setting the window topmost twice? This seems to be rather counter-intuitive as the final result would always be false.

Related

BrowserWindow can not take over keyboard focus when hosted in wpf HwndSource

my wpf application hosts an electron window by HwndSource, all has been working for several years on electron 10.1.6, now following the docs break changes and try to upgrade to 20.3.0. changes only:
in main, require('#electron/remote/main').initialize();
in preload, const remote = require('#electron/remote');
when create BrowserWindow set sandbox = false; contextIsolation = false; nodeIntegration = true; and require('#electron/remote/main').enable(win.webContents);
then mouse click between wpf part and this electron window.
within 10.1.6, keyboard focus will jump between.
within 20.3.2, i can click any buttons in electron area, but can not input anything for its textbox, by snoop, i can see the keyboard focus always on wpf parent window. seems electron can not take keyboard focus from wpf window.
after BrowserWindow show, i have tried wnd.focus(); wnd.focusOnWebView(); wnd.webContents.focus(); even call blur() first then focus(), or an addon extension that call win32 api SetFocus(HWND), but no lucky.
any suggestion here? it blocks me now, also cannot google any helpful solution.
already workaround, no matter wpf or electron, they are win32 window, we can get its handle and monitor message, when WM_PARENTNOTIFY is coming and once if WM_LBUTTONDOWN detected then SetFocus.
case WM_PARENTNOTIFY:
if (LOWORD(wParam) == WM_LBUTTONDOWN)
SetFocus(hwnd);
break;

WPF Window under all other windows after making window visible from being collapsed

I am having some very frustrating issues with my WPF window. The design I was going for is:
When the user minimizes the window it will minimize to a system tray icon (hide the window and icon on start bar). When the user right clicks on the icon, a context menu will show up with options and one of them will say open, which will open the app (show window again / unhide window agian). The user could also double click the icon as well.
Pretty simple right?
I have no issues minimzing to the task bar, I simply do the following on the closing event of the window:
e.Cancel = true;
this.Visibility = Visibility.Collapsed;
However, I am having issues properly restoring the window. I simply do this on the context menu click or icon double click event:
this.Visibility = Visibility.Visible;
this.Activate();
The issue is that the window is once again on the start bar with its icon but it is behind every single window the user has open. I want it so when the user goes to open the window it will be the top most window. I do not always want it to be the topmost, just only when they want to make it visible again.
I have tried many things like setting the show activate on the window to true, waiting a second after making it visible to then activate the window, activating the window multiple times (worked a few times but was maybe 1 out of 10 tries), etc..
I don't think showing / hiding a window should be this annoying and I am not really sure what I am doing wrong.
Any help is appreciated, thank you.
Only after posting this did I realize, the application minimizes first before hiding. When I show the window, it was showing as minimized.
After knowing this issue I was able to fix the issue. This may help others who do decide to hide the window after minimizing.
EDIT
Here is the code I used to hide the window (this is called after the event fires for state changed [minimized]):
Application.Current.MainWindow.Visibility = Visibility.Collapsed;
Application.Current.MainWindow.WindowState = WindowState.Normal;
You will notice I set the window state back to normal after I hide it. Even though the window is hidden and not being rendered it will in memory restore the window location / size.
Then when I want to see the window again I just do:
Application.Current.MainWindow.Visibility = Visibility.Visible;
Which will show the window just fine!
Hopes this helps someone out there!

Avoid Focus on Mainwindow when showing it

I'm currently working on a popup program in wpf that gets hidden either by this.hide(); or MainWindow1.WindowState = WindowState.Hidden and "pops up" using MainWindow1.WindowState = WindowState.Normal;
My problem is, that I don't want the Mainwindow to gain any keyboard focus.
So whatever program has the keyboard focus, when the popup gets shown, it should keep it.
I just want it that way, so people don't type entire emails in the not handeling Popup, just because they didn't notice it popping up.
Edit:
I'm not trying to keep focus in any my own program windows but in other windows programs (e.g. Outlook)
Edit2:
Here is a screenshot from my Program, just to clear obscurities about the usage of the PopUp class.
Edit3: Maybe it's possible to set the keyboard focus back to the programm that was focused before?
so people dont type entire emails in
I would recommend that you set the main page controls, or their container, Enabled property to false when the popup is running and reverse when it is not. Hence focusing the user to the popup until its acknowledged.
If you purpose is to show a notification form, check that component: Windows Forms Toolkit NotificationForm
The solution is (as it seems) too easy, all you need to do is:
MainWindow1.Showactivated = false;
this.Show();
This will show the MainWindow while the focus is kept on whatever program had the focus before the MainWindow appeared.

How to Trigger autohide on the taskbar on WindowsCE

im writing a C# program for a Windowsce 5.0 Device (PSION Teklogix Workabout Pro G2).
The taskbar is set to autohide.
I can't disable it completely, because the user sometimes needs to access the start menu or may like to manually show or hide the SIP. And it should not be displayed all the time, because i'd like to use as much of the small display as possible.
My problem is: When the taskbar is minimized at the bottom of the screen and a user clicks somewhere on it (not the startmenu button), it will slide in and is shown correctly.
But if the user does not activate the startmenu (by clicking on the windows-Logo), the taskbar won't slide out again, unless the startmenu was opened once.
Is there something like an event or so, that i could send to the taskbar, so it hides again, without the user beginning to access the startmenu ?
The way I have done this in the past assuming you mean vanilla ce (standard shell) is to grab the handle of HHTaskBar and simply hide it ;)
I also disable the SipWndClass (just in case the keyboard is left open).
Where iEnabled = true (enter fullscreen), or false to show HHTaskBar: -
HWND hWndToHide = FindWindow(_T("HHTaskBar"), NULL);
if(hWndToHide) {
if(iEnabled) {
// Disable VanillaCE TaskBar
if(IsWindowVisible(hWndToHide))
ShowWindow(hWndToHide, SW_HIDE);
// Disable SIPWnd (On Screen Keyboard).
hWndToHide = FindWindow(_T("SipWndClass"), NULL);
if(hWndToHide && IsWindowVisible(hWndToHide))
ShowWindow(hWndToHide, SW_HIDE);
}
else {
// Enable VanillaCE TaskBar
if(!IsWindowVisible(hWndToHide))
ShowWindow(hWndToHide, SW_SHOW);
}
}
It shouldn't be too hard to translate this to .NET ;)

Hide the taskbar using C#

I am running Windows XP 64 bit. I want to hide the taskbar when I run my application.
I tried codes by searching the web. In all those, it hides the task bar. But the problem is, when i open a notepad and maximize it, it is not actually into full screen. Because the space where task bar was there is still blocked with empty space. I want it fit really into full screen mode.
If you like to replace the windows shell (taskbar) you'll have to change a registry key.
Changing the default shell (all users):
open regedit (start menu > run, and type in regedit)
go to: HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon.
Change Shell from explorer.exe to your program path and name e.g. c:\myKioskApp\Kiosk.exe
Changing the default shell (only current user):
open regedit (start menu > run, and type in regedit).
go to: HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon.
add a new string value (Edit > New > String Value) called shell. and set the value to the path of the new shell e.g. c:\myKioskApp\Kiosk.exe
log out and log back in.
I've done this by making the application borderless, maximized, and setting it to be Topmost. Here's a perfect example from CodeProject.
As one of the commenters has said, replacing disabling Explorer and running your application might be the best way, security-wise.
You Can Hide your task bar by setting Following Properties of your C# Form.
WindowState : Maximized
FormBorderStyle : FixedDialog
on window 7 (or maybe higher) using FormWindowState.Maximized is wrong because the maximum size will be subtracted by Taskbar height but you can do this
this.WindowState = FormWindowState.Normal; // or default
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
// do it here
this.Location = new Point(0,0);
var fullscreenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.Size = fullscreenSize;

Categories

Resources