Important alert: (current site time 7/16/2013 3:50:06 AM EDT)
 

VB icon

Real C++ Buttons (Fixed)

Email
Submitted on: 2/10/2000
By: Randy Mcdowell (Nebuland Technologies)  
Level: Advanced
User Rating: By 8 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 36657
 
     Notice: THIS CODE HAS BEEN FIXED! PLEASE READ THE COMMENTS AT THE BOTTOM OF THE PAGE TO SEE WHAT WAS WRONG BEFORE. Create real C++ style command buttons with ONE line of code!!! You can use this code on any of your commands buttons. You will loose any images and the BackColor of the button, but that really doesn't matter because you can 'paint' images onto the button using the API. Make sure you set the style property to 'Graphical' or this will not work!. OK here is how it works. You see, by default Visual Basic's command buttons are drawn with the 'Default Push Button' style, which makes them flat and ugly when pushed. But when you set the style to 'Graphical' its really just a C++ button with a single pixel outline drawn around it. This code basically just stops the button from drawing that line to create a sunken style button. This code is not compatible with Visual Basic 4.0/32 because its buttons don't have the style property. However, in Visual Basic 4.0 you can apply the code to an option button and it will appear as a C++ push button (they both have the same basic events and properties: Click, MouseMove, etc...) which is a 'rigged' kind of way of getting it in to Visual Basic 4.0...hehe. I wrote this code because I got tired of all the people trying to create C++ style buttons with CreateWindowEx and Message Hooks, and all that other difficult stuff.

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Real C++ Buttons (Fixed)
'**************************************
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
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Real C++ Buttons (Fixed)
' Description:Notice: THIS CODE HAS BEEN FIXED! PLEASE READ THE COMMENTS AT THE BOTTOM OF THE PAGE TO SEE WHAT WAS WRONG BEFORE. 
Create real C++ style command buttons with ONE line of code!!! You can use this code on any of your commands buttons. You will loose any images and the BackColor of the button, but that really doesn't matter because you can 'paint' images onto the button using the API. Make sure you set the style property to 'Graphical' or this will not work!. OK here is how it works. You see, by default Visual Basic's command buttons are drawn with the 'Default Push Button' style, which makes them flat and ugly when pushed. But when you set the style to 'Graphical' its really just a C++ button with a single pixel outline drawn around it. This code basically just stops the button from drawing that line to create a sunken style button. This code is not compatible with Visual Basic 4.0/32 because its buttons don't have the style property.
However, in Visual Basic 4.0 you can apply the code to an option button and it will appear as a C++ push button (they both have the same basic events and properties: Click, MouseMove, etc...) which is a 'rigged' kind of way of getting it in to Visual Basic 4.0...hehe. I wrote this code because I got tired of all the people trying to create C++ style buttons with CreateWindowEx and Message Hooks, and all that other difficult stuff.
' By: Randy Mcdowell (Nebuland Technologies)
'
' Inputs:You will need the hWnd of the command button.
'
' Assumes:Set the buttons style property to 'Graphical' first!!!
'
' Side Effects:You will loose the backcolor and images.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5983&lngWId=1'for details.'**************************************

Sub Form_Load()
 ' Command1.Style = 1 ' Graphical
 SendMessage Command1.hWnd, &HF4&, &H0&, 0&
End Sub


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/11/2000 4:06:07 AMAlex

It's not work!
(If this comment was disrespectful, please report it.)

 
2/11/2000 10:44:48 AMWalker

I don't see that this code does anything. It seems to have the same effect just to set a button's appearance to Graphical. At least, that's what I see under my copy of VB 5 Enterprise Edition. Maybe other versions behave differently(?)
(If this comment was disrespectful, please report it.)

 
2/11/2000 8:41:44 PMSimon

Hey Randy - this does not work. The effect is the same as setting a buttons property t graphical... I use VB6 Enterprise.
(If this comment was disrespectful, please report it.)

 
2/12/2000 11:57:57 PMRandy Mcdowell

Part #1 of Message!
-------------------

For all you who can't get the code to work, let me take you through the steps:

First of all, The Professional and Control Creation editions of Visual Basic 5 have an AutoOverRide feature that keeps you from modifying the style of a button via the API when the control's style property is set to 'Standard'. However, when it is set to 'Graphical', you can modify this attribute via the API. Here is how this is done:

(Microsoft Platform SDK)

(Put this in the form with the button, if you put it in a module, change private reserved words to public)

Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

' SetStyle Button Message
Private Const BM_SETSTYLE = &HF4

' Available PushButton Styles
Private Const BS_DEFPUSHBUTTON = &H1& ' Standard
Private Const BS_PUSHBUTTON = &H0& 'API Style

