top most window in c# - c#

I have a form application, i want to it to be the the top most. i use
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
this function, it works fine.But when task manager property always on top is checked it fails. And task manager will appear as the top most window.
So my concern is, if there any way to achieve it, or we cant not do it with task manager. it will always appear on the top, or anything else i was missing, or doing wrong.

The MSDN say that HWND_TOPMOST simply Places the window above all non-topmost windows. In other words there are two groups of windows: non-topmost (usual) and topmost and you just sent your window to the other group.
If there is any other topmost window (= task manager in your case), you can switch between them as you normally would between non-topmost windows and they will be overlapping depending on which one is currently active.
If you would like to force your window to be always topmost, I guess you would have to watch for the window deactivation (WM_ACTIVATE message) and then move your window up in the Z-order and also focus your window back - this way you would prevent problems like having your window the only one visible, but having the keyboard focus on another window.

you can try this if you just want your application is always at the top of other application.
private void timer1_Tick(object sender, EventArgs e)
{
this.TopMost = true;
}

Related

Window.DragMove() causes the window to freeze temporarily if holding down the mouse left button without moving the cursor

I have a "borderless" window in WPF. It can be dragged from any part of the window that does not handle the Click event, using this code:
// Drag the window:
private void GlassWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left) return;
if (AllowDrag) DragMove();
}
(Note: AllowDrag is always set to true in this case)
This code works fine, except that when I click on the window and hold down the left mouse button, without moving the cursor (= not dragging the window), the window freezes for about 2-3 seconds (i.e. all animations pause, progressbar stops moving). This behaviour is consistent, and does not happen when I click on a button or when I drag the window, only when I hold left click.
Is there any solution for this or is this intended windows behavior?
EDIT: Things that don't solve the problem:
https://stackoverflow.com/a/3275712/2719183
https://stackoverflow.com/a/5494769/2719183
http://www.codeproject.com/Articles/11114
if (AllowDrag) DragMove();
DragMove() is the trouble-maker, it is uses a pretty hacky way to implement the move. And that causes the problem you describe, the WPF team is well-aware of the issue but chose to not fix it. You can read about it in this connect article. Vote if you are not pleased.
So you need to avoid DragMove(). The best way is to do it the way it is normally done, you minimize the risk of reproducing the exact same trouble that way. That requires knowing a little about the way the winapi works. Whenever a window is clicked, Windows sends the WM_NCHITTEST message to ask your app what part of the window was clicked. When you return HTCAPTION, even if you don't have a caption, then Windows takes your word for it and implements what normally happens when you click and drag a window by its caption.
That has been done, you don't have to be an expert in the winapi to get that going. Google "wpf wm_nchittest" to find code. The top hit is an existing SO question, Tergiver's code looks good.

In C#, WPF, could I hide my cursors in all windows?

everyone.
When I develop an application using C#, WPF. I want to hide my cursor in some special area. No matter which application/window on top, the cursor is always hiding.
In WPF Window, normally, we use
this.Cursor = System.Windows.Input.Cursors.None;
But this one could not solve my problem.
I want this situation:
I open my window, the cursor disappears in my window. And then I open a notepad on top of my window, the cursor disappears in the notepad too.
This one is able to do or not? Thank you.
You also can use win32 dll.
internal static class WinCursors
{
[DllImport("user32.dll")]
private static extern int ShowCursor(bool bShow);
internal static void ShowCursor()
{
while (ShowCursor(true) < 0)
{
}
}
internal static void HideCursor()
{
while (ShowCursor(false) >= 0) //使用这个方法可以关闭光标
{
}
}
}
If it area is the same on each Form, you could create a custom BaseForm (or similar) which derives from Form, and put a Label on there and use its MouseEnter and MouseLeave events to hide the cursor.
All your forms can then derive from BaseForm rather than Form so that they all have this area.
Simply inside your mouseEnter event use a cursor hide
private void splitContainer1_Panel2_MouseEnter(object sender, EventArgs e)
{
Cursor.Hide();
}
Have you tried Window Class's event
Activted, FocusableChanged or other relative events
The window on top is the one that controls the mouse cursor. So the only way to do this is to put your window on top, likely by making it Topmost.
If you want to still be able to see the windows underneath it, you can make your window semitransparent. Set the AllowsTransparency property to true and then set your window's background color to a semitransparent color. (You can't make it 100% transparent, because WPF treats full transparency as meaning "there's nothing here, so send mouse events to the next window behind me"; but you can do something like #01000000 that's so close to transparent that nobody will notice it.)

