Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
11 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: CONTRAST.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 1/11/2001
  12. *
  13. * DESCRIPTION: Small preview window for illustrating brightness and contrast settings
  14. *
  15. *******************************************************************************/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. #include "contrast.h"
  19. LRESULT CBrightnessContrast::OnCreate( WPARAM, LPARAM )
  20. {
  21. return 0;
  22. }
  23. CBrightnessContrast::~CBrightnessContrast()
  24. {
  25. KillBitmaps();
  26. }
  27. CBrightnessContrast::CBrightnessContrast( HWND hWnd )
  28. : m_hWnd(hWnd),
  29. m_nIntent(0),
  30. m_nBrightness(50),
  31. m_nContrast(50),
  32. m_hBmpPreviewImage(NULL)
  33. {
  34. for (int i=0;i<NUMPREVIEWIMAGES;i++)
  35. {
  36. m_PreviewBitmaps[i] = NULL;
  37. }
  38. }
  39. LRESULT CBrightnessContrast::OnPaint(WPARAM, LPARAM)
  40. {
  41. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnPaint")));
  42. PAINTSTRUCT ps;
  43. HDC hDC = BeginPaint(m_hWnd,&ps);
  44. if (hDC)
  45. {
  46. if (m_hBmpPreviewImage)
  47. {
  48. RECT rcClient;
  49. GetClientRect( m_hWnd, &rcClient );
  50. //
  51. // Create a halftone palette
  52. //
  53. HPALETTE hHalftonePalette = CreateHalftonePalette(hDC);
  54. if (hHalftonePalette)
  55. {
  56. //
  57. // Select the halftone palette and save the result
  58. //
  59. HPALETTE hOldPalette = SelectPalette( hDC, hHalftonePalette, FALSE );
  60. RealizePalette( hDC );
  61. SetBrushOrgEx( hDC, 0,0, NULL );
  62. //
  63. // Set halftone stretchblt mode
  64. //
  65. int nOldStretchBltMode = SetStretchBltMode(hDC,HALFTONE);
  66. //
  67. // Draw 3D Border
  68. //
  69. //
  70. // Draw shadow
  71. //
  72. RECT rcBottomShadow, rcRightShadow;
  73. MoveToEx(hDC,rcClient.left,rcClient.top,NULL);
  74. LineTo(hDC,rcClient.right-(SHADOW_WIDTH+1),rcClient.top);
  75. LineTo(hDC,rcClient.right-(SHADOW_WIDTH+1),rcClient.bottom-(SHADOW_WIDTH+1));
  76. LineTo(hDC,rcClient.left,rcClient.bottom-(SHADOW_WIDTH+1));
  77. LineTo(hDC,rcClient.left,rcClient.top);
  78. rcBottomShadow.left=rcClient.left+SHADOW_WIDTH;
  79. rcBottomShadow.right=rcClient.right+1;
  80. rcBottomShadow.top=rcClient.bottom-SHADOW_WIDTH;
  81. rcBottomShadow.bottom=rcClient.bottom+1;
  82. //
  83. // bottom edge:
  84. //
  85. FillRect(hDC,&rcBottomShadow,GetSysColorBrush(COLOR_3DSHADOW));
  86. //
  87. // Fill in bottom left corner:
  88. //
  89. rcBottomShadow.left=rcClient.left;
  90. rcBottomShadow.right=rcClient.left+SHADOW_WIDTH;
  91. rcBottomShadow.top=rcClient.bottom-SHADOW_WIDTH;
  92. rcBottomShadow.bottom=rcClient.bottom+1;
  93. FillRect(hDC,&rcBottomShadow,GetSysColorBrush(COLOR_3DFACE));
  94. rcRightShadow.left=rcClient.right-SHADOW_WIDTH;
  95. rcRightShadow.right=rcClient.right+1;
  96. rcRightShadow.top=rcClient.top+SHADOW_WIDTH;
  97. rcRightShadow.bottom=rcClient.bottom-SHADOW_WIDTH;
  98. //
  99. // right edge
  100. //
  101. FillRect(hDC,&rcRightShadow,GetSysColorBrush(COLOR_3DSHADOW));
  102. rcRightShadow.left=rcClient.right-5;
  103. rcRightShadow.right=rcClient.right+1;
  104. rcRightShadow.top=rcClient.top;
  105. rcRightShadow.bottom=rcClient.top+5;
  106. //
  107. // Top right corner
  108. //
  109. FillRect(hDC,&rcRightShadow,(HBRUSH)(COLOR_3DFACE+1));
  110. //
  111. // Paint Image
  112. //
  113. HDC hdcMem = CreateCompatibleDC(hDC);
  114. if (hdcMem)
  115. {
  116. //
  117. // Select and realize the halftone palette
  118. //
  119. HPALETTE hOldMemDCPalette = SelectPalette(hdcMem,hHalftonePalette,FALSE);
  120. RealizePalette(hdcMem);
  121. SetBrushOrgEx(hdcMem, 0,0, NULL );
  122. //
  123. // Select the old bitmap
  124. //
  125. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem,m_hBmpPreviewImage);
  126. //
  127. // Paint the preview bitmap
  128. //
  129. BITMAP bm = {0};
  130. if (GetObject(m_hBmpPreviewImage,sizeof(BITMAP),&bm))
  131. {
  132. StretchBlt(hDC,rcClient.left+2,rcClient.top+2,WiaUiUtil::RectWidth(rcClient)-9,WiaUiUtil::RectHeight(rcClient)-9,hdcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
  133. }
  134. //
  135. // Restore the palette
  136. //
  137. SelectPalette( hdcMem, hOldMemDCPalette, FALSE );
  138. //
  139. // Delete the DC
  140. //
  141. DeleteDC(hdcMem);
  142. }
  143. //
  144. // Restore the old palette and delete the halftone palette
  145. //
  146. SelectPalette( hDC, hOldPalette, FALSE );
  147. DeleteObject( hHalftonePalette );
  148. }
  149. //
  150. // We're done
  151. //
  152. EndPaint(m_hWnd,&ps);
  153. }
  154. }
  155. return 0;
  156. }
  157. LRESULT CBrightnessContrast::SetBrightness(int nBrightness)
  158. {
  159. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::SetBrightness")));
  160. if (nBrightness >= 0 && nBrightness <= 100)
  161. {
  162. m_nBrightness = static_cast<BYTE>(nBrightness);
  163. ApplySettings();
  164. }
  165. return 0;
  166. }
  167. LRESULT CBrightnessContrast::SetContrast(int nContrast)
  168. {
  169. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::SetContrast")));
  170. if (m_nContrast >= 0 && m_nContrast <= 100)
  171. {
  172. m_nContrast = static_cast<BYTE>(nContrast);
  173. ApplySettings();
  174. }
  175. return 0;
  176. }
  177. LRESULT CBrightnessContrast::SetIntent(LONG nIntent)
  178. {
  179. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::SetIntent")));
  180. if (nIntent < NUMPREVIEWIMAGES && nIntent >= 0)
  181. {
  182. m_nIntent = nIntent;
  183. ApplySettings();
  184. }
  185. return 0;
  186. }
  187. LRESULT CBrightnessContrast::KillBitmaps()
  188. {
  189. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::KillBitmaps")));
  190. for (int i=0;i<NUMPREVIEWIMAGES;i++)
  191. {
  192. if (m_PreviewBitmaps[i])
  193. {
  194. DeleteObject( m_PreviewBitmaps[i] );
  195. m_PreviewBitmaps[i] = NULL;
  196. }
  197. }
  198. if (m_hBmpPreviewImage)
  199. {
  200. DeleteObject(m_hBmpPreviewImage);
  201. m_hBmpPreviewImage = NULL;
  202. }
  203. return 0;
  204. }
  205. LRESULT CBrightnessContrast::ApplySettings()
  206. {
  207. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::ApplySettings")));
  208. if (m_PreviewBitmaps[m_nIntent])
  209. {
  210. if (m_hBmpPreviewImage)
  211. {
  212. DeleteObject(m_hBmpPreviewImage);
  213. m_hBmpPreviewImage = NULL;
  214. }
  215. #ifdef DONT_USE_GDIPLUS
  216. m_hBmpPreviewImage = reinterpret_cast<HBITMAP>(CopyImage( m_PreviewBitmaps[m_nIntent], IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ));
  217. #else
  218. //
  219. // If the window is enabled, use the real brightness and contrast settings
  220. //
  221. if (IsWindowEnabled(m_hWnd))
  222. {
  223. if (BCPWM_BW == m_nIntent)
  224. {
  225. m_GdiPlusHelper.SetThreshold( m_PreviewBitmaps[m_nIntent], m_hBmpPreviewImage, m_nBrightness );
  226. }
  227. else
  228. {
  229. m_GdiPlusHelper.SetBrightnessAndContrast( m_PreviewBitmaps[m_nIntent], m_hBmpPreviewImage, m_nBrightness, m_nContrast );
  230. }
  231. }
  232. //
  233. // Otherwise, use the nominal settings, to prevent feedback
  234. //
  235. else
  236. {
  237. if (BCPWM_BW == m_nIntent)
  238. {
  239. m_GdiPlusHelper.SetThreshold( m_PreviewBitmaps[m_nIntent], m_hBmpPreviewImage, 50 );
  240. }
  241. else
  242. {
  243. m_GdiPlusHelper.SetBrightnessAndContrast( m_PreviewBitmaps[m_nIntent], m_hBmpPreviewImage, 50, 50 );
  244. }
  245. }
  246. #endif // !DONT_USE_GDIPLUS
  247. }
  248. InvalidateRect(m_hWnd,NULL,FALSE);
  249. UpdateWindow(m_hWnd);
  250. return 0;
  251. }
  252. BOOL CBrightnessContrast::RegisterClass( HINSTANCE hInstance )
  253. {
  254. WNDCLASS wc = {0};
  255. wc.style = CS_DBLCLKS;
  256. wc.cbWndExtra = sizeof(CBrightnessContrast*);
  257. wc.lpfnWndProc = WndProc;
  258. wc.hInstance = hInstance;
  259. wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  260. wc.lpszClassName = BRIGHTNESS_CONTRAST_PREVIEW_WINDOW_CLASS;
  261. BOOL res = (::RegisterClass(&wc) != 0);
  262. return (res != 0);
  263. }
  264. LRESULT CBrightnessContrast::OnSetBrightness( WPARAM wParam, LPARAM lParam)
  265. {
  266. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnSetBrightness")));
  267. SetBrightness(static_cast<BYTE>(lParam));
  268. return 0;
  269. }
  270. LRESULT CBrightnessContrast::OnSetContrast( WPARAM wParam, LPARAM lParam)
  271. {
  272. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnSetContrast")));
  273. SetContrast(static_cast<BYTE>(lParam));
  274. return 0;
  275. }
  276. LRESULT CBrightnessContrast::OnSetIntent( WPARAM wParam, LPARAM lParam)
  277. {
  278. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnSetIntent")));
  279. SetIntent(static_cast<int>(lParam));
  280. return 0;
  281. }
  282. LRESULT CBrightnessContrast::OnLoadBitmap( WPARAM wParam, LPARAM lParam )
  283. {
  284. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnLoadBitmap")));
  285. int nId = static_cast<int>(wParam);
  286. if (nId < NUMPREVIEWIMAGES && nId >= 0)
  287. {
  288. if (m_PreviewBitmaps[nId])
  289. {
  290. DeleteObject(m_PreviewBitmaps[nId]);
  291. m_PreviewBitmaps[nId] = NULL;
  292. }
  293. m_PreviewBitmaps[nId] = reinterpret_cast<HBITMAP>(lParam);
  294. }
  295. return 0;
  296. }
  297. LRESULT CBrightnessContrast::OnEnable( WPARAM wParam, LPARAM )
  298. {
  299. //
  300. // Update the control's appearance when we are enabled or disabled
  301. //
  302. WIA_PUSH_FUNCTION((TEXT("CBrightnessContrast::OnEnable")));
  303. if (wParam)
  304. {
  305. ApplySettings();
  306. }
  307. return 0;
  308. }
  309. LRESULT CALLBACK CBrightnessContrast::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  310. {
  311. SC_BEGIN_MESSAGE_HANDLERS(CBrightnessContrast)
  312. {
  313. SC_HANDLE_MESSAGE( WM_CREATE, OnCreate );
  314. SC_HANDLE_MESSAGE( WM_ENABLE, OnEnable );
  315. SC_HANDLE_MESSAGE( WM_PAINT, OnPaint );
  316. SC_HANDLE_MESSAGE( BCPWM_SETBRIGHTNESS, OnSetBrightness );
  317. SC_HANDLE_MESSAGE( BCPWM_SETCONTRAST, OnSetContrast );
  318. SC_HANDLE_MESSAGE( BCPWM_SETINTENT, OnSetIntent);
  319. SC_HANDLE_MESSAGE( BCPWM_LOADIMAGE, OnLoadBitmap);
  320. }
  321. SC_END_MESSAGE_HANDLERS();
  322. }