(If this comment was disrespectful, please report it.)

 
2/13/2000 12:02:57 AMRandy Mcdowell


Part #2 of Message!
-------------------

Then when you send the message to the buttons hWnd it sets the style. Note: For some editions of Visual Basic you might have to use FindWindow to get this to work properly:

Dim hCmd as long

hCmd = FindWindow("Button", Command1.Caption)

SendMessage hCmd, BM_SETSTYLE, BS_PUSHBUTTON, 0&

(If this comment was disrespectful, please report it.)

 
2/13/2000 12:05:32 AMRandy Mcdowell

Please respond with what kind of code you would like me to write in the future, I need to know what other people need in their applications.
(If this comment was disrespectful, please report it.)

 
2/13/2000 9:05:38 AMjlewey

The code works if you fix the wMsg part of the call.
in your line
SendMessage Command1.hWnd, &H4&, &H0&, 0&
should read &HF4 instead of &H4&

(If this comment was disrespectful, please report it.)

 
2/14/2000 1:51:26 AMRandy Mcdowell

I am sorry I did not see my mistake but I have re-posted the code as *fixed*. Thank you very much for pointing that out.
(If this comment was disrespectful, please report it.)

 
2/14/2000 11:00:00 AMvb lucas

the code that be cool is to make is graphical menu buttons like the one in office 2000.
(If this comment was disrespectful, please report it.)

 
2/14/2000 10:45:53 PMRandy Mcdowell

I'm not trying to create new buttons or controls, I'm trying to enhance what VB already has without needing the overhead of a third-party OCX, that is my mission.
(If this comment was disrespectful, please report it.)

 
2/14/2000 10:48:49 PMVelure

Wow! This code is SSSSOOOO simple yet it is so great to make your apps look professional...why didn't anybody think of this before... but if they did I didn't know about it. Thanks man and keep on programmin'!
(If this comment was disrespectful, please report it.)

 
2/14/2000 10:51:53 PMRandyMcdowell

yes the code is very simple. But I don't understand why it is getting so many votes...I mean it's not like it can animate a sprite on a 3d graphical engine at pentium speed. Could you people please tell me what you like about this code so much that you are voteing for it???
(If this comment was disrespectful, please report it.)

 
2/14/2000 10:56:15 PMShannon The Sweetheart Hacker

Duh! Do you know how long people have been trying to get C++ buttons into their app...I mean for a long time it has just been one of those things that only VC++ programmer's could do..kinda like the DLLs. But now you fixed this little problem with ONE line of code..thats what makes this simple stuff so great...

Deadly Kisses To All!!
~~ STSH ~~~
(If this comment was disrespectful, please report it.)

 
2/19/2000 9:47:04 PMjahan

Great code...
(If this comment was disrespectful, please report it.)

 
2/22/2000 3:52:39 PMJason Alex

This is a perfect example of how you can use the API to make VB more powerful...I'll give you a Good vote for the code, I would give Excellent if you had included the routines to paint graphics onto the button with the API.
(If this comment was disrespectful, please report it.)

 
2/22/2000 4:00:04 PMClamped Balls

I don't have to MsgHook the button to see if it is being clicked, pushed, etc.. Do I? I mean how do I know when the button is being clicked?
(If this comment was disrespectful, please report it.)

 
2/22/2000 4:03:36 PMRandy Mcdowell (Author)

Hey Clamped Balls (that sounds very painful) this code just modifies an existing VB command button, which means it will use the same Font object and still have all the same events, etc.. The only things you will loose is your BackColor and any images. Hope this helps!
(If this comment was disrespectful, please report it.)

 
2/24/2000 5:15:00 PMd00d

could someonw please please explain what the hell this code is supposed to do????
(If this comment was disrespectful, please report it.)

 
2/25/2000 2:04:20 PMTom Welch

Swell, It took me a few minutes to notice what effect the code was causing... frankly, it's not worth it to me or my users. Thanks for the effort, though.
(If this comment was disrespectful, please report it.)

 
3/1/2000 1:06:02 AMRandy Mcdowell

At least you responded politely, thank you. As for the code, this was just a basis for an entire library of button functions that I am working on, I just submitted this code to see how may hits it would actually get, and BOY WAS I AMAZED!!
(If this comment was disrespectful, please report it.)

 
3/11/2000 3:51:12 AMJohannes Eder

It's not that cool, but try &H8&!
This makes the button REALLY flat (without any borders etc) so you can have something like a REAL FOCUSABLE imagebox! Whoa!
(If this comment was disrespectful, please report it.)

 
3/31/2000 1:39:43 AMHoward Grover

