Results 1 to 12 of 12

Thread: C#.NET detect if user is on the desktop?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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!
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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.
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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.
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    16

    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:
    1. [DllImport("user32.dll")]
    2. private static extern IntPtr GetForegroundWindow();
    3.  
    4. bool IsOnDesktop()
    5. {
    6.      // Get all the processes.
    7.      Process[] proc = Process.GetProcesses();
    8.  
    9.      // Get current window handle.
    10.      IntPtr cWin = GetForegroundWindow();
    11.  
    12.      foreach (Process x in proc)
    13.      {
    14.           if (x.MainWindowHandle == cWin)
    15.           {
    16.               return false;    
    17.           }
    18.      }
    19.      
    20.      return true;
    21. }
    Last edited by farooqaaa; Aug 26th, 2011 at 07:57 PM.
    Farooq Azam - C# Tutorials, tips & tricks and more...

    Images & Videos

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: C#.NET detect if user is on the desktop?

    Quote Originally Posted by farooqaaa View Post
    I am sleepy and came up with this. Its working fine but pretty expensive.

    vb Code:
    1. [DllImport("user32.dll")]
    2. private static extern IntPtr GetForegroundWindow();
    3.  
    4. bool IsOnDesktop()
    5. {
    6.      // Get all the processes.
    7.      Process[] proc = Process.GetProcesses();
    8.  
    9.      // Get current window handle.
    10.      IntPtr cWin = GetForegroundWindow();
    11.  
    12.      foreach (Process x in proc)
    13.      {
    14.           if (x.MainWindowHandle == cWin)
    15.           {
    16.               return false;    
    17.           }
    18.      }
    19.      
    20.      return true;
    21. }
    It hightlights : IntPtr cWin = GetForegroundWindow(); and says some error about converting from IntPtr to int.
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  8. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    16

    Re: C#.NET detect if user is on the desktop?

    Have you declared this API call?

    vb Code:
    1. [DllImport("user32.dll")]
    2. private static extern IntPtr GetForegroundWindow();

    I don't know what's causing that.
    Farooq Azam - C# Tutorials, tips & tricks and more...

    Images & Videos

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: C#.NET detect if user is on the desktop?

    Quote Originally Posted by farooqaaa View Post
    Have you declared this API call?

    vb Code:
    1. [DllImport("user32.dll")]
    2. 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!!!!
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  10. #10
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  12. #12
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: C#.NET detect if user is on the desktop?

    Quote Originally Posted by Gamemaster1494 View Post
    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
  •  



Click Here to Expand Forum to Full Width