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.

251 lines
6.7 KiB

  1. #include <windows.h>
  2. #include <GL\gl.h>
  3. #define WINDSIZEX(Rect) (Rect.right - Rect.left)
  4. #define WINDSIZEY(Rect) (Rect.bottom - Rect.top)
  5. #define XSIZE 16
  6. #define YSIZE 16
  7. long WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
  8. BOOL DrawPalette(HWND hwnd, HDC hdc);
  9. BOOL Cleanup(HWND hwnd, HDC hdc);
  10. HPALETTE ghpal = 0, ghpalOld = 0;
  11. int WINAPI
  12. WinMain( HINSTANCE hInstance,
  13. HINSTANCE hPrevInstance,
  14. LPSTR lpCmdLine,
  15. int nCmdShow
  16. )
  17. {
  18. static char szAppName[] = "View System Palette";
  19. HWND hwnd;
  20. MSG msg;
  21. RECT Rect;
  22. WNDCLASS wndclass;
  23. char title[32];
  24. if ( !hPrevInstance )
  25. {
  26. //wndclass.style = CS_HREDRAW | CS_VREDRAW;
  27. wndclass.style = CS_OWNDC;
  28. wndclass.lpfnWndProc = (WNDPROC)WndProc;
  29. wndclass.cbClsExtra = 0;
  30. wndclass.cbWndExtra = 0;
  31. wndclass.hInstance = hInstance;
  32. //wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  33. wndclass.hCursor = NULL;
  34. wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  35. wndclass.lpszMenuName = NULL;
  36. wndclass.lpszClassName = szAppName;
  37. // With a NULL icon handle, app will paint into the icon window.
  38. wndclass.hIcon = NULL;
  39. //wndclass.hIcon = LoadIcon(hInstance, "CubeIcon");
  40. RegisterClass(&wndclass);
  41. }
  42. /*
  43. * Make the windows a reasonable size and pick a
  44. * position for it.
  45. */
  46. Rect.left = 0;
  47. Rect.top = 0;
  48. Rect.right = 16 * XSIZE;
  49. Rect.bottom = 16 * YSIZE;
  50. AdjustWindowRect( &Rect, WS_OVERLAPPEDWINDOW, FALSE );
  51. hwnd = CreateWindowEx ( WS_EX_TOPMOST,
  52. szAppName, // window class name
  53. szAppName, // window caption
  54. WS_OVERLAPPEDWINDOW
  55. | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  56. CW_USEDEFAULT, // initial x position
  57. CW_USEDEFAULT, // initial y position
  58. WINDSIZEX(Rect), // initial x size
  59. WINDSIZEY(Rect), // initial y size
  60. NULL, // parent window handle
  61. NULL, // window menu handle
  62. hInstance, // program instance handle
  63. NULL // creation parameter
  64. );
  65. ShowWindow( hwnd, nCmdShow );
  66. UpdateWindow( hwnd );
  67. while ( GetMessage( &msg, NULL, 0, 0 ))
  68. {
  69. //if ( (hdlgRotate == 0) || !IsDialogMessage(hdlgRotate, &msg) )
  70. {
  71. TranslateMessage( &msg );
  72. DispatchMessage( &msg );
  73. }
  74. }
  75. return( msg.wParam );
  76. }
  77. long
  78. WndProc ( HWND hWnd,
  79. UINT message,
  80. WPARAM wParam,
  81. LPARAM lParam
  82. )
  83. {
  84. HDC hDc;
  85. PAINTSTRUCT ps;
  86. switch ( message )
  87. {
  88. case WM_PAINT:
  89. hDc = BeginPaint( hWnd, &ps );
  90. DrawPalette( hWnd, hDc );
  91. EndPaint( hWnd, &ps );
  92. return(0);
  93. case WM_PALETTECHANGED:
  94. if (hWnd != (HWND) wParam)
  95. {
  96. int iRet = 0;
  97. if (hDc = GetDC(hWnd))
  98. {
  99. SelectPalette(hDc, ghpal, TRUE);
  100. if (RealizePalette(hDc) != GDI_ERROR)
  101. {
  102. InvalidateRect(hWnd, NULL, FALSE);
  103. iRet = 1;
  104. }
  105. ReleaseDC(hWnd, hDc);
  106. }
  107. return iRet;
  108. }
  109. return 0;
  110. case WM_QUERYNEWPALETTE:
  111. if (hDc = GetDC(hWnd))
  112. {
  113. int iRet = 0;
  114. SelectPalette(hDc, ghpal, FALSE);
  115. if (RealizePalette(hDc) != GDI_ERROR)
  116. {
  117. InvalidateRect(hWnd, NULL, FALSE);
  118. iRet = 1;
  119. }
  120. ReleaseDC(hWnd, hDc);
  121. return iRet;
  122. }
  123. return 0;
  124. case WM_KEYDOWN:
  125. switch (wParam)
  126. {
  127. case VK_ESCAPE:
  128. PostMessage(hWnd, WM_DESTROY, 0, 0);
  129. break;
  130. default:
  131. break;
  132. }
  133. return 0;
  134. case WM_DESTROY:
  135. Cleanup(hWnd, hDc);
  136. PostQuitMessage( 0 );
  137. break;
  138. }
  139. return( DefWindowProc( hWnd, message, wParam, lParam ) );
  140. }
  141. BOOL DrawPalette(HWND hwnd, HDC hdc)
  142. {
  143. int i, j;
  144. LOGBRUSH lb;
  145. LOGPEN lp;
  146. HBRUSH hb, hbOld;
  147. HPEN hp, hpOld;
  148. LOGPALETTE *ppal;
  149. PALETTEENTRY *ppalent;
  150. ppal = (LOGPALETTE *)
  151. LocalAlloc(LMEM_FIXED,
  152. sizeof(LOGPALETTE) + 256*sizeof(PALETTEENTRY));
  153. if (!ppal)
  154. MessageBox(GetFocus(), "Out of memory!", "Error -- seepal.exe", MB_OK);
  155. lp.lopnStyle = PS_SOLID;
  156. lp.lopnWidth.x = 2;
  157. lp.lopnWidth.y = 2;
  158. lp.lopnColor = PALETTERGB(0, 0, 0);
  159. hpOld = SelectObject(hdc, hp = CreatePenIndirect(&lp));
  160. if (!hp || !hpOld)
  161. MessageBox(GetFocus(), "Failed to create pen.", "Error -- seepal.exe", MB_OK);
  162. lb.lbStyle = BS_SOLID;
  163. lb.lbHatch = 0;
  164. ppal->palVersion = 0x0300;
  165. ppal->palNumEntries = 256;
  166. ppalent = ppal->palPalEntry;
  167. if (GetSystemPaletteEntries(hdc, 0, 256, ppalent) != 256)
  168. MessageBox(GetFocus(), "Failed to read system palette.",
  169. "Error -- seepal.exe", MB_OK);
  170. if (ghpal)
  171. DeleteObject(SelectPalette(hdc, ghpalOld, FALSE));
  172. ghpal = CreatePalette(ppal);
  173. ghpalOld = SelectPalette(hdc, ghpal, FALSE);
  174. if (!ghpal || !ghpalOld)
  175. MessageBox(GetFocus(), "Failed to create palette.", "Error -- seepal.exe", MB_OK);
  176. RealizePalette(hdc);
  177. for (i = 0; i < 16; i++)
  178. for (j = 0; j < 16; j++)
  179. {
  180. //lb.lbColor = PALETTERGB(ppalent->peRed,
  181. // ppalent->peGreen,
  182. // ppalent->peBlue);
  183. lb.lbColor = PALETTEINDEX(i*16 + j);
  184. hbOld = SelectObject(hdc, hb = CreateBrushIndirect(&lb));
  185. if (!hb || !hbOld)
  186. MessageBox(GetFocus(), "Failed to create brush.", "Error -- seepal.exe", MB_OK);
  187. Rectangle(hdc, j*XSIZE, i*YSIZE, j*XSIZE + XSIZE, i*YSIZE + YSIZE);
  188. DeleteObject(SelectObject(hdc, hbOld));
  189. ppalent++;
  190. }
  191. DeleteObject(SelectObject(hdc, hpOld));
  192. LocalFree(ppal);
  193. return TRUE;
  194. }
  195. BOOL Cleanup(HWND hwnd, HDC hdc)
  196. {
  197. if (ghpal)
  198. DeleteObject(SelectPalette(hdc, ghpalOld, FALSE));
  199. return TRUE;
  200. }