Thread: Transparent Console

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Smile Transparent Console

    Righty-o, so ive been using VB6 for a break from c++ for a while...
    And now im back to the basics.
    And stuck.
    .
    .
    I'm using this:
    Code:
    #include <iostream>
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    using namespace std;
    int main()
    {
        HWND hWnd = GetConsoleWindow();
        SetConsoleTitle("Test");
        SetWindowLong(hWnd, GWL_EXSTYLE,
        GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
        SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA);
        system("PAUSE");
        return 0;
    }
    To try and make the console window translucent, but failing miserably.
    Any idea's guys?

    Thanks guys.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Check the return value from each function, and show the GetLastError() if zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Sorry for this... But, English?
    I'm just starting from the basics again, .
    VB6 dumbed me down.
    Thankyou, matsp.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Something like this (not in English, I think what I wrote classifies as English - but an example is probably better):
    Code:
    void error()
    {
        cout << "Error: " << GetLastError() << endl;
        system("PAUSE");
        exit(1);   // Naughty, but otherwise we would need a lot of nested if or goto's. 
     }
    
    int main()
    {
        HWND hWnd = GetConsoleWindow();
        if (!hwnd)
            error();
        if (!SetConsoleTitle("Test"))
           error();
        if (!SetWindowLong(hWnd, GWL_EXSTYLE,
                                         GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED))
            error();
        if(!SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA))
           error();
        system("PAUSE");
        return 0;
    }
    Then look up the error code you get (I'm 99% sure that you will) on:
    http://msdn2.microsoft.com/en-us/lib...81(VS.85).aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In other words:

    Code:
    #include <iostream>
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    using namespace std;
    int main()
    {
        LONG lRet;
        HWND hWnd = GetConsoleWindow();
        SetConsoleTitle("Test");
        lRet = SetWindowLong(hWnd, GWL_EXSTYLE,
            GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
        // What is lRet equal to?
        lRet = SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA);
        // What is lRet equal to?
        system("PAUSE");
        return 0;
    }
    Also note that it's very important that if you break a long line into several, add indentation to the rest of the lines, starting from line 2, like I did and marked with red.
    I didn't even see it was two lines until I started transforming the code because you hadn't done that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Woah, thankyou soo very much.
    Error: 5.
    Be right back, looking it up.

    EDIT: Access is denied. What.The.Hell? Vista error?
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    May be. I'd look at the functions (documented at MSDN) and any reasons they might fail. Try running your app as administator, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It may of course help to add a string to the arguments of error() and tell us WHICH one of the functions returned that error - I expect it may be that the console window doesn't accept the alpha value, but that's just a guess... [And the driver could also block this sort of setting, I expect].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Running as admin, same result.
    Hm...
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bradszy View Post
    Running as admin, same result.
    Hm...
    Yes, I expected that to be irrelevant - you should be able to set YOUR own window to transparent if you like, whether you are a regular user or administrator - but it may be that console windows can't be set to transparent, for example. Which of the functions are failing?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    I checked, and

    if (!SetWindowLong(hWnd, GWL_EXSTYLE,
    GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED))
    And
    if(!SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA))
    Seem to be failing.
    Last edited by bradszy; 04-16-2008 at 05:15 AM.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I tried on my XP machine, and I get error 5 from SetWindowLong(), and then (probably as a consequence) error 87 (Invalid parameter) from the SetLayeredWindowsAttributes()).

    As I just saw your update, I expect this is similar to your experience.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Any work around or fix?
    Now that we're on the same page, what do you think is the cause?

    EDIT: I used
    #include <iostream>
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    using namespace std;

    int main()
    {
    int ERRORNUMB = 0;
    HWND hWnd = GetConsoleWindow();
    if (!hWnd)
    ERRORNUMB = 1;
    cout<<ERRORNUMB;
    if (!SetConsoleTitle("Test"))
    ERRORNUMB = 2;
    cout<<ERRORNUMB;
    if (!SetWindowLong(hWnd, GWL_EXSTYLE,
    GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED))
    ERRORNUMB = 3;
    cout<<ERRORNUMB;
    if(!SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA))
    ERRORNUMB = 4;
    cout<<ERRORNUMB;
    system("PAUSE");
    return 0;
    }
    To get the errors... Correct?
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I did this
    Code:
    #include <stdio.h>
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    
    #define CHECK_ERROR(x) if (!x) error(__FILE__, __LINE__, GetLastError())
    
    
    void error(const char *file, int line, int err)
    {
      printf("&#37;s:%d: Error %d\n", file, line, err);
    }
    
    int main()
    {
        LONG lRet;
        HWND hWnd = GetConsoleWindow();
        CHECK_ERROR(hWnd);
        SetConsoleTitle("Test");
        lRet = SetWindowLong(hWnd, GWL_EXSTYLE,
            GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
        CHECK_ERROR(lRet);
        lRet = SetLayeredWindowAttributes(hWnd, 0, 50, LWA_ALPHA);
        CHECK_ERROR(lRet);
    	
        system("PAUSE");
        return 0;
    }
    It would help to do something like:
    Code:
    #include <stdio.h>
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    
    #define CHECK_ERROR(x, name) if (!x) error(__FILE__, __LINE__, name, GetLastError())
    
    
    void error(const char *file, int line, const char *name, int err)
    {
      printf("%s:%d: Error on %s: %d\n", file, line, name, err);
    }
    
    int main()
    {
        LONG lRet;
        HWND hWnd = GetConsoleWindow();
        CHECK_ERROR(hWnd, "GetConsoleWindow");
    ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    K:\Computers\C++ stuff\C++ Projects\Untitled1.cpp:21: Error 5
    K:\Computers\C++ stuff\C++ Projects\Untitled1.cpp:23: Error 87

    I tried your error checking code, and seems to be alot more efficient .
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Transparent panels overlapped
    By AtomRiot in forum C# Programming
    Replies: 3
    Last Post: 04-21-2007, 09:16 PM
  3. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM