Label.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. // Label.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Label.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. BEGIN_MESSAGE_MAP(CLabel, CStatic)
  11. //{{AFX_MSG_MAP(CLabel)
  12. ON_WM_TIMER()
  13. ON_WM_LBUTTONDOWN()
  14. ON_WM_SETCURSOR()
  15. ON_WM_SYSCOLORCHANGE()
  16. ON_WM_PAINT()
  17. ON_WM_ERASEBKGND()
  18. //}}AFX_MSG_MAP
  19. ON_WM_CTLCOLOR_REFLECT()
  20. END_MESSAGE_MAP()
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CLabel Version 1.2
  23. //
  24. // From now on I'll try to keep a log of fixes and enhancements...
  25. //
  26. // The new feature were added due to the response of people.
  27. // All I ask is to all you programmers out there, is if you add, fix or
  28. // enhance this code, sent me a copy and I'll send the copy on to www.codeproject.com
  29. //
  30. // Happy Software Engineer :)
  31. //
  32. // New features include:
  33. //
  34. // A. Support for 3D Fonts
  35. // B. Support for background transparency
  36. // C. More comments provided
  37. // D. If alignment is 'centered' and the window text is seperated by '\r\n'
  38. // the will be centered accordingly - requested by someone @ nasa ;)
  39. // E. Support for font rotation.
  40. // F. Respond to System Color Change
  41. // G. OnPaint improved performance - using Double Buffering Technique
  42. //
  43. // Thanks to:
  44. // Mark McDowell - For suggestion on 'Increasing the flexibility of "hypertext" setting...'
  45. // Erich Ruth - For suggestion on 'Font Rotation'
  46. //
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CLabel Version 1.3
  49. //
  50. // A. Added SS_LEFTNOWORDWRAP to include wordwrap
  51. // B. Fix repainting problem
  52. // C. Fix SetBkColor
  53. // D. Added SS_CENTER
  54. // Thanks to:
  55. // Marius - Added styling problem.
  56. // Azing Vondeling & Broker - Spotting painting Problem.
  57. // Mel Stober - Back Color & SS_CENTER
  58. //
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CLabel Version 1.4
  61. //
  62. // A. Fix to transparency mode
  63. // B. Added new SetText3DHiliteColor to change the 3D Font face color - default is white.
  64. //
  65. // Thanks to:
  66. // michael.groeger - Spotting Transparency with other controls bug.
  67. //
  68. //
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CLabel Version 1.5
  71. //
  72. // A. Sanity handle check
  73. // B. Support Interface Charset
  74. // C. Check compilition with _UNICODE
  75. // D. Fix hyperlink feature
  76. // E. Support default Dialog Font
  77. // F. Inclusion of SS_OWNERDRAW via control creation and subclassing
  78. // G. Modification to Text aligmnent code
  79. // H. New background gradient fill function
  80. //
  81. // Thanks to:
  82. // Steve Kowald - Using null handles
  83. // Alan Chan - Supporting International Windows
  84. // Dieter Fauth - Request for default Dialog font
  85. // Herb Illfelder - Text Alignment code
  86. //
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CLabel Version 1.6
  89. // Jeroen Roosendaal - SetFont suggestion
  90. // Laurent - Spotting SelectObject bugs
  91. // Bernie - Fix PreCreateWindow bug
  92. // Jignesh I. Patel - Added expanded tabs feature
  93. // Jim Farmelant - Fix SetText crash
  94. //////////////////////////////////////////////////////////////////////////
  95. //
  96. // Function: CLabel::CLabel
  97. //
  98. // Description: Default contructor
  99. //
  100. // INPUTS:
  101. //
  102. // RETURNS:
  103. //
  104. // NOTES:
  105. //
  106. // MODIFICATIONS:
  107. //
  108. // Name Date Version Comments
  109. // NT ALMOND 26/08/98 1.0 Origin
  110. //
  111. //////////////////////////////////////////////////////////////////////////
  112. CLabel::CLabel()
  113. {
  114. m_crText = GetSysColor(COLOR_WINDOWTEXT);
  115. // 1.1
  116. m_hBackBrush = NULL;
  117. m_crHiColor = 0;
  118. m_crLoColor = 0;
  119. m_bTimer = FALSE;
  120. m_bState = FALSE;
  121. m_bTransparent = FALSE;
  122. m_Link = LinkNone;
  123. m_hCursor = NULL;
  124. m_Type = None;
  125. m_bFont3d = FALSE;
  126. m_bNotifyParent = FALSE;
  127. m_bToolTips = FALSE;
  128. m_bRotation = FALSE;
  129. m_fillmode = Normal;
  130. m_cr3DHiliteColor = RGB(255,255,255);
  131. m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  132. }
  133. //////////////////////////////////////////////////////////////////////////
  134. //
  135. // Function: CLabel::~CLabel
  136. //
  137. // Description:
  138. //
  139. // INPUTS:
  140. //
  141. // RETURNS:
  142. //
  143. // NOTES:
  144. //
  145. // MODIFICATIONS:
  146. //
  147. // Name Date Version Comments
  148. // NT ALMOND 26/08/98 1.0 Origin
  149. // NT ALMOND 15092000 1.5 Handle Check
  150. //////////////////////////////////////////////////////////////////////////
  151. CLabel::~CLabel()
  152. {
  153. // Clean up
  154. m_font.DeleteObject();
  155. ::DeleteObject(m_hwndBrush);
  156. // Stop Checking complaining
  157. if (m_hBackBrush)
  158. ::DeleteObject(m_hBackBrush);
  159. }
  160. void CLabel::UpdateSurface()
  161. {
  162. CRect (rc);
  163. GetWindowRect(rc);
  164. RedrawWindow();
  165. GetParent()->ScreenToClient(rc);
  166. GetParent()->InvalidateRect(rc,TRUE);
  167. GetParent()->UpdateWindow();
  168. }
  169. //////////////////////////////////////////////////////////////////////////
  170. //
  171. // Function: CLabel::ReconstructFont
  172. //
  173. // Description: Helper function to build font after it was changed
  174. //
  175. // INPUTS:
  176. //
  177. // RETURNS:
  178. //
  179. // NOTES: PROTECTED
  180. //
  181. // MODIFICATIONS:
  182. //
  183. // Name Date Version Comments
  184. // NT ALMOND 26/08/98 1.0 Origin
  185. //
  186. //////////////////////////////////////////////////////////////////////////
  187. void CLabel::ReconstructFont()
  188. {
  189. m_font.DeleteObject();
  190. BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
  191. ASSERT(bCreated);
  192. }
  193. //////////////////////////////////////////////////////////////////////////
  194. //
  195. // Function: CLabel::OnPaint
  196. //
  197. // Description: Handles all the drawing code for the label
  198. //
  199. // INPUTS:
  200. //
  201. // RETURNS:
  202. //
  203. // NOTES: Called by Windows... not by USER
  204. // Probably needs tiding up a some point.
  205. // Different states will require this code to be reworked.
  206. //
  207. //
  208. // MODIFICATIONS:
  209. //
  210. // Name Date Version Comments
  211. // NT ALMOND 22/10/98 1.0 Origin
  212. // NT ALMOND 15092000 1.5 Handle Check
  213. // NT ALMOND 15092000 1.5 Alignment mods
  214. // NT ALMOND 15092000 1.5 Gradient Fill Mode
  215. // NT ALMOND 02072002 1.6 Fill SelectObject bugs
  216. // NT ALMOND 02072002 1.6 Added to expand tabs
  217. //////////////////////////////////////////////////////////////////////////
  218. void CLabel::OnPaint()
  219. {
  220. CPaintDC dc(this); // device context for painting
  221. DWORD dwFlags = 0;
  222. CRect rc;
  223. GetClientRect(rc);
  224. CString strText;
  225. GetWindowText(strText);
  226. CBitmap bmp;
  227. ///////////////////////////////////////////////////////
  228. //
  229. // Set up for double buffering...
  230. //
  231. CDC* pDCMem;
  232. CBitmap* pOldBitmap = NULL;
  233. if (!m_bTransparent)
  234. {
  235. pDCMem = new CDC;
  236. pDCMem->CreateCompatibleDC(&dc);
  237. bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
  238. pOldBitmap = pDCMem->SelectObject(&bmp);
  239. }
  240. else
  241. {
  242. pDCMem = &dc;
  243. }
  244. UINT nMode = pDCMem->SetBkMode(TRANSPARENT);
  245. COLORREF crText = pDCMem->SetTextColor(m_crText);
  246. CFont *pOldFont = pDCMem->SelectObject(&m_font);
  247. // Fill in backgound if not transparent
  248. if (!m_bTransparent)
  249. {
  250. if (m_fillmode == Normal)
  251. {
  252. CBrush br;
  253. if (m_hBackBrush != NULL)
  254. br.Attach(m_hBackBrush);
  255. else
  256. br.Attach(m_hwndBrush);
  257. pDCMem->FillRect(rc,&br);
  258. br.Detach();
  259. }
  260. else // Gradient Fill
  261. {
  262. DrawGradientFill(pDCMem, &rc, m_crLoColor, m_crHiColor, 100);
  263. }
  264. }
  265. // If the text is flashing turn the text color on
  266. // then to the color of the window background.
  267. LOGBRUSH lb;
  268. ZeroMemory(&lb,sizeof(lb));
  269. // Stop Checking complaining
  270. if (m_hBackBrush)
  271. ::GetObject(m_hBackBrush,sizeof(lb),&lb);
  272. // Something to do with flashing
  273. if (!m_bState && m_Type == Text)
  274. pDCMem->SetTextColor(lb.lbColor);
  275. DWORD style = GetStyle();
  276. switch (style & SS_TYPEMASK)
  277. {
  278. case SS_RIGHT:
  279. dwFlags = DT_RIGHT | DT_WORDBREAK;
  280. break;
  281. case SS_CENTER:
  282. dwFlags = SS_CENTER | DT_WORDBREAK;
  283. break;
  284. case SS_LEFTNOWORDWRAP:
  285. dwFlags = DT_LEFT;
  286. break;
  287. default: // treat other types as left
  288. case SS_LEFT:
  289. dwFlags = DT_LEFT | DT_WORDBREAK;
  290. break;
  291. }
  292. // Added to expand tabs...
  293. if(strText.Find(_T('\t')) != -1)
  294. dwFlags |= DT_EXPANDTABS;
  295. // If the text centered make an assumtion that
  296. // the will want to center verticly as well
  297. if (style & SS_CENTERIMAGE)
  298. {
  299. dwFlags = DT_CENTER;
  300. // Apply
  301. if (strText.Find(_T("\r\n")) == -1)
  302. {
  303. dwFlags |= DT_VCENTER;
  304. // And because DT_VCENTER only works with single lines
  305. dwFlags |= DT_SINGLELINE;
  306. }
  307. }
  308. //
  309. // 3333 DDDDD
  310. // 3 D D
  311. // 33 D D E F X
  312. // 3 D D
  313. // 3333 DDDDD
  314. //
  315. //
  316. if (m_bRotation)
  317. {
  318. int nAlign = pDCMem->SetTextAlign (TA_BASELINE);
  319. CPoint pt;
  320. GetViewportOrgEx (pDCMem->m_hDC,&pt) ;
  321. SetViewportOrgEx (pDCMem->m_hDC,rc.Width() / 2, rc.Height() / 2, NULL) ;
  322. pDCMem->TextOut (0, 0, strText) ;
  323. SetViewportOrgEx (pDCMem->m_hDC,pt.x / 2, pt.y / 2, NULL) ;
  324. pDCMem->SetTextAlign (nAlign);
  325. }
  326. else
  327. {
  328. pDCMem->DrawText(strText,rc,dwFlags);
  329. if (m_bFont3d)
  330. {
  331. pDCMem->SetTextColor(m_cr3DHiliteColor);
  332. if (m_3dType == Raised)
  333. rc.OffsetRect(-1,-1);
  334. else
  335. rc.OffsetRect(1,1);
  336. pDCMem->DrawText(strText,rc,dwFlags);
  337. m_3dType;
  338. }
  339. }
  340. // Restore DC's State
  341. pDCMem->SetBkMode(nMode);
  342. pDCMem->SelectObject(pOldFont);
  343. pDCMem->SetTextColor(crText);
  344. if (!m_bTransparent)
  345. {
  346. dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCCOPY);
  347. // continue DC restore
  348. pDCMem->SelectObject ( pOldBitmap ) ;
  349. delete pDCMem;
  350. }
  351. }
  352. //////////////////////////////////////////////////////////////////////////
  353. //
  354. // Function: CLabel::OnTimer
  355. //
  356. // Description: Used in conjunction with 'FLASH' functions
  357. //
  358. // INPUTS: Windows API
  359. //
  360. // RETURNS: Windows API
  361. //
  362. // NOTES:
  363. //
  364. // MODIFICATIONS:
  365. //
  366. // Name Date Version Comments
  367. // NT ALMOND 26/08/98 1.0 Origin
  368. //
  369. //////////////////////////////////////////////////////////////////////////
  370. void CLabel::OnTimer(UINT nIDEvent)
  371. {
  372. m_bState = !m_bState;
  373. UpdateSurface();
  374. CStatic::OnTimer(nIDEvent);
  375. }
  376. //////////////////////////////////////////////////////////////////////////
  377. //
  378. // Function: CLabel::OnSetCursor
  379. //
  380. // Description: Used in conjunction with 'LINK' function
  381. //
  382. // INPUTS: Windows API
  383. //
  384. // RETURNS: Windows API
  385. //
  386. // NOTES:
  387. //
  388. // MODIFICATIONS:
  389. //
  390. // Name Date Version Comments
  391. // NT ALMOND 26/08/98 1.0 Origin
  392. //
  393. //////////////////////////////////////////////////////////////////////////
  394. BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  395. {
  396. if (m_hCursor)
  397. {
  398. ::SetCursor(m_hCursor);
  399. return TRUE;
  400. }
  401. return CStatic::OnSetCursor(pWnd, nHitTest, message);
  402. }
  403. //////////////////////////////////////////////////////////////////////////
  404. //
  405. // Function: CLabel::OnLButtonDown
  406. //
  407. // Description: Called when a link is click on
  408. //
  409. // INPUTS: Windows API
  410. //
  411. // RETURNS: Windows API
  412. //
  413. // NOTES:
  414. //
  415. // MODIFICATIONS:
  416. //
  417. // Name Date Version Comments
  418. // NT ALMOND 26/08/98 1.0 Origin
  419. // NT ALMOND 02072002 1.6 Added Mail support
  420. //////////////////////////////////////////////////////////////////////////
  421. void CLabel::OnLButtonDown(UINT nFlags, CPoint point)
  422. {
  423. if (!m_bNotifyParent) // Fix
  424. {
  425. CString strLink;
  426. GetWindowText(strLink);
  427. if (m_Link == HyperLink)
  428. {
  429. ShellExecute(NULL,_T("open"),m_sLink.IsEmpty() ? strLink : m_sLink,NULL,NULL,SW_SHOWNORMAL);
  430. }
  431. if (m_Link == MailLink)
  432. {
  433. strLink = _T("mailto:") + strLink;
  434. ShellExecute( NULL, NULL, strLink, NULL, NULL, SW_SHOWNORMAL );
  435. }
  436. }
  437. else
  438. {
  439. // To use notification in parent window
  440. // Respond to a OnNotify in parent and disassemble the message
  441. //
  442. NMHDR nm;
  443. nm.hwndFrom = GetSafeHwnd();
  444. nm.idFrom = GetDlgCtrlID();
  445. nm.code = NM_LINKCLICK;
  446. GetParent()->SendMessage(WM_NOTIFY,nm.idFrom,(LPARAM) &nm);
  447. }
  448. CStatic::OnLButtonDown(nFlags, point);
  449. }
  450. //////////////////////////////////////////////////////////////////////////
  451. //////////////////////////////////////////////////////////////////////////
  452. // THE FUNCTIONS START HERE :----
  453. //////////////////////////////////////////////////////////////////////////
  454. //////////////////////////////////////////////////////////////////////////
  455. //////////////////////////////////////////////////////////////////////////
  456. //
  457. // Function: CLabel::SetText
  458. //
  459. // Description: Short cut to set window text - caption - label
  460. //
  461. // INPUTS: Text to use
  462. //
  463. // RETURNS: Reference to this
  464. //
  465. // NOTES:
  466. //
  467. // MODIFICATIONS:
  468. //
  469. // Name Date Version Comments
  470. // NT ALMOND 26081998 1.0 Origin
  471. // NT ALMOND 02072002 1.6 Crash Fix
  472. //////////////////////////////////////////////////////////////////////////
  473. CLabel& CLabel::SetText(const CString& strText)
  474. {
  475. if(IsWindow(this->GetSafeHwnd()))
  476. {
  477. SetWindowText(strText);
  478. UpdateSurface();
  479. }
  480. return *this;
  481. }
  482. //////////////////////////////////////////////////////////////////////////
  483. //
  484. // Function: CLabel::SetTextColor
  485. //
  486. // Description: Sets the text color
  487. //
  488. // INPUTS: True or false
  489. //
  490. // RETURNS: Reference to 'this' object
  491. //
  492. // NOTES:
  493. //
  494. // MODIFICATIONS:
  495. //
  496. // Name Date Version Comments
  497. // NT ALMOND 22/10/98 1.0 Origin
  498. //
  499. //////////////////////////////////////////////////////////////////////////
  500. CLabel& CLabel::SetTextColor(COLORREF crText)
  501. {
  502. m_crText = crText;
  503. UpdateSurface();
  504. return *this;
  505. }
  506. //////////////////////////////////////////////////////////////////////////
  507. //
  508. // Function: CLabel::SetFontBold
  509. //
  510. // Description: Sets the font ot bold
  511. //
  512. // INPUTS: True or false
  513. //
  514. // RETURNS: Reference to 'this' object
  515. //
  516. // NOTES:
  517. //
  518. // MODIFICATIONS:
  519. //
  520. // Name Date Version Comments
  521. // NT ALMOND 22/10/98 1.0 Origin
  522. //
  523. //////////////////////////////////////////////////////////////////////////
  524. CLabel& CLabel::SetFontBold(BOOL bBold)
  525. {
  526. m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
  527. ReconstructFont();
  528. UpdateSurface();
  529. return *this;
  530. }
  531. //////////////////////////////////////////////////////////////////////////
  532. //
  533. // Function: CLabel::SetFontUnderline
  534. //
  535. // Description: Sets font underline attribue
  536. //
  537. // INPUTS: True of false
  538. //
  539. // RETURNS: Reference to 'this' object
  540. //
  541. // NOTES:
  542. //
  543. // MODIFICATIONS:
  544. //
  545. // Name Date Version Comments
  546. // NT ALMOND 26/08/98 1.0 Origin
  547. //
  548. //////////////////////////////////////////////////////////////////////////
  549. CLabel& CLabel::SetFontUnderline(BOOL bSet)
  550. {
  551. m_lf.lfUnderline = bSet;
  552. ReconstructFont();
  553. UpdateSurface();
  554. return *this;
  555. }
  556. //////////////////////////////////////////////////////////////////////////
  557. //
  558. // Function: CLabel::SetFontItalic
  559. //
  560. // Description: Sets font italic attribute
  561. //
  562. // INPUTS: True of false
  563. //
  564. // RETURNS: Reference to 'this' object
  565. //
  566. // NOTES:
  567. //
  568. // MODIFICATIONS:
  569. //
  570. // Name Date Version Comments
  571. // NT ALMOND 26/08/98 1.0 Origin
  572. //
  573. //////////////////////////////////////////////////////////////////////////
  574. CLabel& CLabel::SetFontItalic(BOOL bSet)
  575. {
  576. m_lf.lfItalic = bSet;
  577. ReconstructFont();
  578. UpdateSurface();
  579. return *this;
  580. }
  581. //////////////////////////////////////////////////////////////////////////
  582. //
  583. // Function: CLabel::SetSunken
  584. //
  585. // Description: Sets sunken effect on border
  586. //
  587. // INPUTS: True of false
  588. //
  589. // RETURNS: Reference to 'this' object
  590. //
  591. // NOTES:
  592. //
  593. // MODIFICATIONS:
  594. //
  595. // Name Date Version Comments
  596. // NT ALMOND 26/08/98 1.0 Origin
  597. //
  598. //////////////////////////////////////////////////////////////////////////
  599. CLabel& CLabel::SetSunken(BOOL bSet)
  600. {
  601. if (!bSet)
  602. ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
  603. else
  604. ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
  605. return *this;
  606. }
  607. //////////////////////////////////////////////////////////////////////////
  608. //
  609. // Function: CLabel::SetBorder
  610. //
  611. // Description: Toggles the border on/off
  612. //
  613. // INPUTS: True of false
  614. //
  615. // RETURNS: Reference to 'this' object
  616. //
  617. // NOTES:
  618. //
  619. // MODIFICATIONS:
  620. //
  621. // Name Date Version Comments
  622. // NT ALMOND 26/08/98 1.0 Origin
  623. //
  624. //////////////////////////////////////////////////////////////////////////
  625. CLabel& CLabel::SetBorder(BOOL bSet)
  626. {
  627. if (!bSet)
  628. ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
  629. else
  630. ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
  631. return *this;
  632. }
  633. //////////////////////////////////////////////////////////////////////////
  634. //
  635. // Function: CLabel::SetFontSize
  636. //
  637. // Description: Sets the font size
  638. //
  639. // INPUTS: True of false
  640. //
  641. // RETURNS: Reference to 'this' object
  642. //
  643. // NOTES:
  644. //
  645. // MODIFICATIONS:
  646. //
  647. // Name Date Version Comments
  648. // NT ALMOND 26/08/98 1.0 Origin
  649. //
  650. //////////////////////////////////////////////////////////////////////////
  651. CLabel& CLabel::SetFontSize(int nSize)
  652. {
  653. CFont cf;
  654. LOGFONT lf;
  655. cf.CreatePointFont(nSize * 10, m_lf.lfFaceName);
  656. cf.GetLogFont(&lf);
  657. m_lf.lfHeight = lf.lfHeight;
  658. m_lf.lfWidth = lf.lfWidth;
  659. // nSize*=-1;
  660. // m_lf.lfHeight = nSize;
  661. ReconstructFont();
  662. UpdateSurface();
  663. return *this;
  664. }
  665. //////////////////////////////////////////////////////////////////////////
  666. //
  667. // Function: CLabel::SetBkColor
  668. //
  669. // Description: Sets background color
  670. //
  671. // INPUTS: Colorref of background color
  672. //
  673. // RETURNS: Reference to 'this' object
  674. //
  675. // NOTES:
  676. //
  677. // MODIFICATIONS:
  678. //
  679. // Name Date Version Comments
  680. // NT ALMOND 26/08/98 1.0 Origin
  681. //
  682. //////////////////////////////////////////////////////////////////////////
  683. CLabel& CLabel::SetBkColor(COLORREF crBkgnd, COLORREF crBkgndHigh , BackFillMode mode)
  684. {
  685. m_crLoColor = crBkgnd;
  686. m_crHiColor = crBkgndHigh;
  687. m_fillmode = mode;
  688. if (m_hBackBrush)
  689. ::DeleteObject(m_hBackBrush);
  690. if (m_fillmode == Normal)
  691. m_hBackBrush = ::CreateSolidBrush(crBkgnd);
  692. UpdateSurface();
  693. return *this;
  694. }
  695. //////////////////////////////////////////////////////////////////////////
  696. //
  697. // Function: CLabel::SetFontName
  698. //
  699. // Description: Sets the fonts face name
  700. //
  701. // INPUTS: String containing font name
  702. //
  703. // RETURNS: Reference to 'this' object
  704. //
  705. // NOTES:
  706. //
  707. // MODIFICATIONS:
  708. //
  709. // Name Date Version Comments
  710. // NT ALMOND 26/08/98 1.0 Origin
  711. // NT ALMOND 15092000 1.5 Support internation windows
  712. //////////////////////////////////////////////////////////////////////////
  713. CLabel& CLabel::SetFontName(const CString& strFont, BYTE byCharSet /* Default = ANSI_CHARSET */)
  714. {
  715. m_lf.lfCharSet = byCharSet;
  716. _tcscpy(m_lf.lfFaceName,strFont);
  717. ReconstructFont();
  718. UpdateSurface();
  719. return *this;
  720. }
  721. //////////////////////////////////////////////////////////////////////////
  722. //
  723. // Function: CLabel::FlashText
  724. //
  725. // Description: As the function states
  726. //
  727. // INPUTS: True or false
  728. //
  729. // RETURNS: Reference to 'this' object
  730. //
  731. // NOTES:
  732. //
  733. // MODIFICATIONS:
  734. //
  735. // Name Date Version Comments
  736. // NT ALMOND 26/08/98 1.0 Origin
  737. //
  738. //////////////////////////////////////////////////////////////////////////
  739. CLabel& CLabel::FlashText(BOOL bActivate)
  740. {
  741. if (m_bTimer)
  742. KillTimer(1);
  743. if (bActivate)
  744. {
  745. m_bState = FALSE;
  746. m_bTimer = TRUE;
  747. SetTimer(1,500,NULL);
  748. m_Type = Text;
  749. }
  750. else
  751. m_Type = None; // Fix
  752. return *this;
  753. }
  754. //////////////////////////////////////////////////////////////////////////
  755. //
  756. // Function: CLabel::FlashBackground
  757. //
  758. // Description: As the function states
  759. //
  760. // INPUTS: True or false
  761. //
  762. // RETURNS: Reference to 'this' object
  763. //
  764. // NOTES:
  765. //
  766. // MODIFICATIONS:
  767. //
  768. // Name Date Version Comments
  769. // NT ALMOND 26/08/98 1.0 Origin
  770. //
  771. //////////////////////////////////////////////////////////////////////////
  772. CLabel& CLabel::FlashBackground(BOOL bActivate)
  773. {
  774. if (m_bTimer)
  775. KillTimer(1);
  776. if (bActivate)
  777. {
  778. m_bState = FALSE;
  779. m_bTimer = TRUE;
  780. SetTimer(1,500,NULL);
  781. m_Type = Background;
  782. }
  783. return *this;
  784. }
  785. //////////////////////////////////////////////////////////////////////////
  786. //
  787. // Function: CLabel::SetLink
  788. //
  789. // Description: Indicates the string is a link
  790. //
  791. // INPUTS: True or false
  792. //
  793. // RETURNS: Reference to 'this' object
  794. //
  795. // NOTES:
  796. //
  797. // MODIFICATIONS:
  798. //
  799. // Name Date Version Comments
  800. // NT ALMOND 26/08/98 1.0 Origin
  801. // NT ALMOND 26/08/99 1.2 Added flexbility of
  802. // Sending Click meessage to parent
  803. //
  804. //////////////////////////////////////////////////////////////////////////
  805. CLabel& CLabel::SetLink(BOOL bLink,BOOL bNotifyParent)
  806. {
  807. if (bLink)
  808. m_Link = HyperLink;
  809. else
  810. m_Link = LinkNone;
  811. m_bNotifyParent = bNotifyParent;
  812. if (m_Link != LinkNone)
  813. ModifyStyle(0,SS_NOTIFY);
  814. else
  815. ModifyStyle(SS_NOTIFY,0);
  816. return *this;
  817. }
  818. //////////////////////////////////////////////////////////////////////////
  819. //
  820. // Function: CLabel::SetLinkCursor
  821. //
  822. // Description: Sets the internet browers link
  823. //
  824. // INPUTS: Handle of cursor
  825. //
  826. // RETURNS: Reference to 'this' object
  827. //
  828. // NOTES:
  829. //
  830. // MODIFICATIONS:
  831. //
  832. // Name Date Version Comments
  833. // NT ALMOND 26/08/98 1.0 Origin
  834. //
  835. //////////////////////////////////////////////////////////////////////////
  836. CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
  837. {
  838. m_hCursor = hCursor;
  839. return *this;
  840. }
  841. //////////////////////////////////////////////////////////////////////////
  842. //
  843. // Function: CLabel::SetTransparent
  844. //
  845. // Description: Sets the Label window to be transpaent
  846. //
  847. // INPUTS: True or false
  848. //
  849. // RETURNS: Reference to 'this' object
  850. //
  851. // NOTES:
  852. //
  853. // MODIFICATIONS:
  854. //
  855. // Name Date Version Comments
  856. // NT ALMOND 22/10/98 1.0 Origin
  857. //
  858. //////////////////////////////////////////////////////////////////////////
  859. CLabel& CLabel::SetTransparent(BOOL bSet)
  860. {
  861. m_bTransparent = bSet;
  862. ModifyStyleEx(0,WS_EX_TRANSPARENT); // Fix for transparency
  863. UpdateSurface();
  864. return *this;
  865. }
  866. //////////////////////////////////////////////////////////////////////////
  867. //
  868. // Function: CLabel::SetFont3D
  869. //
  870. // Description: Sets the 3D attribute of the font.
  871. //
  872. // INPUTS: True or false, Raised or Sunken
  873. //
  874. // RETURNS: Reference to 'this' object
  875. //
  876. // NOTES:
  877. //
  878. // MODIFICATIONS:
  879. //
  880. // Name Date Version Comments
  881. // NT ALMOND 22/10/98 1.0 Origin
  882. //
  883. //////////////////////////////////////////////////////////////////////////
  884. CLabel& CLabel::SetFont3D(BOOL bSet,Type3D type)
  885. {
  886. m_bFont3d = bSet;
  887. m_3dType = type;
  888. UpdateSurface();
  889. return *this;
  890. }
  891. void CLabel::OnSysColorChange()
  892. {
  893. if (m_hwndBrush)
  894. ::DeleteObject(m_hwndBrush);
  895. m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  896. UpdateSurface();
  897. }
  898. //////////////////////////////////////////////////////////////////////////
  899. //
  900. // Function: CLabel::SetRotationAngle
  901. //
  902. // Description: Sets the rotation angle for the current font.
  903. //
  904. // INPUTS: Angle in Degress
  905. //
  906. // RETURNS: Reference to 'this' object
  907. //
  908. // NOTES:
  909. //
  910. // MODIFICATIONS:
  911. //
  912. // Name Date Version Comments
  913. // NT ALMOND 22/10/98 1.0 Origin
  914. //
  915. //////////////////////////////////////////////////////////////////////////
  916. CLabel& CLabel::SetRotationAngle(UINT nAngle,BOOL bRotation)
  917. {
  918. // Arrrrh...
  919. // Your looking in here why the font is rotating, aren't you?
  920. // Well try setting the font name to 'Arial' or 'Times New Roman'
  921. // Make the Angle 180 and set bRotation to true.
  922. //
  923. // Font rotation _ONLY_ works with TrueType fonts...
  924. //
  925. //
  926. m_lf.lfEscapement = m_lf.lfOrientation = (nAngle * 10);
  927. m_bRotation = bRotation;
  928. ReconstructFont();
  929. UpdateSurface();
  930. return *this;
  931. }
  932. //////////////////////////////////////////////////////////////////////////
  933. //
  934. // Function: CLabel::SetText3DHiliteColor
  935. //
  936. // Description: Sets the 3D font hilite color
  937. //
  938. // INPUTS: Color
  939. //
  940. // RETURNS: Reference to 'this' object
  941. //
  942. // NOTES:
  943. //
  944. // MODIFICATIONS:
  945. //
  946. // Name Date Version Comments
  947. // NT ALMOND 17/07/00 1.0 Origin
  948. //
  949. //////////////////////////////////////////////////////////////////////////
  950. CLabel& CLabel::SetText3DHiliteColor(COLORREF cr3DHiliteColor)
  951. {
  952. m_cr3DHiliteColor = cr3DHiliteColor;
  953. UpdateSurface();
  954. return *this;
  955. }
  956. //////////////////////////////////////////////////////////////////////////
  957. //
  958. // Function: CLabel::PreSubclassWindow
  959. //
  960. // Description: Assigns default dialog font
  961. //
  962. // INPUTS:
  963. //
  964. // RETURNS:
  965. //
  966. // NOTES:
  967. //
  968. // MODIFICATIONS:
  969. //
  970. // Name Date Version Comments
  971. // NT ALMOND 15092000 1.5 Origin
  972. // NT ALMOND 02072002 1.6 Fix crash when GetFont returns NULL
  973. //////////////////////////////////////////////////////////////////////////
  974. void CLabel::PreSubclassWindow()
  975. {
  976. CStatic::PreSubclassWindow();
  977. CFont* cf = GetFont();
  978. if(cf !=NULL)
  979. {
  980. cf->GetObject(sizeof(m_lf),&m_lf);
  981. }
  982. else
  983. {
  984. GetObject(GetStockObject(SYSTEM_FONT),sizeof(m_lf),&m_lf);
  985. }
  986. ReconstructFont();
  987. }
  988. //////////////////////////////////////////////////////////////////////////
  989. //
  990. // Function: CLabel::PreCreateWindow
  991. //
  992. // Description:
  993. //
  994. // INPUTS:
  995. //
  996. // RETURNS:
  997. //
  998. // NOTES:
  999. //
  1000. // MODIFICATIONS:
  1001. //
  1002. // Name Date Version Comments
  1003. // NT ALMOND 15092000 1.5 Origin
  1004. //////////////////////////////////////////////////////////////////////////
  1005. BOOL CLabel::PreCreateWindow(CREATESTRUCT& cs)
  1006. {
  1007. return CStatic::PreCreateWindow(cs);
  1008. }
  1009. //////////////////////////////////////////////////////////////////////////
  1010. //
  1011. // Function: CLabel::DrawGradientFill
  1012. //
  1013. // Description: Internal help function to gradient fill background
  1014. //
  1015. // INPUTS:
  1016. //
  1017. // RETURNS:
  1018. //
  1019. // NOTES:
  1020. //
  1021. // MODIFICATIONS:
  1022. //
  1023. // Name Date Version Comments
  1024. // NT ALMOND 15092000 1.5 Origin
  1025. //////////////////////////////////////////////////////////////////////////
  1026. void CLabel::DrawGradientFill(CDC* pDC, CRect* pRect, COLORREF crStart, COLORREF crEnd, int nSegments)
  1027. {
  1028. // Get the starting RGB values and calculate the incremental
  1029. // changes to be applied.
  1030. COLORREF cr;
  1031. int nR = GetRValue(crStart);
  1032. int nG = GetGValue(crStart);
  1033. int nB = GetBValue(crStart);
  1034. int neB = GetBValue(crEnd);
  1035. int neG = GetGValue(crEnd);
  1036. int neR = GetRValue(crEnd);
  1037. if(nSegments > pRect->Width())
  1038. nSegments = pRect->Width();
  1039. int nDiffR = (neR - nR);
  1040. int nDiffG = (neG - nG);
  1041. int nDiffB = (neB - nB);
  1042. int ndR = 256 * (nDiffR) / (max(nSegments,1));
  1043. int ndG = 256 * (nDiffG) / (max(nSegments,1));
  1044. int ndB = 256 * (nDiffB) / (max(nSegments,1));
  1045. nR *= 256;
  1046. nG *= 256;
  1047. nB *= 256;
  1048. neR *= 256;
  1049. neG *= 256;
  1050. neB *= 256;
  1051. int nCX = pRect->Width() / max(nSegments,1), nLeft = pRect->left, nRight;
  1052. pDC->SelectStockObject(NULL_PEN);
  1053. for (int i = 0; i < nSegments; i++, nR += ndR, nG += ndG, nB += ndB)
  1054. {
  1055. // Use special code for the last segment to avoid any problems
  1056. // with integer division.
  1057. if (i == (nSegments - 1))
  1058. nRight = pRect->right;
  1059. else
  1060. nRight = nLeft + nCX;
  1061. cr = RGB(nR / 256, nG / 256, nB / 256);
  1062. {
  1063. CBrush br(cr);
  1064. CBrush* pbrOld = pDC->SelectObject(&br);
  1065. pDC->Rectangle(nLeft, pRect->top, nRight + 1, pRect->bottom);
  1066. pDC->SelectObject(pbrOld);
  1067. }
  1068. // Reset the left side of the drawing rectangle.
  1069. nLeft = nRight;
  1070. }
  1071. }
  1072. //////////////////////////////////////////////////////////////////////////
  1073. //
  1074. // Function: CLabel::SetFont
  1075. //
  1076. // Description: Sets font with LOGFONT structure
  1077. //
  1078. // INPUTS:
  1079. //
  1080. // RETURNS:
  1081. //
  1082. // NOTES:
  1083. //
  1084. // MODIFICATIONS:
  1085. //
  1086. // Name Date Version Comments
  1087. // NT ALMOND 02072002 1.6 Origin
  1088. //////////////////////////////////////////////////////////////////////////
  1089. CLabel& CLabel::SetFont(LOGFONT lf)
  1090. {
  1091. CopyMemory(&m_lf, &lf, sizeof(m_lf));
  1092. ReconstructFont();
  1093. UpdateSurface();
  1094. return *this;
  1095. }
  1096. BOOL CLabel::OnEraseBkgnd(CDC* pDC)
  1097. {
  1098. // TODO: Add your message handler code here and/or call default
  1099. return TRUE;
  1100. }
  1101. //////////////////////////////////////////////////////////////////////////
  1102. //
  1103. // Function: CLabel::SetMailLink
  1104. //
  1105. // Description: Sets the label so it becomes Mail enabled
  1106. //
  1107. // INPUTS:
  1108. //
  1109. // RETURNS:
  1110. //
  1111. // NOTES:
  1112. //
  1113. // MODIFICATIONS:
  1114. //
  1115. // Name Date Version Comments
  1116. // NT ALMOND 02072002 1.6 Origin
  1117. //////////////////////////////////////////////////////////////////////////
  1118. CLabel& CLabel::SetMailLink(BOOL bEnable, BOOL bNotifyParent)
  1119. {
  1120. if (bEnable)
  1121. m_Link = MailLink;
  1122. else
  1123. m_Link = LinkNone;
  1124. m_bNotifyParent = bNotifyParent;
  1125. if (m_Link != LinkNone)
  1126. ModifyStyle(0,SS_NOTIFY);
  1127. else
  1128. ModifyStyle(SS_NOTIFY,0);
  1129. return *this;
  1130. }
  1131. //////////////////////////////////////////////////////////////////////////
  1132. //
  1133. // Function: CLabel::SetHyperLink
  1134. //
  1135. // Description: Sets the label so it becomes hyperlink enabled
  1136. //
  1137. // INPUTS:
  1138. //
  1139. // RETURNS:
  1140. //
  1141. // NOTES:
  1142. //
  1143. // MODIFICATIONS:
  1144. //
  1145. // Name Date Version Comments
  1146. // NT ALMOND 02072002 1.6 Origin
  1147. //////////////////////////////////////////////////////////////////////////
  1148. CLabel& CLabel::SetHyperLink(const CString& sLink)
  1149. {
  1150. m_sLink = sLink;
  1151. return *this;
  1152. }
  1153. /*************************************************************
  1154. *add at : 2014-08-11 by唐太闲
  1155. *describe: 设置背景透明,返回空画刷
  1156. *************************************************************/
  1157. HBRUSH CLabel::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
  1158. {
  1159. // TODO: 在此更改 DC 的任何属性
  1160. // TODO: 如果不应调用父级的处理程序,则返回非 null 画笔
  1161. //return NULL;
  1162. pDC->SetBkMode(TRANSPARENT);
  1163. return (HBRUSH)GetStockObject(NULL_BRUSH);
  1164. }