• 0

[C#] Detecting full screen


Question

Im working on a cool little app.

It sits in the background hidden, and when you middle-click your mouse, it pops up allowing quick access to apps/files/folders.

The problem is, that it still works when in fullscreen apps, like games and movies, so i want to stop this.

So far, i have:

//if right button has been click, show the menu
			if (e.Button.Equals(MouseButtons.Middle))
			{
				//rect for the window size
				System.Drawing.Rectangle rect;

				//get the dimensions of the active window
				GetWindowRect(GetForegroundWindow(), out rect);

				//if window is fullscreen, dont show popup
				if (rect.Height == Screen.PrimaryScreen.Bounds.Height && rect.Width == Screen.PrimaryScreen.Bounds.Width)
				{

					//make sure it isnt the desktop window
					if (!GetForegroundWindow().Equals(FindWindow("progman", null)))
					{

						Console.WriteLine("cant show");
						return;
					}
				}

				this.Top = e.Y - (this.Height / 2);
				this.Left = e.X - (this.Width / 2);
				this.Show();
			}

The above code works fine, except when the taskbar is the foreground window, it wont let the menu popup. The little "progman" bit is to check if the desktop is the foreground window, and if so, allow the popup. (desktop is considered fullscreen by that code, but i want to allow the popup)

Any help is appreciated.

post-129601-1151489204.jpg

Link to comment
https://www.neowin.net/forum/topic/473950-c-detecting-full-screen/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You should probably switch to something like:

Screen.FromHandle(hWnd).Bounds.Width

so that you take int account multiple monitors....

also, you can use

[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();

if (GetDesktopWindow() != hWnd) {
//true
}else {
//false
}

to get the desktop instead of findwindow....

As far as I can tell, the class name 'WorkerW' is what is throwing off you code. Just throw in a check to see if it is your app. FindWindow("WorkerW", null)

Oh, and my googling informs me that it can also be 'WorkerA' on older versions of windows....

Edited by MioTheGreat
  • 0

is WorkerW is the class name of the taskbar?

I tried printing the window title of the taskbar, but it printed a blank null string.

I think ill use a GetClassName to see what the class name of the foreground window is, and if its the desktop or taskbar or anything like that, ill allow the popup.

  • 0
  MioTheGreat said:

also, you can use

[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();

if (GetDesktopWindow() != hWnd) {
//true
}else {
//false
}

to get the desktop instead of findwindow....

Not true. this isn't the window of the "desktop" (the desktop with all the icons), but this is the desktop-window as in the main window.

Often made mistake!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.