Get window handle underneath mouse pointer, while ignoring a semi-transparent window

I've created a semi-transparent form (60% opacity with black background color) that my app launches, maximized, over the entire screen. Basically, it casts a gray color on the entire desktop.
When the user mouses-over a window on the desktop, I want to get that window's handle (hWnd).
The easy way to do this, which is working for me, is:
Temporarily hide my form (OR, temporarily set my form's opacity to 0.0)
Call [GetCursorPos][1]
Call [WindowFromPoint][2]
Show my form again
The problem with this approach is that my form / the screen blinks, which I don't like.
I've tried to fix this in two ways:
I figure there should be a way to get the hWnd of the window directly underneath my form by calling ChildWindowFromPointEx (passing-in the hWnd of the desktop and CWP_SKIPTRANSPARENT), but it doesn't seem to work. I also played with [ChildWindowFromPoint][4] and [RealChildWindowFromPoint][5] with no success. (P.S. Raymond Chen discusses the differences between these calls, here and it seems to me that ChildWindowFromPointEx is designed to do exactly what I need)
I tried preventing the entire desktop from refreshing (kind of "freezing" the screen momentarily) by using (1) SendMessage(GetDesktopWindow(), WM_SETREDRAW, false, 0) before I hide my form and (2) SendMessage(GetDesktopWindow(), WM_SETREDRAW, true, 0) after I hide my form. This didn't work quite right: some areas of the screen would freeze, some weird black blocks would appear, etc. I do know, however, that (1) does work, because one time I called (1) and didn't call (2) and my desktop appeared completely frozen (had to reboot, even TaskMgr didn't render correctly). I also tried using SuspendLayout and ResumeLayout on my form, but I don't think they are meant to handle my case.
Any help would be greatly appreciated.
You can do the checking yourself since your need to customise beyond that what the standard functions offer.
Call EnumWindows() to get a list of top-level windows.
Remove your semi-transparent window from this list.
For each window in the list use PtInRegion() to determine whether or not the mouse is over the window. Remove any windows that don't fit the bill.
Use GetNextWindow(), starting from one of the remaining windows to walk the z-order and find out which of the candidates is at the top.

C# Fullscreen, hiding the taskbar

I have recently written an application for my daughter, which is a kid-free zone where she has all unnecessary key presses ignored (windows key, Esc etc) but the problem I am having is that when I use the following code:
targetForm.WindowState = FormWindowState.Maximized;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
I am able to HIDE the taskbar, but it is not truly overlayed. When I move the mouse to where the taskbar would be, and click, it pops up, also, using this code and running external applications withing my windows form, I am left with this windows form keeping itself on top.
If anyone could help me with a proper way to display my windows form as a true fullscreen application, and be able to run external applications from within the form and have them prioritize themselves on top, that would be greatly appreciated.
In case you missed it, I am using VS2010, C# and winforms.
Thanks in advance!
The proper way to make a full-screen app is to just put something like Bounds = Screen.PrimaryScreen.Bounds; in your main form. Then when your app has focus it will cover the task bar.
You also probably want FormBorderStyle = FormBorderStyle.None;
The order of the performed actions is incorrect.
You should first hide the border (FormBorderStyle=None), and then set the window state to maximized. You even don't have to set TopMost to true.

How can I prevent other apps from stealing the focus?

I have a fullscreen window with the Topmost property set to true. Whenever an application changes its WindowState property, my window is automatically minimized even though it has the active focus. For example, the code below exemplifies the problem. 3 seconds after the window is deactivated, it changes from Minimized to Normal, minimizing the other fullscreen application.
// Topmost = false
private void Form1_Deactivate(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
this.WindowState = FormWindowState.Normal;
}
Is there a way to preserve the fullscreen window's WindowState property in such a case? I want the user to choose to minimize the fullscreen app, so I want to stop other programs from stealing the focus.
You may want to investigate "Kiosk Mode".
If this is a kiosk application and you are the only program running on the computer then fine. Otherwise this is a really bad idea and that's why there is not a documented way of doing it.
As Raymund Chen says this kind of question should be followed with the thought experiment "what if two programs did this?":
http://blogs.msdn.com/oldnewthing/archive/2005/06/07/426294.aspx
Perhaps this is what you want: Windows Form in Full Screen "Kiosk Mode".
Otherwise I'll second Henk's recomendation.

Categories