Command button properties

Posted on 2005-04-21
Last Modified: 2013-12-25
Hi there

I have several command buttons which I need to set certain properties.

I already have some code that aligns the caption on the command button to the left, this is:

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_STYLE = (-16)

lngOldValue = GetWindowLong(cmd_btn1.hWnd, GWL_STYLE)
SetWindowLong cmd_btn1.hWnd, GWL_STYLE, lngOldValue Or &H100

How do I set the background and foreground colours etc???

By simply typing "cmd_btn1.BackColor = RGB(148, 185, 226)", then I essentially overwrite what I have done in the above code and hence my caption returns to centre.

I don't really understand what GWL_STYLE is and I am assuming that I somehow have to incorporate my colour scheme using this method somehow?

Any help would be great!!!


Question by:Colly321
    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    Change Style Propery of button to
       1 -Graphical
    and try

    Author Comment

    Nah thats not it - it allows me to change the colours etc, but I no longer can set the caption to be left justified???
    LVL 10

    Accepted Solution

    Hi, Colly321.

    This does seem somewhat involved.  For example, to set the ForeColor of a command button, see this URL:

    This link also discusses this topic, and itself links to a 3rd party solution:

    Or, along shijusn's comment, go with the Style Property of "Graphical" and show an image on the command button (e.g. with the image being a snapshot of text that is aligned and colored as you wish).

    Author Comment

    Hi edwardiii

    I think it comes down to either:

    1. Colouring the background and accepting the caption is going to be aligned centre


    2. Aligning the caption left, and accepting the standard windows colour.

    I personally don't want to create images, which although they may look nice, they create more work in the long run, especially if the text needs changing on them?

    Your 2 links aren't really what I want?


    LVL 10

    Expert Comment

    A third option, with the ( link as an example, is to obtain a third-party button code, which will allow you to do everything you want.  Short of third-party code, you may be able to get the ForeColor API to work in tandem with your existing Left-alignment code, but setting BackColor hasn't been addressed at all.

    Leave this post up for a while; another expert may provide exactly what you need.  You may even contact the page admin (bingie) to verify.  But thus far, everything I'm scanning online seems to indicate the fastest, easiest way to get what you want is to acquire a third-party solution as with
    LVL 48

    Expert Comment


    USe there command buttons. Anything you can ever dream of doing with a button is there.
    LVL 18

    Expert Comment

    Here are a few excellent free XP controls that I use and the command button is too good. You can give it an XP or a classic look as well:

    Author Comment

    Hi guys

    Thanks for all your help, I have pretty much given up on this - there are ways to do both my requests, but it is pretty tricky to acheive both.

    I think that the best way to think about it is to remember that it is a Windows program - not a Director style CD-ROM!!!!

    My clients like the fancy graphics etc which I think is not always the best method of doing things!!!



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now