ShadeButtonST.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "stdafx.h"
  2. #include "ShadeButtonST.h"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. CShadeButtonST::CShadeButtonST()
  9. {
  10. }
  11. CShadeButtonST::~CShadeButtonST()
  12. {
  13. }
  14. void CShadeButtonST::SetShade(UINT shadeID, BYTE granularity, BYTE highlight, BYTE coloring, COLORREF color)
  15. {
  16. long sXSize,sYSize,bytes,j,i,k,h;
  17. BYTE *iDst ,*posDst;
  18. RECT rect;
  19. GetWindowRect(&rect);
  20. sYSize=rect.bottom-rect.top;
  21. sXSize=rect.right-rect.left ;
  22. m_dh.Create(max(1,sXSize /*-2*m_FocusRectMargin-1*/ ),1,8); //create the horizontal focus bitmap
  23. m_dv.Create(1,max(1,sYSize /*-2*m_FocusRectMargin*/),8); //create the vertical focus bitmap
  24. m_dNormal.Create(sXSize,sYSize,8); //create the default bitmap
  25. COLORREF hicr=GetSysColor(COLOR_BTNHIGHLIGHT); //get the button base colors
  26. COLORREF midcr=GetSysColor(COLOR_BTNFACE);
  27. COLORREF locr=GetSysColor(COLOR_BTNSHADOW);
  28. long r,g,b; //build the shaded palette
  29. for(i=0;i<129;i++){
  30. r=((128-i)*GetRValue(locr)+i*GetRValue(midcr))/128;
  31. g=((128-i)*GetGValue(locr)+i*GetGValue(midcr))/128;
  32. b=((128-i)*GetBValue(locr)+i*GetBValue(midcr))/128;
  33. m_dNormal.SetPaletteIndex((BYTE)i,(BYTE)r,(BYTE)g,(BYTE)b);
  34. m_dh.SetPaletteIndex((BYTE)i,(BYTE)r,(BYTE)g,(BYTE)b);
  35. m_dv.SetPaletteIndex((BYTE)i,(BYTE)r,(BYTE)g,(BYTE)b);
  36. }
  37. for(i=1;i<129;i++){
  38. r=((128-i)*GetRValue(midcr)+i*GetRValue(hicr))/128;
  39. g=((128-i)*GetGValue(midcr)+i*GetGValue(hicr))/128;
  40. b=((128-i)*GetBValue(midcr)+i*GetBValue(hicr))/128;
  41. m_dNormal.SetPaletteIndex((BYTE)(i+127),(BYTE)r,(BYTE)g,(BYTE)b);
  42. m_dh.SetPaletteIndex((BYTE)(i+127),(BYTE)r,(BYTE)g,(BYTE)b);
  43. m_dv.SetPaletteIndex((BYTE)(i+127),(BYTE)r,(BYTE)g,(BYTE)b);
  44. }
  45. m_dNormal.BlendPalette(color,coloring); //color the palette
  46. iDst=m_dh.GetBits(); //build the horiz. dotted focus bitmap
  47. j=(long)m_dh.GetWidth();
  48. for(i=0;i<j;i++){
  49. // iDst[i]=64+127*(i%2); //soft
  50. iDst[i]=255*(i%2); //hard
  51. }
  52. iDst=m_dv.GetBits(); //build the vert. dotted focus bitmap
  53. j=(long)m_dv.GetHeight();
  54. for(i=0;i<j;i++){
  55. // *iDst=64+127*(i%2); //soft
  56. *iDst=255*(i%2); //hard
  57. iDst+=4;
  58. }
  59. bytes = m_dNormal.GetLineWidth();
  60. iDst = m_dNormal.GetBits();
  61. posDst =iDst;
  62. long a,x,y,d,xs,idxmax,idxmin;
  63. int grainx2=RAND_MAX/(max(1,2*granularity));
  64. idxmax=255-granularity;
  65. idxmin=granularity;
  66. switch(shadeID){
  67. //----------------------------------------------------
  68. case 8: //SHS_METAL
  69. m_dNormal.Clear();
  70. // create the strokes
  71. k=40; //stroke granularity
  72. for(a=0;a<200;a++){
  73. x=rand()/(RAND_MAX/sXSize); //stroke postion
  74. y=rand()/(RAND_MAX/sYSize); //stroke position
  75. xs=rand()/(RAND_MAX/min(sXSize,sYSize))/2; //stroke lenght
  76. d=rand()/(RAND_MAX/k); //stroke color
  77. for(i=0;i<xs;i++){
  78. if (((x-i)>0)&&((y+i)<sYSize))
  79. m_dNormal.SetPixelIndex(x-i,y+i,(BYTE)d);
  80. if (((x+i)<sXSize)&&((y-i)>0))
  81. m_dNormal.SetPixelIndex(sXSize-x+i,y-i,(BYTE)d);
  82. }
  83. }
  84. //blend strokes with SHS_DIAGONAL
  85. posDst =iDst;
  86. a=(idxmax-idxmin-k)/2;
  87. for(i = 0; i < sYSize; i++) {
  88. for(j = 0; j < sXSize; j++) {
  89. d=posDst[j]+((a*i)/sYSize+(a*(sXSize-j))/sXSize);
  90. posDst[j]=(BYTE)d;
  91. posDst[j]+=rand()/grainx2;
  92. }
  93. posDst+=bytes;
  94. }
  95. break;
  96. //----------------------------------------------------
  97. case 7: // SHS_HARDBUMP
  98. //set horizontal bump
  99. for(i = 0; i < sYSize; i++) {
  100. k=(255*i/sYSize)-127;
  101. k=(k*(k*k)/128)/128;
  102. k=(k*(128-granularity*2))/128+128;
  103. for(j = 0; j < sXSize; j++) {
  104. posDst[j]=(BYTE)k;
  105. posDst[j]+=rand()/grainx2-granularity;
  106. }
  107. posDst+=bytes;
  108. }
  109. //set vertical bump
  110. d=min(16,sXSize/6); //max edge=16
  111. a=sYSize*sYSize/4;
  112. posDst =iDst;
  113. for(i = 0; i < sYSize; i++) {
  114. y=i-sYSize/2;
  115. for(j = 0; j < sXSize; j++) {
  116. x=j-sXSize/2;
  117. xs=sXSize/2-d+(y*y*d)/a;
  118. if (x>xs) posDst[j]=idxmin+(BYTE)(((sXSize-j)*128)/d);
  119. if ((x+xs)<0) posDst[j]=idxmax-(BYTE)((j*128)/d);
  120. posDst[j]+=rand()/grainx2-granularity;
  121. }
  122. posDst+=bytes;
  123. }
  124. break;
  125. //----------------------------------------------------
  126. case 6: //SHS_SOFTBUMP
  127. for(i = 0; i < sYSize; i++) {
  128. h=(255*i/sYSize)-127;
  129. for(j = 0; j < sXSize; j++) {
  130. k=(255*(sXSize-j)/sXSize)-127;
  131. k=(h*(h*h)/128)/128+(k*(k*k)/128)/128;
  132. k=k*(128-granularity)/128+128;
  133. if (k<idxmin) k=idxmin;
  134. if (k>idxmax) k=idxmax;
  135. posDst[j]=(BYTE)k;
  136. posDst[j]+=rand()/grainx2-granularity;
  137. }
  138. posDst+=bytes;
  139. }
  140. break;
  141. //----------------------------------------------------
  142. case 5: // SHS_VBUMP
  143. for(j = 0; j < sXSize; j++) {
  144. k=(255*(sXSize-j)/sXSize)-127;
  145. k=(k*(k*k)/128)/128;
  146. k=(k*(128-granularity))/128+128;
  147. for(i = 0; i < sYSize; i++) {
  148. posDst[j+i*bytes]=(BYTE)k;
  149. posDst[j+i*bytes]+=rand()/grainx2-granularity;
  150. }
  151. }
  152. break;
  153. //----------------------------------------------------
  154. case 4: //SHS_HBUMP
  155. for(i = 0; i < sYSize; i++) {
  156. k=(255*i/sYSize)-127;
  157. k=(k*(k*k)/128)/128;
  158. k=(k*(128-granularity))/128+128;
  159. for(j = 0; j < sXSize; j++) {
  160. posDst[j]=(BYTE)k;
  161. posDst[j]+=rand()/grainx2-granularity;
  162. }
  163. posDst+=bytes;
  164. }
  165. break;
  166. //----------------------------------------------------
  167. case 1: //SHS_DIAGSHADE
  168. a=(idxmax-idxmin)/2;
  169. for(i = 0; i < sYSize; i++) {
  170. for(j = 0; j < sXSize; j++) {
  171. posDst[j]=(BYTE)(idxmin+a*i/sYSize+a*(sXSize-j)/sXSize);
  172. posDst[j]+=rand()/grainx2-granularity;
  173. }
  174. posDst+=bytes;
  175. }
  176. break;
  177. //----------------------------------------------------
  178. case 2: //SHS_HSHADE
  179. a=idxmax-idxmin;
  180. for(i = 0; i < sYSize; i++) {
  181. k=a*i/sYSize+idxmin;
  182. for(j = 0; j < sXSize; j++) {
  183. posDst[j]=(BYTE)k;
  184. posDst[j]+=rand()/grainx2-granularity;
  185. }
  186. posDst+=bytes;
  187. }
  188. break;
  189. //----------------------------------------------------
  190. case 3: //SHS_VSHADE:
  191. a=idxmax-idxmin;
  192. for(j = 0; j < sXSize; j++) {
  193. k=a*(sXSize-j)/sXSize+idxmin;
  194. for(i = 0; i < sYSize; i++) {
  195. posDst[j+i*bytes]=(BYTE)k;
  196. posDst[j+i*bytes]+=rand()/grainx2-granularity;
  197. }
  198. }
  199. break;
  200. //----------------------------------------------------
  201. default: //SHS_NOISE
  202. for(i = 0; i < sYSize; i++) {
  203. for(j = 0; j < sXSize; j++) {
  204. posDst[j]=128+rand()/grainx2-granularity;
  205. }
  206. posDst+=bytes;
  207. }
  208. }
  209. //----------------------------------------------------
  210. m_dDisabled.Clone(&m_dNormal); //build the other bitmaps
  211. m_dOver.Clone(&m_dNormal);
  212. m_dOver.BlendPalette(hicr,highlight);
  213. m_dDown.Clone(&m_dOver);
  214. } // End of SetShade
  215. DWORD CShadeButtonST::OnDrawBackground(CDC *pDC, CRect* pRect)
  216. {
  217. if (m_bMouseOnButton)
  218. {
  219. if (m_bIsPressed)
  220. m_dOver.Draw(pDC->GetSafeHdc(),1,1);
  221. else
  222. m_dOver.Draw(pDC->GetSafeHdc(),0,0);
  223. }
  224. else
  225. m_dNormal.Draw(pDC->GetSafeHdc(),0,0);
  226. return BTNST_OK;
  227. } // End of OnDrawBackground
  228. DWORD CShadeButtonST::OnDrawBorder(CDC* pDC, CRect* pRect)
  229. {
  230. BASE_BTNST_CLASS::OnDrawBorder(pDC, pRect);
  231. return BTNST_OK;
  232. } // End of OnDrawBorder