SetForegroundWindow method sometimes does not hide start menu - c#

I have a Windows form with no border that is set to maximized in order to make it look "fullscreen". When I open my fullscreen form in my C# app I call the following code...
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
With the handle I get from the Windows forms api by calling...
ScreenScrape.SetForegroundWindow(Handle.ToInt32());
When the "Shown" event is raised.
The only problem is sometimes I get the expected result (a fullscreen app with nothing in front of it) and sometimes I get this...
If i click anywhere on my Windows form and its embedded browser I see this...
Another developer suggested programmatically clicking at a known location Say 10,10 but that seems like a hack. Any idea what might be happening here?

Related

Getting a console window out of "select mode" to prevent a crash

In my C# application, I am trying to prevent a crash in my application, basically, I am using a Console window as a log display window. I already solved the "Close Button" issue by disabling the close window, and I show/hide the menu with Show and Hide calls, all of that is working just fine.
My final hurtle is if Text Selection is active and the window attempting to be hidden.
I either need to:
A. Kick the window out of select mode. (not sure how I would do this, since selection pauses all output.)
B. Disable the "Edit" menu in the same way I disabled the Close menu, in a hope that it would also disable the mouse selection, but I have yet to find any way to remove the "Edit" menu, and I am not even sure that would prevent mouse selection.
C. This seems the most obvious, disable mouse selection, and this is the one I have currently in my code, but it's not working, so I am not sure what I am missing.
The Code in Question:
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
....
[DllImport("Kernel32.dll", ExactSpelling = true)]
private static extern int SetConsoleMode(IntPtr hWnd, int wFlag);
....
public static void HideConsoleLog () {
if (handle != IntPtr.Zero) {
SetConsoleMode(handle, 0x0080); // ENABLE_EXTENDED_FLAGS 0x0080
ShowWindow(handle, (int)WinCntrlOpt.SW_HIDE);
LogVisible = false;
}
}
According to the Documentation:
ENABLE_EXTENDED_FLAGS 0x0080 Required to enable or disable extended
flags. See ENABLE_INSERT_MODE and ENABLE_QUICK_EDIT_MODE.
And...
ENABLE_QUICK_EDIT_MODE 0x0040 This flag enables the user to use the
mouse to select and edit text.
To enable this mode, use ENABLE_QUICK_EDIT_MODE |
ENABLE_EXTENDED_FLAGS. To disable this mode, use ENABLE_EXTENDED_FLAGS
without this flag.
So, it seems I am doing this correctly, and it does run, but it does not prevent mouse selection. I do know that not to long ago, Microsoft tinkered with Consoles, and I don't know if that's broke this or not.
There is a similar question on stack exchange, but that nether gives a working solution, and is in-fact the reverse to what I am trying to do and in C or C++. I can interpolate, but it's basically what I am already trying here.
It seems to be the same suggestion, use ENABLE_EXTENDED_FLAGS without any other flags to disable the option, and that's what I have. But it doesn't work.
So, I am either scratching at the wrong solution or am not doing something else correctly.
The effect is, while the console window is open, you can use the mouse to select text which automatically puts the console in that "select mode" that Microsoft's tinkering added. That's not a problem, but it's when it's left IN that mode, and you select "Show Log" (a check-marked toggle menu option in the main application window) The console window does indeed hide, but then the main window locks up and the application crashes. (my guess is because the window is blocked by that select mode.) It doesn't crash when the 'select mode' is not active.
I would also like to try this from the same documentation:
ENABLE_MOUSE_INPUT 0x0010 If the mouse pointer is within the borders
of the console window and the window has the keyboard focus, mouse
events generated by mouse movement and button presses are placed in
the input buffer. These events are discarded by ReadFile or
ReadConsole, even when this mode is enabled.
Problem is, it says:
When a console is created, all input modes except ENABLE_WINDOW_INPUT
are enabled by default.
So, how do I disable it?
I am fine with any solution that either kicks it out of select mode, or prevents it to begin with, and I have here what I have tried.
When the "Show Log" menu option is selected, the window is going to minimize.
Before the window minimizes, you have to check whether the user has selected any input to prevent the crash. You can check whether the user is selecting anything by using GetConsoleSelectionInfo.
The CONSOLE_SELECTION_INFO out parameter should be equal to 0x00, and if it's not, you need to process the selection. As the documentation from GetConsoleMode/SetConsoleMode shows, you either need to call ReadFile or ReadConsole to discard the selection event that might be going on.
When you've implemented this, it shouldn't crash anymore.

C# - on mouseover at taskbar, a preview is generated. how can i achieve this by C# coding

