Solved

Coloring a CTabCtrl problem

Posted on 2003-11-09
18
2,360 Views
Last Modified: 2013-11-20
Hi

I have canged the color of my dialog (WM_CTLCOLOR) and the color of the actual tabs (WM_ITEMDRAW), but the problem is the body of the selected tab and also a small area to the right of the tabs is not colored, so I derived a class from a tab ctrl and handled the WM_PAINT, but the problem is the OnPaint, paints over the tabs, can someone tell me what I'm doing wrong

Steven
0
Comment
Question by:Dj_Fx8
  • 8
  • 5
  • 2
  • +2
18 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 9711472
You might want to avoid the tab rect in the onpaint.

CTabCtrl::GetItemRect  will give you the rectangle of a tab item, you could find the extends of the tab rectangle using this and the window rect of the tab control.
0
 
LVL 5

Expert Comment

by:waelothman
ID: 9711659
be sure that you return brush containg color you want in the function WM_CTLCOLOR like
BOOL CTestDlg::OnInitDialog()
{
 m_redbrush.CreateSolidBrush(RGB(255,0,0));    
}
Finally do this on the ID_CTLCOLOR handle:

HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr;
switch (nCtlColor)
  {
  // process my edit controls by ID.
  case CTLCOLOR_EDIT:

  switch (pWnd->GetDlgCtrlID())
    {    
    case IDC_CONTROLE :         // the id of your controle
     pDC->SetBkColor(RGB(255,0,0));  
      pDC->SetTextColor(RGB(0,254,0));  // change the text color
      hbr = (HBRUSH)  m_redbrush;
      break;  
   default:
      hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
      break;
    }  
    break;
 default:
    hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
  }

  return hbr; }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9712778
In your customised class (based on CTabCtrl) make a virtual function DrawItem and do your painting of the tab part including the text there.
0
 

Author Comment

by:Dj_Fx8
ID: 9717850
Hi

Andy I feel your reply is the right way to go, however I had add a msg handler through the class wizzard for WM_DRAWITEM but this never gets called, I noticed you said "make a virtual function DrawItem" and my OnDrawItem wasn't virtual so I changed it to
virtual void DrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct); but again this never gets called??

Steven
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 9717891

 have you tried In the OnPaint call dont call the base class OnPaint
0
 
LVL 5

Expert Comment

by:waelothman
ID: 9717940
i think you don't have to inhirit from the main class just use function
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
0
 

Author Comment

by:Dj_Fx8
ID: 9718795
Hi
Forgive me if I'm missing something here but
William I take it you mean to draw the tabs in the on paint, if so how do I get the DC to draw them on

Waelothman OnCtrlColor does not work for a tab ctrl :-( if only so simple
0
 

Author Comment

by:Dj_Fx8
ID: 9718993
Hi wiliam
I didn't quite say that right, got confused, if I draw the tabs in the on paint I have to handle everything, tab edge, font etc where as in the OnDrawItem I just set the colors

0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 9719147

  I think you will have to draw it yourself, is this a problem for you?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9720831
OK - maybe you do not have the tab set as owner draw.  Something like the following should help to make it owner draw and it should then get the DrawItem message.

(I used this in making the active tab text appear with a larger font and different colour - it does work, and I think it is what you are trying to accomplish)

bool COwnerDrawTab::SubclassDlgItem(UINT nID, CWnd* pParent)
{
      if (!CTabCtrl::SubclassDlgItem(nID, pParent))
            return false;

      ModifyStyle(0, TCS_OWNERDRAWFIXED);
0
 

Author Comment

by:Dj_Fx8
ID: 9724718
Hi

Andy
I had it set as owner drawen, I got that prob sorted, I had a handler for draw item in the dialog, but even when it did get called the tabs still wern't drawen, so it's looking like I'm going to have to handle drawing the tabs myself.

William
>>>I think you will have to draw it yourself, is this a problem for you?
:-) more of a bother, although I'll run this by you to see if my thoughts are along the right lines. I can draw each tab by looping of the num of tabs and use GetIitemRect, and GetItem to provide my with the required info, more of a prob is drawing the 3d edge of each tab, If I use DrawEdge or RoundedRect I'm going to get a bottom edge on the tab, so I assume to prevent this I have to use regions to prevent this bottom edge.
Am I on the right wave length, if so I need to do I bit of sudying to figure this all out

Steven
0
 

Author Comment

by:Dj_Fx8
ID: 9724820
Andy
I can get the tabs drawen in different colors and fonts easly enough, the prob is when I use OnPaint to color the background of the tab the tabs dissapear
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9724861
Don't use the OnPaint to paint the tabs if you are doing an owner draw. In your customised OnPaint put as the first line a return and see if the OwnerDraw approach works for you.  If it does forget customising the OnPaint event.
(The OnPaint default behaviour should be calling the DrawItem)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9724961
I have a web site www.ainscow.ch  Have a look at the example 3 on that.  That has some screen shots of how this customised tab control looks like.
0
 

Author Comment

by:Dj_Fx8
ID: 9725433
>>>>Don't use the OnPaint to paint the tabs
I'm trying to use the OnPaint to color the page part (under the tab(s))

I'll have a look at your site now
0
 

Author Comment

by:Dj_Fx8
ID: 9725567
Hi I had a look at your example, I can achive a similar tabctrl, but I want mine to
1. have colored tabs (this I can do in DrawItem)
2. color page part (was trying to do in OnPaint)
3. color the area to the right of the tabs
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 9730184
I think I understand now.  You want the whole width of the page up to the top of the tabs drawn.

The following doesn't work quite correctly - is this sort of what you require?

trap WM_ERASEBACKGROUND message for the tab, no OnPaint handler
BOOL COwnerDrawTab::OnEraseBkgnd(CDC* pDC)
{
       CRect rect;
       GetWindowRect(&rect);
      GetParent()->ScreenToClient(&rect);
       pDC->FillSolidRect(&rect, RGB(255, 0, 0));
      return true;      
}
0
 

Author Comment

by:Dj_Fx8
ID: 9734947
I think thats the way I need to go, so it looks like there is no easy way of doing it and through all your answers I've got pointed in the right direction, so I'll close this question soon as I feel it has run its course, and split the points based on effort and directional content. I'm sure I'll have a few more question very soon

Thanks to all for you help

Steven
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now