An Advanced TabCtrl

Download Source Code and Example


The aim for this project was to control the colors of the TabCtrl. In MFC you are not able to configure your own color combinations for the TabCtrl. With this TabCtrlAdvanced you can choose your own colors for the background, the display area and the TabCtrl items.

Normally the OnCtlColor function is called in response to a message, so I use a SendMessage to get the color, although it is now also implemented as a virtual function. The message call is then mapped to the OnCtlColor in the WindowProc. I thought that this is a nice way to map a range of messages to one (or if you want more) function(s).

Get your desired color by calling:

SendMessage(WM_CTLCOLOR_XXX, (WPARAM)CDC*, (LPARAM)LPDRAWITEMSTRUCT)

WM_CTLCOLOR_XXX can be one of:

  • WM_CTLCOLOR_BKGND     For the Background, behind the Items
  • WM_CTLCOLOR_TABITEM   For the Item
  • WM_CTLCOLOR_DISPLAY   For the client area of the selected Item

The return value is a CBrush*.Note: You only have to give the LPDRAWITEMSTRUCT if you want to have the background color for an Item.

I changed the original second parameter of OnCtlColor, to get all the information of the item, so you can give every item its own color! OK this can be a bit too much color, but if you want to point out one item more than the others than it might be useful for your users.

To get the full control of the appearance of the items I had to draw them on my own. I like it when they are bit round at the corners and so I used the createRoundedRectRgn function to create the region for every item. I used Region because I can easily add them to the clipping region of the Drawing-Process and I can fill them with a CBrush. Try a pattern-Bitmap for the background - under NT 4.0 it will look great! Unfortunately Win95 can only handle 8x8 sized bitmaps, but there may be someone who is able to make Win95 use larger sized bitmaps or implement a fast function to fill a region with larger tiled-Bitmaps.

Now after I decided to work with Regions I recognize another problem, how to draw the shadows? I solved this problem by creating two regions, one for the outer border, and one for the inner one. The difference between these two Regions is the Border. The light is always coming from the Left-Top and so everything above a diagonal from the Left-Bottom to the Right-Top must be drawn with the 3D-Highlight color and the other side has to be drawn with the 3D-Shadow color.

Now to the drawing process: first of all I draw the selected Item, it is larger than the other ones and its border will overlap its neighbors, if it is drawn later. The remaining item will be drawn line by line from the bottom to the top. By drawing it this way I can always clip out the region for drawn items. This avoids flickering and in case of the selected item it assures that this item looks like placed above the others.

To add more information to every Item you can use the lParam of the TC_ITEM Structure. It is a 32-bit value and so it can hold a pointer to a structure, or what I prefer to a class. I prefer using classes for this kind of parameters because I can check whether they have the correct RUNTIME_CLASS or not.

In my sample I keep the color information and the Title in my additional parameter, but you can expand this information class for whatever you need. You can see the result in the sample project. You can control the colors, the appearance of every item and it IS almost flicker-free!

Last updated: 4 May 1998



Comments

  • Bobs dad says

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: pakistani dude

    I CAN MAKE MY OWN ILLEGAL OPERATION MESSAGES TOO, NEXT TIME TEST THE CODE YOU FAT BASTARD ASS

    Reply
  • LISTEN GENIUS

    Posted by Legacy on 03/02/2002 12:00am

    Originally posted by: BOB

    Please, oh pretty please with lots of sugare on top...next time test your code. i can create Dr. Watson erros on my own, dont need your help

    thank you and best of luck :)

    Reply
  • Example crashes under Win2k

    Posted by Legacy on 04/15/2001 12:00am

    Originally posted by: Paul Barass

    The example EXE crashes under Win2k on my machine (I don't have the SP installed).

    Reply
  • what is this all about?

    Posted by Legacy on 11/11/2000 12:00am

    Originally posted by: bob

    the only thing this code does is bring up Dr. Watson.....is that what it was intended to do? :-)

    Reply
  • GDI32 violation exception

    Posted by Legacy on 07/22/1999 12:00am

    Originally posted by: kyuyeol

    when GetClipRgn() is invoked in OnPaint(),exception occur.
    moreover,C compiler complains that the parameter does not be intitialized.

    your code help me and frustrate me.

    thank for your work.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds