Results 1 to 4 of 4

Thread: Classic VB - How can I make my form use Windows XP Styles?

  1. #1

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    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:

    Code:
    #define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
    #define RT_MANIFEST                     24
    #define CONTROL_PANEL_RESOURCE_ID       123
    
    CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST             "foo.exe.manifest"
    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:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
        <assemblyIdentity 
            version="1.0.0.0" 
            processorArchitecture="X86" 
            name="CompanyName.ProductName.YourAppName" 
            type="win32" />
        <description>Your application description here</description>
        <dependency>
            <dependentAssembly>
                <assemblyIdentity 
                    type="win32" 
                    name="Microsoft.Windows.Common-Controls" 
                    version="6.0.0.0" 
                    processorArchitecture="X86" 
                    publicKeyToken="6595b64144ccf1df" 
                    language="*" />
            </dependentAssembly>
        </dependency>
    </assembly>
    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:

    Code:
    "C:\Program Files\Microsoft Visual Studio\VB98\Wizards\RC.EXE" /r /fo foo.res foo.rc
    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!

    Name:  foo.bat.gif
Views: 3237
Size:  8.8 KB

    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.

    Name:  Add File.png
Views: 3278
Size:  16.0 KB

    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.

    Name:  Res Editor Add-In.png
Views: 3565
Size:  14.5 KB

    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!

    Name:  Compiled.png
Views: 3308
Size:  11.8 KB

    I hope this has helped you and if you have any problems, start a thread on it and PM me with a link!
    Attached Files Attached Files
    Last edited by si_the_geek; Apr 8th, 2008 at 11:20 AM.

  2. #2
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Re: Classic VB - How can I make my form use Windows XP Styles?

    Actually, in my app, i was able to just add the Form_Initialize code, and that seemed to work fine... I didn't need to embed the manifest at all!

  3. #3

    Thread Starter
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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