I am developing a winForm application in which i am list all the running processes.
on mouseover of any process, i want preview window like (In Windows 7, when you mouse over an application in your task bar, a preview is generated showing the contents of the application window.)
See Image.
Any idea how to achieve this, i am not able to get it.
Any suggestion will be helpful.
Thanks..
full example at this site
You need two functions :
[DllImport("dwmapi.dll")]
static extern int DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb);
[DllImport("dwmapi.dll")]
static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props);
usage of DwmRegisterThumbnail
you are at your form, and process it a external program that you want to paint at your program.
IntPtr hWnd = process.MainWindowHandle;
int i = DwmRegisterThumbnail(this.Handle, hWnd, out thumb);
after registration, you just saying to windows the location to paint the picture
DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES();
props.fVisible = true;
props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY;
props.opacity = 255;
props.rcDestination = new Rect(panel.Left, panel.Top, panel.Right, panel.Bottom);
DwmUpdateThumbnailProperties(thumb, ref props);
You don't need to write any code to make this happen. It's part of the Aero theme, introduced with Windows Vista and still present in Windows 7. By default, a little thumbnail of the application's window is shown when you hover over its icon in the taskbar. That's true for the Skype example you showed, and also for a C# application that you write yourself.
If you want a finer level of control over exactly what gets shown in the preview (like if you only wanted to show a small portion of your app's window), you can write some code that calls one or more of the relevant DWM API functions.
But don't write all of the P/Invoke code yourself. Use one of the existing libraries that wraps it all up in a .NET friendly way, like Windows Forms Aero or the Windows API Code Pack.
If you want your own application to display an Aero thumbnail, maybe this C++ sample could help you. Apparently it uses DwmRegisterThumbnail() to say "This HWND will now display a thumbnail of this other one".
So, a little P/Invoke call and IWin32Window...

Finding the IntPtr of the TaskBar

Previously, I asked this question about returning the user to the previous window they were using, but I have found the solution, but it has created another problem. The function that determines the previous window will sometimes work (if a certain set of events happen that I cannot recreate at will, but happens randomly). At some point it goes 2 windows back, sometimes 1 window back. It does this because the taskbar is in the way first, and then there is the window.
So, in order to fix this, I have determined that I should keep going through the previous windows until I get to a window that is not the taskbar, however, I do not know how to determine the IntPtr of the taskbar.
I have tried:
IntPtr taskBarWnd = FindWindow("Shell_TrayWnd", null);
But it doesn't seem to work. If I call MessageBox.Show(taskBarWnd.ToInt64().ToString()), I get 131258, but when I do:
IntPtr thisWnd = GetForegroundWindow();
IntPtr lastWnd = GetWindow(thisWnd, 2);
It is referencing the taskbar, but when I call the same MessageBox function above, it returns 131260 (65774 if the icon is within the hidden icons area).
Does anyone know if there is anything else I can try to determine if the lastWnd I have calculated is the taskbar? I would prefer not to have something that checks if the IntPtr's are close to each other instead of being equal.
I can't check right now since this is my iPad, but I'm pretty sure the taskbar windows all use custom window classes so you should be able to call GetClassName and figure out from the result what your handle refers to.

Windows +D disabled but not working

I have two windows form MainForm and MinimizedMainForm. I want to disable Windows+D for both the forms. Parent of both forms is a third form FormBase. I have used following code for disabling the Windows+D
private const in GWL_HWNDPARENT =-8
IntPtr hprog = NativeMethods.FindWindowEx(NativeMethods.FindWindowEx(NativeMethods.FindWindow("Progman", "Program Manager"),
IntPtr.Zero, "SHELLDLL_DefView", ""),
IntPtr.Zero, "SysListView32", "FolderView");
NativeMethods.SetWindowLong(this.Handle, GWL_HWNDPARENT, hprog);
Problem I am facing is my MainForm is never getting minimized if I press Win+D.
and for first launch of MinimizedForm if I i press Win+D it doesnt get minimized.
But for second+ time Load of minimizedMainForm if I press Win+D , Form gets minimized. I have added above code on the FormLoad event of both the forms. MinimizedMainForm is opened when i click on panel on MainForm and vice versa. MainForm is the starting form of my project.
What should I do to stop the form from minimizing on 2nd+ loadForm events?
Maybe instead of preventing minimizing of window what can be minimized, you have to create window what can't be minimized instead: MinimizeBox = false ? (it can't be that simple I bet, but i can't write comments, so here comes a solution =D)
Basically Win + D is not for minimizing the windows but for showing desktop. Win+D = Show desktop. So even if your window does not have minimize box, it will get minimized.
Further, when you press Win+D, it is not your application which handles it but it is the windows itself which is handling it so it will never reach to your application at all so it makes no sense if you handle it in your application.
Try finding out some hack where your application is up, you stop windows from processing Win+D key.
There is a key in registry
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
changing values of hotkeys here will disable the Win+D combination but it is not recommended as you will have to restore it back when you application leaves the focus.
And yes, if you are making a widget, follow the requirements of creating a widget. It will automatically provide you such functionality

Setting my C# form as a child to another application

I'm currently developing a form application which works as an overlay to another program (Skype). Right now, I'm using TopMost = true, but that's a pretty bad solution.
I have a handle to the Skype window, as well as a handle to my own window. How do I make my program fulfill the following three statements:
1. It has to disappear if Skype is minimized
2. It has to appear above Skype
3. It has to appear behind any other application which is above Skype
Above and behind relates to z-order.
I'm currently using the SetWindowLong function, but I cant get the desired results.
[DllImport("user32.dll")]
public static extern int SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong);
SetWindowLong(
new HandleRef(child, child.Handle),
-8, // GWL_HWNDPARENT
new HandleRef(owner, owner.Handle));
For #1, my application continually checks if the dimensions of Skype has changed, so I could simply also check if the window is no longer visible. However, I'm completely at loss with #2 and #3.
Thanks in advance.
Kloar

Categories

Resources