I thought is was a nice touch Randy,
What I had to do in the past was to use a check box, set to Graphical put a line of code in the click event chk1.vbUnchecked which simulated a cmd button then I just made sure the background image was the same as the button colour RGB 192 etc.
This allows me to kill 2 birds with one stone as they say, a graphic and C++ style button.
If only that pesky black square could be removed from the inside of the button it would be perfect!
Good stuff and I use your code iterating thru the command button collection in one hit using TypeOf etc.

(If this comment was disrespectful, please report it.)

 
3/31/2000 8:29:15 AMHG

I recieved mail from -stai@email.com- asking how to iterate thru a control collection but could not reply as the address was rejected by server so if you happen to look back here it is.
Private Declare Function SendMessage Lib "User32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long

Public Sub SetButtonStyle(frm As Form)
' C++ Stle Command buttons
' Set Styles to 1 (Graphical) ps.
' Will lose picture if one assigned to button
' so I am checking for absence of picture first.
Dim ctrl As Control

On Error Resume Next

For Each ctrl In frm.Controls

If TypeOf ctrl Is CommandButton Then
If ctrl.Picture = 0 Then _
Call SendMessage(ctrl.hWnd, &HF4&, &H0&, 0&)
End If

Next ctrl

End Sub 'SetButtonStyle(frm As Form)


'call from form load is:
Call SetButtonStyle(Me)


(If this comment was disrespectful, please report it.)

 
6/23/2000 10:49:48 AMFormatC

It has been quite a while since someone last posted something on this.... oh well... anyway, here's my comment:
First of all, I must say, this is quite handy... definently the most used single line of code of everything I do - there's nothing like showing nice Cpp-looking programs magically coming out of VB to my coworkers, whose programs are all, well, flat.
But, now, for my contribution to this code: You do not necessarily need to use a Graphical commandbutton. You see, VB apparently does all its keep-the-flat-button stuff in its own behind-the-scenes GotFocus procedure... but, lucky for us, they decided to implement their pseudo-GotFocus procedure before the one available to us. So, if you simply put this SendMessage call into the GotFocus event of any command button with normal style, VB will never get a chance to show that button as flat and ugly...

Just my two cents...
(If this comment was disrespectful, please report it.)

 
7/1/2000 5:30:54 PMRoman

This code works GREAT!
Thanx!
(If this comment was disrespectful, please report it.)

 
8/16/2000 1:40:14 PMTom

The code works, but was it really THAT important????
(If this comment was disrespectful, please report it.)

 
8/24/2000 10:44:28 PMBrian

OK, so the VB command buttons look ugly and flat when the're pushed, but frankly, in 12 years of using Windows (5 of them using Win32), I really haven't noticed the difference. I know that it's the code we've all been waiting for, but frankly, it's one line of code - and it's the #5 best rated code of all time on PSC. Why? Sure, it's cool, but does it really make a dramatic difference in your programs? Is it really that important? No offence, I'm greatful that someone thought of this, but I'll stick with my regular flat command buttons, and if I need heavy duty looks, I'll use the upcoming B2Skin controls (which use skins directly from B2, and can also provide translucency and window skinning). Or I'll use the Flat Button OCX (it's free, and looks cool!). As I said previouslly, I'm not insulting your code, I just wonder why it's #5 on PSC. That's my $.02
(If this comment was disrespectful, please report it.)

 
9/10/2000 7:36:16 PMVasilis Sagonas

Can we remove also the focus points
...........
. Button .
...........
things when its focused? And please please is there any way to keep the picture? Thanks a lot! :-)
(If this comment was disrespectful, please report it.)

 
10/13/2000 2:34:06 PMGabriel Pensador

I think that this got so many votes because it lets VB programmers pretend to be VC++ programmers.

Wouldn't it be great if all these people took the trouble to learn VC++ and write these _professional_ apps they strive for.
I would like to recomend an excellent book:
Using Visual C++ 6 (special edition)
by Kate Gregory

I guarantee that anyone who reads this book will learn VC++.
(If this comment was disrespectful, please report it.)

 
10/31/2000 5:22:45 PMMangaMorgan

This code is good 4 doing the little [_], [o] nd [X] buttons on the title bar
(If this comment was disrespectful, please report it.)

 
11/18/2000 3:19:28 PMlivinINvb

under paint this will turn a check, frame, and option to look like a button!
(If this comment was disrespectful, please report it.)

 
10/23/2001 9:48:08 AMGenocide Flux

