CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    How to change the Forecolor of Command Button?





    Hi Gurus,


    I have a simple question!. How to change the ForeColor of a command button?


    VB's 'Properties' window does not show 'ForeColor' for a command button.

    It allows 'BackColor' to be changed and takes effect when 'Style' is set to 1.

    What if i want to set 'ForeColor'?


    But VB allows 'ForeColor' setting for Check Box & Option Button.

    Right now i am faking using check box , with Style =1, it looks like a CommandButton and the first line in the Click event handler, would be to

    set the value back to zero, to avoid 'pushed' look.


    I dont want to use Sheredian controls or some such ActiveX controls just for the sake of color!, (unless unavoidable)


    Do i need to use 'MaskColor' setting, to achieve the effect?

    Interestingly, MSDN ( atleast the copy that i have, slightly old one, '98)

    says 'ForeColor' property is available for command button also!


    Can you please give me some hints?


    Ravi Kiran



  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to change the Forecolor of Command Button?



    Hi


    There's no easy way to do this in VB. When programming 'true' windows (ie. c/c++/delphi etc), you can create the Button Control with owner-drawn style which allows you to paint the command button in whatever way you want.


    There is an excellent article at http://vbaccelerator.com that shows how to do this in VB with subclassing. You can get to the link from the codeguru site :


    http://www.codeguru.com/vb/links/jump.cgi?ID=1678


    Regards


    Chris Eastwood


    Codeguru - the website for developers

    http://www.codeguru.com/vb




    Drawing Buttons, Option Boxes and Check Boxes In Your Own Style

  3. #3
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: How to change the Forecolor of Command Button? Yes it's possible!



    Hi


    Yes you can do this in VB! (There's no need to subclass the button for this)


    Use the SetTextColor API.


    This is the declaration:

    Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long


    And this is call itself:

    Call SetTextColor(MyCommand1.hDC, [a long value containing the color, for instance vbBlack])

    hmm ok vbBlack isn't a great example.. LOL vbRed will look fine.


    Hope (I'm sure :-) this helps


    Crazy D

  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to change the Forecolor of Command Button? Yes it's possible!



    Hi


    I couldn't get that to work - is this VB5/6 ?


    You also need to get the Device Context from the command button (if using VB5 - don't know about VB6), ie.


    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long


    Dim lDC As Long


    lDC = GetDC(Command1.hwnd)


    Ideas ?


    Chris Eastwood

    CodeGuru - the website for developers

    http://www.codeguru.com/vb




  5. #5
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: How to change the Forecolor of Command Button? Yes it's possible!





    Hi,


    I too tried, and it doesn't work, atleast in VB 5.0


    I tried with & w/o style set to 1; with & w/o Refresh, i.e call command1.Refresh after SetTextColor. Even call TextOut after setting color

    all possibilities. But nothing works the way it is supposed to!


    Ravi

  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Try my original suggestion



    Try the original link :


    http://www.codeguru.com/vb/links/jump.cgi?ID=1678


    The subclassing is very easy with Steve's subclassing DLL (or you can just compile the source of the DLL into your own app).


    Regards


    Chris Eastwood


    Codeguru - the website for developers

    http://www.codeguru.com/vb



  7. #7
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Try my original suggestion





    Hi,


    Ys. I have downloaded and am trying. It seems to require X-Times dll sSubtimer dll or something. I am on it right now..


    Ravi

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

Featured