Activate a window from other process which has ShowInTaskbar = false - c#

I was using this single instance implementation:
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
However, It stopped working when I added a NotifyIcon and I set ShowInTaskbar = false when the window is minimized. In this state the windows doesn't get the broadcast event.
So I changed the code to find the window using EnumWindows and matching the window title, which is a flimsy solution. I tried SetProp, GetProp, but the window handled is recreated when the value in ShowInTaskbar changes. I'm in .net core so I can't use the visual basic class WindowsFormsApplicationBase.
Is there a better good simple solution for activating the first window? Hopefully without sockets, pipes and stuff like that?

Related

Debugging WPF window is in the way blocking the editor

Whenever I debug a WPF program or any windows program in Visual Studio at the initialization code, using step into feature, the wpf window always goes on top of the editor and I cant see whats behind it, this is of course during the process of initializing the main app which becomes unresponsive thus not able to move the idling window out of the way so I can see what I am debugging.
Is there a way around this? This might be a silly question if there is an obvious solution I apologize in advanced. Hopefully the question make sense.
Update: Here is the screen shot of what I'm dealing with.
As you can see in here "Ernanis Renamer" window blocks the view, and if I try to drag it and move it away, It wont let me as it is suspended and need to continue until all component is displayed.
**Update:** Here is a video of a simple annoyance that I keep getting every time I try to debug during initialization.
<div style="position:relative;height:0;padding-bottom:56.25%"><iframe src="https://www.youtube.com/embed/RmbMGqcMtiU?ecver=2" width="640" height="360" frameborder="0" style="position:absolute;width:100%;height:100%;left:0" allowfullscreen></iframe></div>
The "topmost" behaviour might be being added in a non-obvious manner (e.g. a window style, or some quirky codebehind), which is why you can't find it.
To definitively answer the question as to whether your "Window" is being made to be "Always On Top" / "TopMost", download Windows Detective.
http://windowdetective.sourceforge.net/
Install it, and run it with "Administration Rights".
"Pick"/"Find" your application window (make sure it is the outermost/main frame window) while it is running, and then choose "Edit Properties" (as it shows the flag clearer).
You then have various ways to "handle" the situation your errant window.
1) Use Window Detective to turn ON the Always on Top for Visual Studio - so it can come before your app window
2) Use Window Detective to turn OFF the Always on Top for your app window
3) Put some code into your app (after the window intialization but before where you are debugging/tracing) so it turns OFF topmost IF you are attached/running it from a debugger e.g.
if (System.Diagnostics.Debugger.IsAttached)
{
this.Topmost = false;
}
You then won't forget to "keep" the "topmost" behaviour if that is "part" of your design.

getting correct window style as configured by windows shortcut

When you create a shortcut in windows to an application, in its properties you can define under "Run:" the initial window state (Minimized / Maximized / Normal).
C# applications do not, apparently, start up their forms according to this configuration out-of-the-box, so I figured I need to get this information somehow and write a code that does that programmatically on start up.
I cannot find a way to get this information in my c# application.
I have tried:
Process.GetCurrentProcess().StartInfo.WindowStyle
But for some reason this is always "Normal" regardless of the configuration in the file shortcut.
Does anyone have any idea how to come around this?
Just made a quick test and it does work out-of-the-box only if your main form is set to start as Normal window state. If you choose Minimized or Maximized it will override the shortcut settings.
In case your main form is set as Normal you can detect the actual state using such code:
FormWindowState actualState = Application.OpenForms["Form1"].WindowState;
if (actualState != FormWindowState.Normal)
{
//probably launched via shortcut overriding the state, handle.
}

How to start Firefox in hidden mode from C#?

how can I start Firefox as hidden application in my C# application?
The ProcessStartInfo.CreateNoWindow property only applies to console mode apps. A regular GUI app requires a window to be functional. It is supposed to observe the ProcessStartInfo.WindowStyle property but not all GUI apps are well behaved, particularly when you ask for ProcessWindowStyle.Hidden. Which presents a user interface problem, can't get the window activated when it is hidden.
A minimized window is usually the best you can get, with a taskbar button.
I think. You are asking how to run a program without displaying in Task manager. ?? if its true, then You Can Set ShowInTaskBar Property to false of that Form. if not then can u explain what "Hidden Mode" means ? so that it is easy to solve the problem
Form.ShowInTaskBar=false;
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar.aspx

Activate window of hidden process

If the user tries to start another instance of my application, I want to activate the window of the process which is already running.
To find the process, I call Process.GetProcessesByName(), which gives me the System.Diagnostics.Process instance of the running instance of my application. However, I have hidden my process from the taskbar using
Form.ShowInTaskbar = false
This causes the Process.MainWindowHandle to be zero, so I can not access the current window of the running process.
Is there another way I can activate the window of the already running process?
Yes, the ShowInTaskBar property is special. There are several other properties of the Form class that are in the same category. These properties are implemented by style flags specified in the native CreateWindowEx() winapi call. The WS_EX_APPWINDOW flag for ShowInTaskBar.
Which is a problem when you change these properties, the window has to be recreated. Winforms does this automatically for you but it has several side-effects. One of which is that the Handle property value changes. Making it impossible for the Process class to find the MainWindowHandle back.
You'll have to find the window back another way. Making EnumWindows work is definitely not easy for Winforms forms, you can't get a guessable window class name. Not changing the ShowInTaskBar property is certainly the better approach. Also consider using the WindowsFormsApplicationBase class, it makes this trivial with the OnStartupNextInstance method.

Form top most?

How can I display something over all other application. I want to to display something over all form of my program and all other programs open on my desktop (not mine).
*Top Most doesn't work I have tested and my browser can go OVER my application :S
Here is an image of when I use TopMost to TRUE. You can see my browser is over it...
http://www.freeimagehosting.net/uploads/5a98165605.png
You can use the form instance and set the property TopMost to True.
If you want to be over all Windows, there are another way with Win32 Api calls.
Here is what you could do:
In your form class add :
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
In the form load you can add :
SetForegroundWindow(this.Handle);
This should do the trick.
Update
TopMost should do the job BUT: Top most OR/AND the Win32 Api call will only work not inside Visual Studio (well for Vista and with VS2008 I tested it... I can't tell for other). Try running the program with the .Exe from the /bin directory, it will works.
The Form.TopMost property will set your form the top form above all other running windows applications (not just your forms.)
myForm.TopMost = true; // This will do the job
TopMost property is what you need (never had a problem with that)
On MSDN it says:
A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx
In my team we had a internal tool that keeps on running after Windows startup. It is a WinForm with TopMost set to ture. On Vista, sometimes we had the same problem. At very random times, the form will lost TopMost property and other non-topmost window can appear above it.
I had a log of researches but found no answer and many other people had this same problem, it seems that on Vista at very low level there is a bug about TopMost property.

Categories

Resources