Results 1 to 18 of 18

Thread: VB Console Background Screen Color

  1. #1

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Arrow VB Console Background Screen Color

    Hello everyone,

    im looking for a way to change the bgcolor of a VB console color to blue.
    this is a small example, but i only know how to change the text color.

    Sub Console()
    cInit
    cTitle "Console"
    cPrint "" & vbCrLf
    cPrint "Hello everyone" & vbCrLf
    cPrint "" & vbCrLf
    cColor 9
    cPrint "Hello Again!" & vbCrLf
    cPrint "Press Enter to Exit"
    cTerminate
    End Sub

    Some option colors:

    Code:
    Public Sub cColor(Optional ByVal intForeColor As Integer, _
    Optional ByVal intBackColor As Integer)
    'Manually set colors using the SetTextAttribute function.
    
    If IsNull(intForeColor) Then
    'User left blank [OK]
    Else
    Select Case intForeColor
    
    Case 0
    SetConsoleTextAttribute hConsoleOut, 0
    
    Case 1
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED
    
    Case 2
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_GREEN
    
    Case 3
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_BLUE
    
    Case 4
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED Or _
    FOREGROUND_GREEN
    
    Case 5
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED Or _
    FOREGROUND_BLUE
    
    Case 6
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_GREEN Or _
    FOREGROUND_BLUE
           
    Case 7
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED _
    Or FOREGROUND_BLUE Or FOREGROUND_GREEN
    
    Case 8
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY
    
    Case 9
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_RED
            
    Case 10
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_GREEN
    
    Case 11
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_BLUE
    
    Case 12
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_RED Or FOREGROUND_GREEN
    
    Case 13
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_RED Or FOREGROUND_BLUE
    
    Case 14
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_BLUE Or FOREGROUND_GREEN
    
    Case 15
    SetConsoleTextAttribute hConsoleOut, FOREGROUND_INTENSITY _
    Or FOREGROUND_RED Or FOREGROUND_BLUE Or FOREGROUND_GREEN
    
    Case Else
    cPrint "Error: " & intForeColor & " is not a valid color."
    End Select
    End If
    End Sub
    can someone tell me how?
    i will appreciate any help i can get.

    Thanks

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB Console Background Screen Color

    Look at your source and see if you have any BACKGROUND_RED, _GREEN, _BLUE, _INTENSITY constants. The background colors are those constants and they are sent along with the foreground values using OR.

    Since I have it, I'll pass you the constants... The reverse constant below swaps foreground/background colors.
    Code:
    Private Const BACKGROUND_BLUE As Long = &H10
    Private Const BACKGROUND_GREEN As Long = &H20
    Private Const BACKGROUND_INTENSITY As Long = &H80
    Private Const BACKGROUND_RED As Long = &H40
    Private Const COMMON_LVB_REVERSE_VIDEO As Long = &H4000
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Thank you LaVolpe, can i ask you how to use it?
    yeah, how to add it to my code, like i did with this:

    Sub Console()
    cInit
    cTitle "Console"
    cPrint "" & vbCrLf
    cPrint "Hello everyone" & vbCrLf
    cPrint "" & vbCrLf
    cColor 9
    cPrint "Hello Again!" & vbCrLf
    cPrint "Press Enter to Exit"
    cTerminate
    End Sub

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: VB Console Background Screen Color

    SetConsoleTextAttribute hConsoleOut, FOREGROUND_BLUE Or BACKGROUND_RED

  5. #5

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Hi baja_yu,

    you know whats the thing? ...i think i am not doing right.
    i get errors, i would like to know how i can add the code.

    Like in this example:

    Sub Console()
    cInit
    cTitle "Console"
    cPrint "" & vbCrLf
    cPrint "Hello everyone" & vbCrLf
    cPrint "" & vbCrLf
    cColor 9
    cPrint "Hello Again!" & vbCrLf
    cPrint "Press Enter to Exit"
    cTerminate
    End Sub

    This is part of the same bas where the rest of the colors code are.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB Console Background Screen Color

    You'd have to validate this. Using bit shifting, you can have 1 easy function.
    Code:
    Private Sub SetConsoleColors(Optional ForeColor As ColorConstants = vbWhite, _
                              Optional BackColor As ColorConstants = vbBlack, _
                              Optional ForeHighIntensity As Boolean = False, _
                              Optional BackHighIntensity As Boolean = False) 
    
         Dim lColor As Integer
         lColor = ((BackColor And &H100000) \ &H10000) Or _
                 ((BackColor And &H2000) \ &H100) Or _
                 (BackColor And &H40) Or Abs(BackHighIntensity) * &H80 Or _
                 ((ForeColor And &H10000) \ &H10000) Or _
                 ((ForeColor And &H200) \ &H100) Or _
                 (ForeColor And &H4) Or Abs(ForeHighIntensity) * &H8
    
         SetConsoleTextAttribute hConsoleOut, lColor
    
    End Sub
    sample call...... SetConsoleColors vbYellow, vbBlue

    Edited: Changed Long to Integer in the lColor declaration above. Per the MSDN reference the console attribute is of type: WORD
    Last edited by LaVolpe; Mar 22nd, 2010 at 10:40 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    I added it to my bas but it doesn't do nothing.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB Console Background Screen Color

    Doesn't do anything? Surely it does . Whether it does what you intended or not is another question.

    Take this line in your sample code from post #1 above: cColor 9
    And replace it with: SetConsoleColors vbYellow, vbBlue

    If you have a valid hConsoleOut window, I'd think the color should change?

    Edited: Oh, change lColor from Long to Integer. I just noticed the parameter per MSDN is Word not DWord
    That is very likely why no color changes occurred. Value of zero was being interpretted which keeps default white/black colors.
    Last edited by LaVolpe; Mar 22nd, 2010 at 11:29 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    LaVolpe:
    Doesn't do anything? Surely it does .
    Whether it does what you intended or not is another question.
    what am i intended to do? ...
    My first question (#1) explains well what i really want,
    and what i really want is to turn the whole console bg blue.

    to be honest, your example code didn't work for me.

    I try this other example:
    Code:
     SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED Or FOREGROUND_INTENSITY Or BACKGROUND_BLUE
    but it doesn't turn the bg blue, it does, but only for the area of the text.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB Console Background Screen Color

    Quote Originally Posted by C0der View Post
    ...but it doesn't turn the bg blue, it does, but only for the area of the text.
    Gotcha; the entire window background color should be blue or whatever color you want. Sorry, don't know the answer.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Is OK, it's not your fault, i think there's no way to do this.

    Thank you anyway

  12. #12
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: VB Console Background Screen Color

    Give this a try. It sounds like it may be what you are after.
    Slower than a crippled Vista
    More buggy than a fresh XP install
    Look! Down the road, some 50 miles behind the drunken snail.
    It's Ubuntu!

  13. #13

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Thank you AsmIscool,
    but this example does not coordinate with my project,
    my project is about VB Console, something like DOS.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB Console Background Screen Color

    I generally send people here for these sorts of questions:

    http://vb.mvps.org/samples/Console/

    Karl's sample project is quite a dumpster-load of code and API calls, but he has probably provided a way to do what you want here. If nothing else perhaps you can find the parts you actually need in his code, extract them, and clean them up. Or you might just use it as-is.

  15. #15

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Hi dilettante,
    WoW, i am impressed with the code in this page, very nice,
    some how it doesn't have no explanation of the whole bg color. :-(

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB Console Background Screen Color

    Yes I agree. He has a lot of stuff in there, but to dig out any single feature to use it yourself you have to follow the trail of breadcrumbs. In this case look at what happens in his BackColor property.

  17. #17

    Thread Starter
    Lively Member C0der's Avatar
    Join Date
    Mar 2010
    Location
    Somewhere in Internet
    Posts
    113

    Re: VB Console Background Screen Color

    Hi dilettante,
    hey sorry for what happend in the other post,
    i really didn't realise what i was talking about.

    and once again thank you for the page you suggested,
    i will live this question open till someone come with an easy solution,
    is just that i wanted to make this project as simple as posible.

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB Console Background Screen Color

    No problem, I don't get too worried about that sort of thing.

    If you root through Karl's code you may find he uses one API call to set those colors though. I just haven't looked at it in a long time.

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