Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

Command button Style on runtime


hello experts,


how can i change the style from the default command button at runtime?

whenever i try the Command1.style = 1 at Form_load, i get the error that this is not possible at runtime.

can this been done using api's or somthing like that?


mark
0
Mark_FreeSoftware
Asked:
Mark_FreeSoftware
4 Solutions
 
List244Commented:
Mark, what is the purpose of this?  Just set it to 1 at design, it doesn't change anything until after you
make changes anyway.
0
 
Mark_FreeSoftwareAuthor Commented:

i have a code snippet that changes the buttons to C style buttons (no ugly black border)

but that will only work when the style is graphical
0
 
List244Commented:
What is wrong with setting them at design time?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mark_FreeSoftwareAuthor Commented:


i want to add that function to a few existing projects, so it is very much work to find all command buttons
0
 
List244Commented:
If that is your only problem, I would suggest the following:

Create a new project with ONLY a module, no form.  Add this code:

Public Sub Main()
Dim temp() As String
Dim File As String
Dim CLine As String
Dim ADD As Boolean
ADD = False
temp = Split(Command$, """ """)
For i = 0 To UBound(temp)
    temp(i) = Replace(temp(i), """", "")
    Open temp(i) For Input As #1
        Do Until EOF(1)
            If ADD = True Then
                File = File & "Style = 1" & vbCrLf
                ADD = False
            End If
            Line Input #1, CLine
            If InStr(CLine, "Begin VB.CommandButton") Then ADD = True
            File = File & CLine & vbCrLf
        Loop
    Close #1
    Open temp(i) For Output As #1
        Print #1, File
    Close #1
Next i
End Sub

Then, compile that.  With that you can just drag forms to the exe and it will run and fix the form files to make all the buttons graphical style.

You CAN make a button graphical style by using GetWindowLong and SetWindowLong as well, but switching that code will be more work than
the above.  Also, I personally do not know the codes you need to set style, perhaps another expert does.
0
 
Mark_FreeSoftwareAuthor Commented:

GetWindowLong and SetWindowLong

can you pls give an example of that,

since i have already code that walks trough all command buttons at startup, i just can add that code to it, so that i only have to add that code at the startup of the form
0
 
List244Commented:
Like I said, I have never done such a thing, I do not know the codes for get and setwindowlong to change a picturebox style.
However, the code I have posted is a complete project which when compiled will allow you to drag frm files to it and it will
change all of the command buttons to graphical at design-time without you having to find all the buttons and set them manually.

Your choices are pretty much that or to wait for another expert who may be more familiar with Get and Setwindowlong on
picture boxes.  Perhaps a pointer question asking for help with Get/Setwindowlong on pictureboxes?
0
 
iHadiCommented:
Hi
Add a CommandButton to your form and paste the following code in your form.

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const BS_ICON& = &H40&
Private Const BS_BITMAP& = &H80&
Private Const BS_OWNERDRAW = &HB&

Private Sub Command1_Click()
Dim winLong As Long
winLong = GetWindowLong(Command1.hwnd, GWL_STYLE)
SetWindowLong Command1.hwnd, GWL_STYLE, winLong Or &HB& Or 4096 Or BS_ICON& Or BS_BITMAP&
Command1.Picture = Nothing
End Sub
0
 
List244Commented:
iHadi, I did something very similar to that, however you are facing the same problem I had.  Your button stops drawing correctly.
And in turn is not a proper graphical styled button.
0
 
Mark_FreeSoftwareAuthor Commented:

i don't think the only difference between the 2 styles is the gwl_style, cause i copied the style from a graphical button to a normal one, and that didnt work either....


but i have no idea what else is different.
0
 
nffvrxqgrcfqvvcCommented:
Hi, Mark


Option Explicit

Private Const BM_SETSTYLE = 244
Private Const BS_OWNERDRAW = &HB&

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

Private Sub Form_Initialize()
     SendMessage Command1.hwnd, BM_SETSTYLE, BS_OWNERDRAW, 1
End Sub

Private Sub Form_Load()
 Command1.Picture = LoadPicture("C:\WINDOWS\Greenstone.bmp")
End Sub
0
 
Mark_FreeSoftwareAuthor Commented:

when the button loses focus, the border does disappear
0
 
List244Commented:
Mark, I really think you will be best off just setting your button at design-time.
0
 
Mark_FreeSoftwareAuthor Commented:

too bad.....


if i want an addin for vb (6.0) to do that, should i open a new question or can i ask it here?
0
 
Mark_FreeSoftwareAuthor Commented:

oh well, nvm i just ask a new question and split points because you 3 took the time to listen to me and to try it.
0
 
GPrentice00Commented:
Based on dropping a button on a form and making its style normal or graphical, the difference between them in my tests was the ORing of 200B in all cases. So I dropped a handful of buttons on a couple different forms, made their background colors different (at design time), but left them all as normal ones since only background colors are a property of graphical buttons.

This may be something for you to try, since you say you have code to handle the rest of it as long as a button is made graphical...

 It may be way off the mark, but at the least, should you find a way to make it happen, you can use this code to plow through all your forms and work on all the buttons without having to explicitly name everything
-------------------------
 Test project: module1; form1, form2, form3 all with various mixes of buttons, listboxes, combos, etc command button backcolours set at designtime for visual reference.

 Project properties set for startup = Sub Main

============================= CODE =============================
 form1, form2 no code

'====== form3 code ================================================
Option Explicit

Private Sub Form_Load()
    ChangeButtonsInForm Me
End Sub

'===== module1 code ===============================================

Option Explicit

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

Private Const sChange1 As Long = &HB
Private Const sChange2 As Long = &H2000

Public Sub main()
    'load (by displaying) form 1 and 2 then process
    Form1.Show
    Form2.Show
    ProcessAllLoadedForms
    DoEvents
    ' Form3 wasn't loaded when form collection processed, but
    ' will code in its load will force itself to be processed
    Form3.Show
    DoEvents
End Sub

Public Function ProcessAllLoadedForms()
  'go through all LOADED forms in form collection and apply button changes
    Dim fTarget As Form
    For Each fTarget In Forms
        ChangeButtonsInForm fTarget
    Next
    Set fTarget = Nothing
End Function


Public Function ChangeButtonToGraphical(ByRef CMDBTN As CommandButton)
    Dim winLong As Long
    winLong = GetWindowLong(CMDBTN.hwnd, GWL_STYLE)
    SetWindowLong CMDBTN.hwnd, GWL_STYLE, winLong Or sChange1 Or sChange2
    'do other things with the command button here, you have its object reference
    Set CMDBTN.Picture = Nothing
   
    '...
End Function

Public Function ChangeButtonsInForm(ByRef fTarget As Form)
    Dim ControlItem As Control
    For Each ControlItem In fTarget.Controls
        If TypeOf ControlItem Is CommandButton Then
            ChangeButtonToGraphical ControlItem
        End If
    Next
    Set ControlItem = Nothing
End Function
0
 
List244Commented:
GPrentice00, I tried that same thing, I found the difference to be 8203.  I found this by doing a xor
of the two.  I then tried using that, however to no success.  I see the real problem was not my method,
but the picture not being set to nothing.

Mark_FreeSoftware:

Just so you know rather than &HB OR &H2000 you can simply do:

SetWindowLong CMDBTN.hwnd, GWL_STYLE, winLong Or 8203 '//Note the 8203
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now