Classic VB - How can I make my form use Windows XP Styles?
I have seen this question asked a lot, but until very recently, I never found a very straight or working answer. Lots of people ask how to make their program make use of the Windows XP visual styles (I assume this method also works for Vista) in there Visual Basic Applications.
The first thing you must do is have a project. Since you are reading this, you likely have a project you are using already. I have attached everything mentioned in this FAQ below in a ZIP file (except the .bat file of course).
If not create a new project with lots of buttons and text boxes and check boxes and radio buttons and sliders and frames and picture boxes. If you want to see a slight problem with this method, put some radio buttons and stuff in a frame.
Now. You will need to open up notepad (this works best) and type in the following:
Save this file and call it 'foo.rc' (without the quotes). Make sure to save as 'All Files' or notepad will add .txt to the end of it.
In the last line, 'foo.exe.manifest' is the name of the following file. Call it what you like, but naming it the program's compiled name followed by .manifest is best.
Now to create foo.exe.manifest. Type the following into (a blank) notepad:
Code:
Your application description here
Feel free to replace the denoted fields with your own information (Company, product, and application names, and a description). Then, save this file as 'foo.exe.manifest'.
The last file you must make is not included in the .zip file with this post because it is a batch file, which is banned from being posted. Luckily, it is very simple, so you should have no problem making it. Add the following text into notepad and save it as foo.bat:
If you installed Visual Studio somewhere else, replace the above path with it, making sure it still points to 'RC.EXE'.
The argument foo.res is the file that the batch file will make and foo.rc is the file we just made which references foo.exe.manifest. The argument '/fo' is not a typo of foo, it is a switch needed to create any resource file like this, regardless of name. This program basically brings it all together.
Now, once you have saved foo.bat, double-click it and you may see a flicker across the screen as the command line opens and closes, and then the file called foo.res should now be in the foo.bat folder. THIS IS THE FILE WE NEED!
The only reason you would maybe want to make your own foo.res instead of just using the one in the attached folder (which you certainly may) is that if your program is opened in a resource editor (e.g. ResHacker) people may read through the stuff in foo.exe.manifest. Unless you care about this, I can't think of any reason not to just use the one in the attached project.
NOW:
since we have foo.res, we need to go into VB and tell VB to use it. Either open your project you are trying to add styles to or the new project you have made and in the top right side of the screen (by default) you will see in the project box all of the files in your project. Right click it and select 'Add->Add File...' and on the dialog that opens, find the foo.res file we just made and add it to the project.
Now, to see it properly, you may need to make sure that the Resource Editor add-in is enabled. To do this, under the Add-Ins menu, click Add-In Manager and find 'VB 6 Resource Editor' and make sure the check boxes saying 'Loaded/Unloaded' and 'Load on Startup' are selected and press OK.
If you now compile your project and run the EXE, you should have successfully added the XP style to them, HOWEVER - I RECOMMEND YOU DON'T! To be sure your program will work fine with this style, you should add a bit of code to either the Sub Main() or the From_Initialize of the first form to be shown.
Code:
Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private m_hMod As Long
Private Sub Form_Initialize()
m_hMod = LoadLibrary("shell32.dll")
InitCommonControls
End Sub
Private Sub Form_Unload(Cancel As Integer)
FreeLibrary m_hMod
End Sub
This code will ensure that the Common Controls are initialized properly before your program tries to draw any controls. I don't know the exact details, but just do it.
That is it - your compiled program should work perfectly now!
One more thing you will likely notice: Some controls in a Frame will be drawn BLACK (so you can't see them). The option button is the most known one. To work around this, put all of your controls in a picture box and put the picture box in the frame. To hide the picture box (so it doesn't look ugly), set the border style of the picture box to none.
One more note - this will not take effect at design time (running your project in the IDE), so you must compile to an EXE to see the effects of it!
I hope this has helped you and if you have any problems, start a thread on it and PM me with a link!
Last edited by si_the_geek; Apr 8th, 2008 at 11:20 AM.
Re: Classic VB - How can I make my form use Windows XP Styles?
I haven't even opened VB in months now (been really busy), but I'm pretty sure I never got that code working right. And I believe there is some chance of it crashing the program or it had a dependency, I forget now...
But, if it works for you, good!
Visit here to learn to make the VB interface fit you!.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge
Re: Classic VB - How can I make my form use Windows XP Styles?
There are other options for including a manifest within a resource file without the use or RC.exe. Additionally, there is a gotcha if using VB-created custom usercontrols that may pop up and now with Vista & Win7, one may want to provide a Security manifest with/without a common controls manifest entry.
This link discusses those topics/options in the 1st 2 posts of that thread.
Insomnia is just a byproduct of, "It can't be done"