No offense, but most vb programmers are not able to afford VC++ & VB (considering vb is easier for the script kiddies to find pirated.) i spent over $200 on vb6 pro edition and that was on sale, plus not many places have vc++, or any others then the starter kits. Although there is always Borland. Although in the long run (right now) it's better to learn C++ but i find that vb suites most my needs and i don't use my vc++ 6 all that much.
Then again it is easier to make forms with vb, but i feel if you settle for a generic form generator then you settle for what it gives you. Plus, vb is used more for applications (from what i have seen) and c++ is more game used. But again, that is only from what i've seen on the market place. C++ also has a lot of applications to it's name as well though. But all this is a different story.
(If this comment was disrespectful, please report it.)

 
11/6/2001 11:00:06 PMtec

Im sorry, but this code is bad... i have no idea why it got so many excellent ratings, and it really takes the quality of psc voting down.. i mean... if stuff like this can make it into the code of the month, what else can! ive seen code that is truely remarkable, and it didnt even get 3 excellent ratings... obviously there is something wrong here..
(If this comment was disrespectful, please report it.)

 
3/10/2002 1:11:36 PMTim Sørensen

This really rocks!!
Beautifull button.

I´m implementing it in all
my apps.
(If this comment was disrespectful, please report it.)

 
3/15/2002 10:04:23 AMUDMX IOCP®

code sound so great!!! buT.. I can't seem to make it work... can't you make a project and post it or something??
(If this comment was disrespectful, please report it.)

 
10/28/2002 5:34:42 AMCharles R

I have just read all of this and just let me say Wow. Haven't tried it yet, but I'm sure it's decent, anyway I have a problem you may be able to solve with one of my projects. I need to run some code when the desktop gets clicked, I'm not sure if the desktop icons are a child of the desktop or seperate so this may be two problems in one. Just detecting the desktop click and not the icons click is ok if it's not possible to do both.

Thanking you in Advance

Charles
(If this comment was disrespectful, please report it.)

 
4/29/2003 6:23:11 AMCasey (Kc) Kneale

I have a sort of dumb question that may or may not go along wiht this at all...how can i make the mouse move to a different part of the screen thenhave the same acttion as a mouseclick?eamil me if ya wanna help...KcKneale777@hotmail.com
(If this comment was disrespectful, please report it.)

 
6/3/2004 12:02:27 PM

hello..this is really cool...i have a question like...how to dynamically change the style of an option button to graphical and apply backcolor....please do let me know...
(If this comment was disrespectful, please report it.)

 
7/6/2004 1:10:00 PM

Yeah I kinda agree with what a few people have said It's not great and the change is practically unoticable, there has been alot other great code that doesnt get any votes what are you people thinking.. thanks for the effort though randy - Stop multi voting for your code LoL
(If this comment was disrespectful, please report it.)

 
9/23/2004 3:11:09 PM

doh, it kills the selected style image (tab selection) when used with a manifest res file. ya know, the blue outline that remains after you click a button with the XP-Style skinned buttons.

but for those that are interested, it's easy enough to just not call the SendMessage when the app is skinned, here's how.. (NEXT POST, pfft char limit)

(If this comment was disrespectful, please report it.)

 
9/23/2004 3:13:36 PM

Public Function IsThemedXP() As Boolean
Dim osVer As OSVERSIONINFO
osVer.OSVSize = Len(osVer)
GetVersionEx osVer
'// Evaluate return. If greater than or equal to 5.1 then running
'// WindowsXP or newer.
If osVer.dwVerMajor + osVer.dwVerMinor / 10 >= 5.1 Then
'// Check for Active Visual Style
If IsAppThemed Then
'// Double Check by assessing DLL version loaded
Dim dllVer As DLLVERSIONINFO
dllVer.cbSize = Len(dllVer)
DllGetVersion dllVer
IsThemedXP = (dllVer.dwMajor >= 6)
End If
End If
End Function

so i took FormatC's suggestion and call it from the GotFocus event of my command button so i can avoid using graphical.

Private Sub Command1_GotFocus()
If Not IsThemedXP Then SendMessage Command1.hWnd, &HF4&, &H0&, 0&
End Sub
(If this comment was disrespectful, please report it.)

 
9/23/2004 3:14:44 PM

and for the person who asked about killin the focus rectangle, you can use
Private Const WM_KILLFOCUS = &H8
SendMessage Command1.hwnd, WM_KILLFOCUS, 0, 0

(If this comment was disrespectful, please report it.)

 
4/7/2005 12:59:36 AMXeon Productions

Private Sub Form_Load()
Dim cmd As CommandButton
For Each cmd In Me.Controls
SendMessage cmd.hWnd, &HF4&, &H0&, 0&
Next cmd
End Sub
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.