ThemePageControl.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <vsstyle.h>
  6. #include <memory>
  7. #include "ThemePageControl.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. // Based on
  12. // http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=6355
  13. //---------------------------------------------------------------------------
  14. //#define USE_DEFAULT_XP_TOPTAB // XP top tab is drawn only for test purpose. To use default, uncoment this line
  15. //---------------------------------------------------------------------------
  16. // constant string definitions here (or you can put it into resource string table)
  17. #define IDS_UTIL_TAB L"TAB"
  18. #define IDS_UTIL_UXTHEME L"UxTheme.dll"
  19. #define IDS_UTIL_THEMEACT "IsThemeActive"
  20. #define IDS_UTIL_THEMEOPN "OpenThemeData"
  21. #define IDS_UTIL_THEMEBCKG "DrawThemeBackground"
  22. //---------------------------------------------------------------------------
  23. static inline void ValidCtrCheck(TThemePageControl *)
  24. {
  25. new TThemePageControl(NULL);
  26. }
  27. //---------------------------------------------------------------------------
  28. namespace Themepagecontrol
  29. {
  30. void __fastcall PACKAGE Register()
  31. {
  32. TComponentClass classes[1] = {__classid(TThemePageControl)};
  33. RegisterComponents(L"Scp", classes, 0);
  34. }
  35. }
  36. //----------------------------------------------------------------------------------------------------------
  37. __fastcall TThemeTabSheet::TThemeTabSheet(TComponent * Owner) :
  38. TTabSheet(Owner)
  39. {
  40. FShadowed = false;
  41. }
  42. //----------------------------------------------------------------------------------------------------------
  43. void __fastcall TThemeTabSheet::SetShadowed(bool Value)
  44. {
  45. if (Shadowed != Value)
  46. {
  47. FShadowed = Value;
  48. TThemePageControl * ThemePageControl = dynamic_cast<TThemePageControl *>(Parent);
  49. if (ALWAYS_TRUE(ThemePageControl != NULL))
  50. {
  51. ThemePageControl->InvalidateTab(TabIndex);
  52. }
  53. else
  54. {
  55. Parent->Invalidate();
  56. }
  57. }
  58. }
  59. //----------------------------------------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------------------------------------
  61. __fastcall TThemePageControl::TThemePageControl(TComponent * Owner) :
  62. TPageControl(Owner)
  63. {
  64. FOldTabIndex = -1;
  65. }
  66. //----------------------------------------------------------------------------------------------------------
  67. int __fastcall TThemePageControl::GetTabsHeight()
  68. {
  69. // Calculated height includes tab/contents separator line on Windows 7/8,
  70. // but not on Windows XP
  71. TRect Rect = GetClientRect();
  72. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&Rect);
  73. int Result = Rect.Top - 1;
  74. // two different ways to calculate the same, not sure which one is more reliable,
  75. // so we want to know in case they differ
  76. if (ALWAYS_TRUE(PageCount >= 0))
  77. {
  78. TRect Rect = TabRect(0);
  79. int Result2 = Rect.Bottom + 1;
  80. if (ALWAYS_FALSE(Result != Result2))
  81. {
  82. Result = Result2;
  83. }
  84. }
  85. return Result;
  86. }
  87. //----------------------------------------------------------------------------------------------------------
  88. void __fastcall TThemePageControl::PaintWindow(HDC DC)
  89. {
  90. // Themes not enabled, give up
  91. if (!UseThemes())
  92. {
  93. TPageControl::PaintWindow(DC);
  94. return;
  95. }
  96. // TODO use GetClipBox
  97. TRect PageRect = GetClientRect();
  98. // 1st paint the tab body
  99. TRect ClientRect = PageRect;
  100. ::SendMessage(Handle, TCM_ADJUSTRECT, FALSE, (LPARAM)&PageRect);
  101. ClientRect.Top = PageRect.Top - 2;
  102. DrawThemesXpTabItem(DC, -1, ClientRect, true, 0);
  103. // 2nd paint the inactive tabs
  104. TPoint Point = ScreenToClient(Mouse->CursorPos);
  105. int HotIndex = IndexOfTabAt(Point.X, Point.Y);
  106. int SelectedIndex = TabIndex;
  107. for (int Tab = 0; Tab < PageCount; Tab++)
  108. {
  109. if (Tab != SelectedIndex)
  110. {
  111. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(Pages[Tab]);
  112. bool Shadowed = (ThemeTabSheet != NULL) ? ThemeTabSheet->Shadowed : false;
  113. TRect Rect = TabRect(Tab);
  114. int State = (Tab == HotIndex ? TIS_HOT : (Shadowed ? TIS_DISABLED : TIS_NORMAL));
  115. DrawThemesXpTabItem(DC, Tab, Rect, false, State);
  116. }
  117. }
  118. if (SelectedIndex >= 0)
  119. {
  120. // 3rd paint the active selected tab
  121. TRect Rect = TabRect(SelectedIndex);
  122. Rect.Inflate(2, 2);
  123. Rect.Bottom--;
  124. DrawThemesXpTabItem(DC, SelectedIndex, Rect, false, TIS_SELECTED);
  125. }
  126. }
  127. //----------------------------------------------------------------------------------------------------------
  128. // This function draws Themes Tab control parts: a) Tab-Body and b) Tab-tabs
  129. void __fastcall TThemePageControl::DrawThemesXpTabItem(HDC DC, int Item,
  130. const TRect & Rect, bool Body, int State)
  131. {
  132. TSize Size = Rect.Size;
  133. // Draw background
  134. HDC DCMem = CreateCompatibleDC(DC);
  135. HBITMAP BitmapMem = CreateCompatibleBitmap(DC, Size.Width, Size.Height);
  136. HBITMAP BitmapOld = (HBITMAP)SelectObject(DCMem, BitmapMem);
  137. TRect RectMem(0, 0, Size.Width, Size.Height);
  138. if (!Body && (State == TIS_SELECTED))
  139. {
  140. RectMem.Bottom++;
  141. }
  142. if (Body)
  143. {
  144. DrawThemesPart(DCMem, TABP_PANE, State, IDS_UTIL_TAB, &RectMem);
  145. }
  146. else
  147. {
  148. DrawThemesPart(DCMem, TABP_TABITEM, State, IDS_UTIL_TAB, &RectMem);
  149. }
  150. // Init some extra parameters
  151. BITMAPINFO BitmapInfo;
  152. // Fill local pixel arrays
  153. ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
  154. BITMAPINFOHEADER & BitmapInfoHeader = BitmapInfo.bmiHeader;
  155. BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
  156. BitmapInfoHeader.biCompression = BI_RGB;
  157. BitmapInfoHeader.biPlanes = 1;
  158. // force as RGB: 3 bytes,24 bits -> good for rotating bitmap in any resolution
  159. BitmapInfoHeader.biBitCount = 24;
  160. BitmapInfoHeader.biWidth = Size.Width;
  161. BitmapInfoHeader.biHeight = Size.Height;
  162. if (!Body && (Item >= 0))
  163. {
  164. if ((State == TIS_SELECTED))
  165. {
  166. RectMem.Bottom--;
  167. }
  168. DrawTabItem(DCMem, Item, RectMem, (State == TIS_SELECTED), (State == TIS_DISABLED));
  169. }
  170. // Blit image to the screen
  171. BitBlt(DC, Rect.Left, Rect.Top, Size.Width, Size.Height, DCMem, 0, 0, SRCCOPY);
  172. SelectObject(DCMem, BitmapOld);
  173. DeleteObject(BitmapMem);
  174. DeleteDC(DCMem);
  175. }
  176. //----------------------------------------------------------------------------------------------------------
  177. // draw tab item context: possible icon and text
  178. void __fastcall TThemePageControl::DrawTabItem(HDC DC, int Item, TRect Rect,
  179. bool Selected, bool Shadowed)
  180. {
  181. if (Selected)
  182. {
  183. Rect.Bottom -= 1;
  184. }
  185. else
  186. {
  187. Rect.Bottom += 2;
  188. }
  189. Rect.Left += 6;
  190. Rect.Top += 2 + (Selected ? 1 : 3);
  191. UnicodeString Text = Pages[Item]->Caption;
  192. if ((Images != NULL) && (Pages[Item]->ImageIndex >= 0))
  193. {
  194. int Left;
  195. if (!Text.IsEmpty())
  196. {
  197. Left = Rect.Left + (Selected ? 2 : 0);
  198. }
  199. else
  200. {
  201. Left = (Rect.Right - Images->Width - Rect.Left) / 2;
  202. }
  203. int Y = ((Rect.Top + Rect.Bottom - Images->Height) / 2) - 1 + (Selected ? 0 : -2);
  204. std::unique_ptr<TCanvas> Canvas(new TCanvas());
  205. Canvas->Handle = DC;
  206. Images->Draw(Canvas.get(), Left, Y, Pages[Item]->ImageIndex, !Shadowed);
  207. //ImageList_Draw((HIMAGELIST)Images->Handle, Pages[Item]->ImageIndex, DC,
  208. // Left, Y, ILD_TRANSPARENT, );
  209. Rect.Left += Images->Width + 3;
  210. }
  211. else
  212. {
  213. Rect.Left -= 2;
  214. }
  215. int OldMode = SetBkMode(DC, TRANSPARENT);
  216. if (!Text.IsEmpty())
  217. {
  218. HFONT OldFont = (HFONT)SelectObject(DC, Font->Handle);
  219. Rect.Right -= 3;
  220. wchar_t * Buf = new wchar_t[Text.Length() + 1 + 4];
  221. wcscpy(Buf, Text.c_str());
  222. TRect TextRect(0, 0, Rect.Right - Rect.Left, 20);
  223. ::DrawText(DC, Buf, -1, &TextRect, DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING | DT_END_ELLIPSIS);
  224. OffsetRect(&Rect, 0, (Selected ? 0 : -2));
  225. DrawText(DC, Buf, -1, &Rect, DT_NOPREFIX | DT_CENTER);
  226. delete[] Buf;
  227. SelectObject(DC, OldFont);
  228. }
  229. SetBkMode(DC, OldMode);
  230. }
  231. //----------------------------------------------------------------------------------------------------------
  232. void __fastcall TThemePageControl::DrawThemesPart(HDC DC, int PartId,
  233. int StateId, LPCWSTR PartNameID, LPRECT Rect)
  234. {
  235. HTHEME Theme = OpenThemeData(NULL, PartNameID);
  236. if (Theme != 0)
  237. {
  238. DrawThemeBackground(Theme, DC, PartId, StateId, Rect, NULL);
  239. CloseThemeData(Theme);
  240. }
  241. }
  242. //==========================================================================================================
  243. // these two messages are necessary only to properly redraw deselected tab background, because
  244. bool __fastcall TThemePageControl::CanChange()
  245. {
  246. FOldTabIndex = ActivePageIndex;
  247. return TPageControl::CanChange();
  248. }
  249. //----------------------------------------------------------------------------------------------------------
  250. void __fastcall TThemePageControl::InvalidateTab(int Index)
  251. {
  252. if (HandleAllocated())
  253. {
  254. TRect Rect = TabRect(Index);
  255. if (Index == TabIndex)
  256. {
  257. Rect.Inflate(2, 2);
  258. }
  259. // Original code was invalidating range against parent window
  260. // (recalculating coordinates first)
  261. InvalidateRect(Handle, &Rect, true);
  262. }
  263. }
  264. //----------------------------------------------------------------------------------------------------------
  265. void __fastcall TThemePageControl::Change()
  266. {
  267. // note that TabIndex yields correct value already here,
  268. // while ActivePageIndex is not updated yet
  269. if ((FOldTabIndex >= 0) && (FOldTabIndex != TabIndex) && UseThemes())
  270. {
  271. InvalidateTab(FOldTabIndex);
  272. }
  273. TPageControl::Change();
  274. }
  275. //----------------------------------------------------------------------------------------------------------