HListBox.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "stdafx.h"
  2. #include "HListBox.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CHListBox
  10. CHListBox::CHListBox()
  11. {
  12. width = 0;
  13. }
  14. CHListBox::~CHListBox()
  15. {
  16. }
  17. BEGIN_MESSAGE_MAP(CHListBox, CListBox)
  18. //{{AFX_MSG_MAP(CHListBox)
  19. // NOTE - the ClassWizard will add and remove mapping macros here.
  20. //}}AFX_MSG_MAP
  21. END_MESSAGE_MAP()
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CHListBox message handlers
  24. void CHListBox::updateWidth(LPCTSTR s)
  25. {
  26. CClientDC dc(this);
  27. CFont * f = CListBox::GetFont();
  28. dc.SelectObject(f);
  29. CSize sz = dc.GetTextExtent(s, (int)_tcslen(s));
  30. sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
  31. if(sz.cx > width)
  32. { /* extend */
  33. width = sz.cx;
  34. CListBox::SetHorizontalExtent(width);
  35. } /* extend */
  36. }
  37. int CHListBox::AddString(LPCTSTR s)
  38. {
  39. int result = CListBox::AddString(s);
  40. if(result < 0)
  41. return result;
  42. updateWidth(s);
  43. return result;
  44. }
  45. int CHListBox::InsertString(int i, LPCTSTR s)
  46. {
  47. int result = CListBox::InsertString(i, s);
  48. if(result < 0)
  49. return result;
  50. updateWidth(s);
  51. return result;
  52. }
  53. void CHListBox::ResetContent()
  54. {
  55. CListBox::ResetContent();
  56. width = 0;
  57. }
  58. int CHListBox::DeleteString(int n)
  59. {
  60. int result = CListBox::DeleteString(n);
  61. if(result < 0)
  62. return result;
  63. CClientDC dc(this);
  64. CFont * f = CListBox::GetFont();
  65. dc.SelectObject(f);
  66. width = 0;
  67. for(int i = 0; i < CListBox::GetCount(); i++)
  68. { /* scan strings */
  69. CString s;
  70. CListBox::GetText(i, s);
  71. CSize sz = dc.GetTextExtent(s);
  72. sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
  73. if(sz.cx > width)
  74. width = sz.cx;
  75. } /* scan strings */
  76. CListBox::SetHorizontalExtent(width);
  77. return result;
  78. }