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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds