|
-
Aug 21st, 2011, 11:36 PM
#1
Thread Starter
Fanatic Member
C#.NET detect if user is on the desktop?
Hello. I was wondering if it was possible to see if the user was viewing the desktop(not on any other screen). If so, how? Thank you!
-
Aug 22nd, 2011, 12:05 AM
#2
Re: C#.NET detect if user is on the desktop?
That doesn't really make a lot of sense. Perhaps you could provide a more complete description of what you want to achieve and why. Are you saying that you want to detect whether a logged-in user doesn't have any windows open?
-
Aug 22nd, 2011, 02:54 PM
#3
Thread Starter
Fanatic Member
Re: C#.NET detect if user is on the desktop?
I'm making a hot key program. I want it to trigger tho only when the user is on the main desktop. Like no other windows in focus, just looking at the desktop and the icon(s) they have. (i was also thinking of doing an option for this, if yes check if no don't). I don't want the user to do Control + A on a notepad document and open up a program when all they wanted to do was select all.
-
Aug 22nd, 2011, 10:24 PM
#4
Re: C#.NET detect if user is on the desktop?
You should be able to use the GetForegroundWindow API to get the handle of the window with focus. If it's the desktop then that handle should be zero I think. You can certainly test that theory.
-
Aug 24th, 2011, 02:51 PM
#5
Thread Starter
Fanatic Member
Re: C#.NET detect if user is on the desktop?
It does not work. I tried it and 0 did not work... It turns out the desktop was 131232 or something, and when i was just viewing the desktop it wouldn't trigger... Anyone else? please help... I need this for a project in tech class.
-
Aug 26th, 2011, 07:54 PM
#6
Junior Member
Re: C#.NET detect if user is on the desktop?
I am sleepy and came up with this. Its working fine but pretty expensive.
vb Code:
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); bool IsOnDesktop() { // Get all the processes. Process[] proc = Process.GetProcesses(); // Get current window handle. IntPtr cWin = GetForegroundWindow(); foreach (Process x in proc) { if (x.MainWindowHandle == cWin) { return false; } } return true; }
Last edited by farooqaaa; Aug 26th, 2011 at 07:57 PM.
-
Aug 29th, 2011, 02:52 PM
#7
Thread Starter
Fanatic Member
Re: C#.NET detect if user is on the desktop?
 Originally Posted by farooqaaa
I am sleepy and came up with this. Its working fine but pretty expensive.
vb Code:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
bool IsOnDesktop()
{
// Get all the processes.
Process[] proc = Process.GetProcesses();
// Get current window handle.
IntPtr cWin = GetForegroundWindow();
foreach (Process x in proc)
{
if (x.MainWindowHandle == cWin)
{
return false;
}
}
return true;
}
It hightlights : IntPtr cWin = GetForegroundWindow(); and says some error about converting from IntPtr to int.
-
Aug 29th, 2011, 05:55 PM
#8
Junior Member
Re: C#.NET detect if user is on the desktop?
Have you declared this API call?
vb Code:
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();
I don't know what's causing that.
-
Aug 29th, 2011, 06:29 PM
#9
Thread Starter
Fanatic Member
Re: C#.NET detect if user is on the desktop?
 Originally Posted by farooqaaa
Have you declared this API call?
vb Code:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
I don't know what's causing that.
Yes i did. oh wait.. i may not of done IntPtr before GetForegroundWindow();... I'll have to check it tomarrow =D. Hopefully it works!!!!
-
Aug 31st, 2011, 03:34 AM
#10
PowerPoster
Re: C#.NET detect if user is on the desktop?
it would be good if you could post your code that you currently have in its entirety. we cannot second guess what you have coded
-
Aug 31st, 2011, 02:32 PM
#11
Thread Starter
Fanatic Member
Re: C#.NET detect if user is on the desktop?
Alright. I will tomarrow afternoon, since the code is at school atm.
OFF TOPIC: question, am I allowed to have 2 threads up at once?
-
Aug 31st, 2011, 05:41 PM
#12
Frenzied Member
Re: C#.NET detect if user is on the desktop?
 Originally Posted by Gamemaster1494
OFF TOPIC: question, am I allowed to have 2 threads up at once?
Two forum threads up at once? Yes you may. As long as they are about different questions. Each question goes in a new thread. It helps organize the different threads on the forum so people can search for answers better. If the topics are extremely similiar then they can be put into the same thread. Sometimes someone gets an answer in a thread that leads into another question.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|