The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
1 VB 5 Custom Controls &nbsp
&nbsp

NOTE: this code has been superceded by the newer Flat Control DLL.

Flat Combo Boxes, Office 97 Style

Demonstrating flat combo boxes

Download the Flat Combo Box source code compiled directly into an EXE. (19kb)

Download the Flat Combo Box DLL and source (25kb)

&nbsp Source Code Note &nbsp
&nbsp This DLL is a binary compatible component which works with all other samples. If you compile your own copy of this DLL yourself please make sure you change the name. See disclaimer and license for more details. &nbsp
&nbsp Before you Begin &nbsp
&nbsp This project requires the SSubTmr.DLL component. Make sure you have loaded and registered this before trying the project. &nbsp

Overview
This project demonstrates how to subclass standard VB Combo Box or vbAccelerator Owner Combo and List Box controls so that they draw like the toolbar combo boxes in Office 97. The code is a translation of an excellent MFC sample written by Kirk Stowell (his site, Code Jockey, is at www.geocities.com/SiliconValley/Haven/8230. Note this is an MFC only site).

How Does It Work?
Combo boxes normally draw their own sunken border and a button border around the drop-down button. This code takes the sneaky approach of intercepting the painting messages and drawing over the default borders with nice new ones. Here are the messages that are subclassed:

  • WM_PAINT
  • WM_MOUSEMOVE
  • WM_LBUTTONDOWN
  • WM_LBUTTONUP
On receiving one of these messages, the code gets the Combo box's DC and then paints over the sunken and raised borders so that they appear either flat (when the mouse is not over) or with a soft edge when the mouse is over. Smart!

The code also ensures that it correctly identifies when the mouse moves off the control by creating a temporary API timer in code. Timer events are responded to by subclassing the WM_TIMER message sent to the combo box's window handle.

How to Make Your Combo Boxes Flat
This is the easy bit!

There are two versions of this code. One is a private class module (15kb) which you can compile directly into your executable. The other is exactly the same class built into an ActiveX DLL (25kb). The reason for providing the DLL is to make it easier to debug code using this class. Because the class has to subclass the combo box's WM_PAINT message, when you break in VB's IDE many events can fire which make it difficult to debug (and, in the worst case, could cause a crash!). By using the DLL, you don't need to worry about these problems because all the subclassing code is isolated from your project. (And since the DLL is binary compatible, and supported, you can ship it with your project as well).

Start a new project, and add a combo box to the form. If you are using the DLL, add a reference to it (it will appear as "vbAccelerator Flat Combo Box Extender" in the references list. (Make sure you have registered vbalFCbo.dll and SSubTmr.Dll before doing this). Then you only need two lines of code:

' In the declarations section of the form:
Private m_c As New cFlatComboBox

Private Sub Form_Load()

&nbsp &nbsp
' Attach your combo box to the flat combo box
&nbsp &nbsp ' drawing routines:
&nbsp &nbsp m_c.Attach cboThis


End Sub


Erm, and that's it. Hope you find it useful. Also, check out the Creating Flat Combo Boxes in a Rebar sample.

&nbsp
&nbsp


Back to top

Back to Source Code

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 6 November 1998