Главная страница | назад





Article #21780: Creating a Owner Drawn PageControl

Question:
I have a TPageControl on my TForm. I set the TPageControl::OwnerDraw property to true and then draw my tab in the TPageControl::OnDrawTab event, but the control still draws the tab. How do I get it so that I can draw the tab?

Answer:
You are going to have to create a new component to do this. Create a new component derived from TPageControl. Then all you have to do is over-ride the PaintWindow method. The PaintWindow method will give you a HDC that is the HDC for the Tabs. You can then use the Pages and page count property of you new ansestor of TPageControl to get the tabsheets and draw there tabs. The following code will draw a Tab and write the caption of the tab to the ansestor of TPageControl:

void __fastcall KPageControl::PaintWindow(HDC DC)\{
RECT r;
POINT old_p, p;
for (int i = 0; i < PageCount; i++)\{
//get the size of the rect for this tab
if (!TabCtrl_GetItemRect (this->Handle, i, &r))\{
return;
\}
//using the rect draw the tab and caption
::MoveToEx (DC, r.left + 5, r.top, &old_p);
::LineTo (DC, r.left, r.bottom);
::MoveToEx (DC, r.left + 5, r.top, &p);
::LineTo (DC, r.right - 5, r.top);
::MoveToEx (DC, r.right - 5, r.top, &p);
::LineTo (DC, r.right, r.bottom);
if (i != TabCtrl_GetCurSel (this->Handle)) \{
::MoveToEx (DC, r.left, r.bottom, &p);
::LineTo (DC, r.right, r.bottom);
\}
//Set the font
::SelectObject (DC, Pages[i]->Font->Handle);
//set is so that drawtext will draw transparent
int old_mode = ::SetBkMode (DC, TRANSPARENT);
::DrawTextEx (DC, Pages[i]->Caption.c_str (), Pages[i]->Caption.Length (),
&r, DT_CENTER | DT_VCENTER | DT_SINGLELINE, NULL);
::SetBkMode (DC, old_mode);
//move back to the old location
::MoveToEx (DC, old_p.x, old_p.y, &p);
\}
\}

This will draw the tabs, but remember there are lots of other properties of the PageControl that need to be considered (like the TapPosition, Highlighted ...).

Last Modified: 20-APR-00