Hiding the console - extended

This is a discussion on Hiding the console - extended within the C++ Programming forums, part of the General Programming Boards category; This thread is already on the C++ board, however it doesn't explain what's happening. I'd like to know what a ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Hiding the console - extended

    This thread is already on the C++ board, however it doesn't explain what's happening. I'd like to know what a few things mean :

    Code:
    #include <windows.h>
    
    int main() 
    { 
            char wintitle[] = "Disappearing CONSOLES..."; 
    
    
            ::SetConsoleTitle( wintitle ); 
    
    
            Sleep( 2000 ); 
    
    
            HWND hWnd = NULL; 
            while (NULL == hWnd) 
            { 
                    hWnd = ::FindWindow( NULL, wintitle ); 
            } 
    
    
            ShowWindow( hWnd, SW_HIDE ); 
            Sleep( 2000); 
            ShowWindow( hWnd, SW_SHOW ); 
            Sleep( 2000 ); 
    
    
            return 0; 
    }
    So, firstly, what's the :: operator that sets the console title ?
    Secondly, what's HWND hWnd, and why is it being set to NULL ?
    Thirdly, again, the :: operator, but after an equals sign ? Why ? What are the two variables being used in the function FindWindow ?
    Fourthly, the function ShowWindow, what are the two variables that are fed in ?

    Thanks


    EDIT : A handle is something that can be used to manipulate programs, correct ? Please define a handle "the correct way", :P before I start pulling my hair out Also, how are they used ?
    Last edited by Korhedron; 07-23-2003 at 01:17 PM.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The :: operator is used for calling functions or using variables that are in a different scope.

    hwnd is a variable of type HWND which is a handle to a window. It's being set to NULL because it's a pointer and you should always initialize pointers to NULL.

    A handle is a pointer to a pointer to a window, for example. Just remember that it's a pointer to a pointer to something. It allows the Windows memory manager to move things around in memory without your program knowing about it (if you had a pointer to the actual object and it was moved in memory, your pointer would be invalid. But a pointer to a pointer which always contains the correct address (controlled by Windows) will be okay). That's probably a bit confusing for you, but it's not that important to know the exact details of why it's like that when you're first starting.

    Read the code. First, the title of the console window is set to the value of wintitle (Disappearing CONSOLES...) with the SetConsoleTitle function. Then hWnd is declared and initialized to NULL. Then, the FindWindow function is passed a NULL pointer and the title of the window it is supposed to find (wintitle). It returns a handle to that window, which is stored in hWnd. ShowWindow is then called with the pointer to the window and the flag SW_HIDE, hiding the window. The system sleeps. Then ShowWindow is called again with the handle to the window and the flag SW_SHOW. The system sleeps again, and the program ends.
    Away.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    1. You don't need the :: in that case, it means use the current namespace basically. It is the scope resolution operator. Standard C++.

    2. HWND is a handle to a window. It is being set to NULL upon declaration because it is good practice so to do.

    3. The loop executes until it finds a top level window whos title matches that you have given. To see what the arguments are for FindWindow() look in your compilers help or at MSDN.

    4. To see what arguments ShowWindow() takes, look in your compilers help or at MSDN.

    A handle is a value used internally by Windows to identify a memory structure. Many Win32 API routines require a handle to some resource in order to function. Handles are an abstraction - they save you the hassle of worrying about the details of the memory structure necessary to support the object the handle referes too.

    *** EDIT ***

    Overlap...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot, I understand the code well now. However, if I extend this, I understand that if I used FindWindow, I could give it, to find the window I want, a pointer or a window name, or both. Therefore, if I used FindWindow using a different name than my console window, I could eventually hide other windows than the console, correct ? Say something like this :

    Code:
            HWND hWnd = NULL; 
            while (NULL == hWnd) 
            { 
                    hWnd = ::FindWindow(NULL, "Programmers Heaven"); 
            } 
    
    ShowWindow(hWnd, SW_HIDE);
    and it would hide my IE window with Programmer's Heaven open, correct ?

    I'd like to know, lastly, why the coder didn't say :
    Code:
    HWND hWnd = ::FindWindow(NULL, wintitle);
    and not set it to NULL, then use a WHILE loop, which evidently will only loop once, seeing as it sets its statement false from what the loop needs, inside the loop...

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Sorry, overlap. Adrian, you say the FindWindow function is inside a loop so that it can find it, because using it simply once would search the first window and then return a false because it may not be the first window it searches ( according to the name you gave ), correct ?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    66
    You don't need the :: in that case, it means use the current namespace basically. It is the scope resolution operator. Standard C++.
    This is not exactly correct. :: operator in that context means use the global function. In windows (and mostly MFC) there are many classes that implement class level functions to hide the global ones with class specific behavior.

    For example:

    Code:
    class Window {
      void paintWindow();
    }
    
    class Button : public Window {
      void paintWindow();
    }
    
    void paintWindow(Window *w = NULL)
    {
      if (!w) {
        // repaint the desktop
      }
      else {
        // window specific stuff
      }
    }
    Now if you decide to do:

    Code:
    class MyButton : public Button
    {
      void onRepaintDesktop() {
        ::paintWindow();  // repaint desktop
    
         // calling paintWindow() here would call Button::paintWindow() instead of the desktop version
      }
    }
    MFC has a lot of layers with function names reused often, it is clearer to specify :: to denote the global namespace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hiding The Console...
    By David Somekh in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2007, 01:32 PM
  2. Extended character set and the console
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2006, 03:13 PM
  3. Hiding the Console
    By Rare177 in forum C Programming
    Replies: 4
    Last Post: 06-09-2004, 12:34 PM
  4. Hiding the console.
    By knave in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2003, 12:19 AM
  5. Hiding the console window?
    By XenoCodex Admin in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21