EasySize.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*===================================================*\
  2. | |
  3. | EASY-SIZE Macros |
  4. | |
  5. | Copyright (c) 2001 - Marc Richarme |
  6. | devix@devix.cjb.net |
  7. | http://devix.cjb.net |
  8. | |
  9. | License: |
  10. | |
  11. | You may use this code in any commersial or non- |
  12. | commersial application, and you may redistribute |
  13. | this file (and even modify it if you wish) as |
  14. | long as you keep this notice untouched in any |
  15. | version you redistribute. |
  16. | |
  17. | Usage: |
  18. | |
  19. | - Insert 'DECLARE_EASYSIZE' somewhere in your |
  20. | class declaration |
  21. | - Insert an easysize map in the beginning of your |
  22. | class implementation (see documentation) and |
  23. | outside of any function. |
  24. | - Insert 'INIT_EASYSIZE;' in your |
  25. | OnInitDialog handler. |
  26. | - Insert 'UPDATE_EASYSIZE' in your OnSize handler |
  27. | - Optional: Insert 'EASYSIZE_MINSIZE(mx,my);' in |
  28. | your OnSizing handler if you want to specify |
  29. | a minimum size for your dialog |
  30. | |
  31. | Check http://devix.cjb.net for the |
  32. | docs and new versions |
  33. | |
  34. \*===================================================*/
  35. #ifndef __EASYSIZE_H_
  36. #define __EASYSIZE_H_
  37. #define ES_BORDER 0xffffffff
  38. #define ES_KEEPSIZE 0xfffffffe
  39. #define ES_HCENTER 0x00000001
  40. #define ES_VCENTER 0x00000002
  41. #define DECLARE_EASYSIZE \
  42. void __ES__RepositionControls(BOOL bInit);\
  43. void __ES__CalcBottomRight(CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft, UINT id, UINT br, int es_br, CRect &rect, int clientbottomright);
  44. #define INIT_EASYSIZE __ES__RepositionControls(TRUE); __ES__RepositionControls(FALSE)
  45. #define UPDATE_EASYSIZE if(GetWindow(GW_CHILD)!=NULL) __ES__RepositionControls(FALSE)
  46. #define EASYSIZE_MINSIZE(mx,my,s,r) if(r->right-r->left < mx) { if((s == WMSZ_BOTTOMLEFT)||(s == WMSZ_LEFT)||(s == WMSZ_TOPLEFT)) r->left = r->right-mx; else r->right = r->left+mx; } if(r->bottom-r->top < my) { if((s == WMSZ_TOP)||(s == WMSZ_TOPLEFT)||(s == WMSZ_TOPRIGHT)) r->top = r->bottom-my; else r->bottom = r->top+my; }
  47. #define BEGIN_EASYSIZE_MAP(class) \
  48. void class::__ES__CalcBottomRight(CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft, UINT id, UINT br, int es_br, CRect &rect, int clientbottomright) {\
  49. if(br==ES_BORDER) bottomright = clientbottomright-es_br;\
  50. else if(br==ES_KEEPSIZE) bottomright = topleft+es_br;\
  51. else { CRect rect2;\
  52. pThis->GetDlgItem(br)->GetWindowRect(rect2); pThis->ScreenToClient(rect2);\
  53. bottomright = (bBottom?rect2.top:rect2.left) - es_br;}}\
  54. void class::__ES__RepositionControls(BOOL bInit) { CRect rect,rect2,client; GetClientRect(client);
  55. #define END_EASYSIZE_MAP Invalidate(); UpdateWindow(); }
  56. #define EASYSIZE(id,l,t,r,b,o) \
  57. static int id##_es_l, id##_es_t, id##_es_r, id##_es_b;\
  58. if(bInit) {\
  59. GetDlgItem(id)->GetWindowRect(rect); ScreenToClient(rect);\
  60. if(o & ES_HCENTER) id##_es_l = rect.Width()/2; else {\
  61. if(l==ES_BORDER) id##_es_l = rect.left; else if(l==ES_KEEPSIZE) id##_es_l = rect.Width(); else {\
  62. GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2);\
  63. id##_es_l = rect.left-rect2.right;}}\
  64. if(o & ES_VCENTER) id##_es_t = rect.Height()/2; else {\
  65. if(t==ES_BORDER) id##_es_t = rect.top; else if(t==ES_KEEPSIZE) id##_es_t = rect.Height(); else {\
  66. GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2);\
  67. id##_es_t = rect.top-rect2.bottom;}}\
  68. if(o & ES_HCENTER) id##_es_r = rect.Width(); else { if(r==ES_BORDER) id##_es_r = client.right-rect.right; else if(r==ES_KEEPSIZE) id##_es_r = rect.Width(); else {\
  69. GetDlgItem(r)->GetWindowRect(rect2); ScreenToClient(rect2);\
  70. id##_es_r = rect2.left-rect.right;}}\
  71. if(o & ES_VCENTER) id##_es_b = rect.Height(); else { if(b==ES_BORDER) id##_es_b = client.bottom-rect.bottom; else if(b==ES_KEEPSIZE) id##_es_b = rect.Height(); else {\
  72. GetDlgItem(b)->GetWindowRect(rect2); ScreenToClient(rect2);\
  73. id##_es_b = rect2.top-rect.bottom;}}\
  74. } else {\
  75. int left,top,right,bottom; BOOL bR = FALSE,bB = FALSE;\
  76. if(o & ES_HCENTER) { int _a,_b;\
  77. if(l==ES_BORDER) _a = client.left; else { GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2); _a = rect2.right; }\
  78. if(r==ES_BORDER) _b = client.right; else { GetDlgItem(r)->GetWindowRect(rect2); ScreenToClient(rect2); _b = rect2.left; }\
  79. left = _a+((_b-_a)/2-id##_es_l); right = left + id##_es_r;} else {\
  80. if(l==ES_BORDER) left = id##_es_l;\
  81. else if(l==ES_KEEPSIZE) { __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right); left = right-id##_es_l;\
  82. } else { GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2); left = rect2.right + id##_es_l; }\
  83. if(l != ES_KEEPSIZE) __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right);}\
  84. if(o & ES_VCENTER) { int _a,_b;\
  85. if(t==ES_BORDER) _a = client.top; else { GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2); _a = rect2.bottom; }\
  86. if(b==ES_BORDER) _b = client.bottom; else { GetDlgItem(b)->GetWindowRect(rect2); ScreenToClient(rect2); _b = rect2.top; }\
  87. top = _a+((_b-_a)/2-id##_es_t); bottom = top + id##_es_b;} else {\
  88. if(t==ES_BORDER) top = id##_es_t;\
  89. else if(t==ES_KEEPSIZE) { __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom); top = bottom-id##_es_t;\
  90. } else { GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2); top = rect2.bottom + id##_es_t; }\
  91. if(t != ES_KEEPSIZE) __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom);}\
  92. GetDlgItem(id)->MoveWindow(left,top,right-left,bottom-top,FALSE);\
  93. }
  94. #endif //__EASYSIZE_H_