Thread: Desktop handle

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Desktop handle

    Does anyone know how to get the handle to the desktop (the actual desktop with the trashcan and everything on it, not the one returned by GetDesktopWindow())? I want to put a WS_CHILD window on it. When using the value returned by GetDesktopWindow(), it simply puts the child window on top of everything...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The GetDesktopWindow function returns the handle of the Windows desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which all icons and other windows are painted.
    Sounds like it's the function you seek.

    I've never messed with messing up the desktop, but have you considered the fact that when creating windows it's appended LAST in the window list and is thus rendered last, on top of other child windows?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    There is no other 'desktop' window handle - the desktop window is the ultimate parent window of all other windows.

    edit: beaten by Magos.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Perhaps you can use SetWindowPos or something to change the Z-order?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Actually I think there is another handle, I found it using WindowFromPos() with my cursor's position when moving my mouse on the desktop. When I use this value (0x120088) in CreateWindow() as the parent parameter, I get the result I want (a child window on the desktop). But I don't know wether this value (0x120088) is constant, or wether it's different per pc/session, therefor I'm looking for a function that returns the (correct) handle.

    Btw the handle GetDesktopWindow() returns is 0x10014, so it's different from the desktop handle I'm looking for

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Btw the handle GetDesktopWindow() returns is 0x10014, so it's different from the desktop handle I'm looking for<<

    The handle returned by GetDesktopWindow is the handle of 'the' actual desktop - any other handle you have obtained will be a child window of 'the' desktop.

    Any application you create with a main window has the desktop as its parent ie you normally specify NULL as the parent handle in the call to CreateWindow/CreateWindowEx (unless you deliberately specify a different parent).

    For a control you could, for example
    Code:
    #include <windows.h>
    #include <tchar.h>
    
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
    {
    HWND h=CreateWindowEx(0,_T("button"),_T("child of desktop"),WS_VISIBLE|WS_CHILD,
                          100,100,200,200,
                          GetDesktopWindow(),0,GetModuleHandle(0),0);
    
    MSG msg;
    while (GetMessage(&msg,0,0,0)>0)
      {
      if (msg.message==WM_LBUTTONDOWN)
        {
        DestroyWindow(h);
        break;
        }
      DispatchMessage(&msg);
      TranslateMessage(&msg);
      }
    return 0;
    }
    which makes that (button) control an explicit child of the desktop; the same should apply to user-defined window classes with the WS_CHILD style.

    If all you are concerned about is the z-order, then use SetWindowPos, as Magos has already suggested.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    isnt the desktop hwnd HWND_DESKTOP?
    Dev-C++ 4.9.9.2
    Code::Blocks
    Win XP

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    HWND_DESKTOP is #defined as (HWND)0 so for those api functions that, in general, recognise NULL as 'the desktop handle', it's fine to use (MapWindowPoints is such a function).

    However, if you want to get the 'actual' handle then the return value of GetDesktopWindow will give you that.

    For example, try the code I posted previously with HWND_DESKTOP in place of GetDesktopWindow as the parent handle and you will see the window will not be created (you might want to add a conditional statement to test the return value from CreateWindowEx so you can exit cleanly).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. How to get the correct handle to the desktop?
    By deadlocklegend in forum Windows Programming
    Replies: 1
    Last Post: 02-14-2002, 10:39 AM