backprev.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 11k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*  BACKPREV.C
  2. **
  3. **  Copyright (C) Microsoft, 1993, All Rights Reserved.
  4. **
  5. **  window class to display a preview of the screen background,
  6. **  complete with rudimentary palette handling and stretching
  7. **  of bitmaps to fit the preview screen.
  8. **
  9. **  this can be replaced with a static bitmap control only
  10. **  if palettes can also be handled by the control.
  11. **
  12. */
  13. #include <windows.h>
  14. #include "desk.h"
  15. #include "deskid.h"
  16. #define GWW_INFO        0
  17. #define CXYDESKPATTERN 8
  18. BOOL g_bInfoSet = FALSE;
  19. HBITMAP g_hbmPreview = NULL;    // the bitmap used for previewing
  20. HBITMAP  g_hbmWall = NULL;      // bitmap image of wallpaper
  21. HDC      g_hdcWall = NULL;      // memory DC with g_hbmWall selected
  22. HPALETTE g_hpalWall = NULL;     // palette that goes with hbmWall bitmap
  23. HBRUSH   g_hbrBack = NULL;      // brush for the desktop background
  24. //extern HPALETTE WINAPI CreateHalftonePalette(HDC hdc);
  25. /*-------------------------------------------------------------
  26. ** given a pattern string from an ini file, return the pattern
  27. ** in a binary (ie useful) form.
  28. **-------------------------------------------------------------*/
  29. void FAR PASCAL ReadPattern(LPTSTR lpStr, WORD FAR *patbits)
  30. {
  31.   short i, val;
  32.   /* Get eight groups of numbers seprated by non-numeric characters. */
  33.   for (i = 0; i < CXYDESKPATTERN; i++)
  34.     {
  35.       val = 0;
  36.       if (*lpStr != TEXT(''))
  37.         {
  38.           /* Skip over any non-numeric characters. */
  39.           while (!(*lpStr >= TEXT('0') && *lpStr <= TEXT('9')))
  40.               lpStr++;
  41.           /* Get the next series of digits. */
  42.           while (*lpStr >= TEXT('0') && *lpStr <= TEXT('9'))
  43.               val = val*10 + *lpStr++ - TEXT('0');
  44.          }
  45.       patbits[i] = val;
  46.     }
  47.   return;
  48. }
  49. /*----------------------------------------------------------------------------*
  50. *----------------------------------------------------------------------------*/
  51. HPALETTE PaletteFromDS(HDC hdc)
  52. {
  53.     DWORD adw[257];
  54.     int i,n;
  55.     n = GetDIBColorTable(hdc, 0, 256, (LPRGBQUAD)&adw[1]);
  56.     adw[0] = MAKELONG(0x300, n);
  57.     for (i=1; i<=n; i++)
  58.         adw[i] = RGB(GetBValue(adw[i]),GetGValue(adw[i]),GetRValue(adw[i]));
  59.     if (n == 0)
  60.         return NULL;
  61.     else
  62.         return CreatePalette((LPLOGPALETTE)&adw[0]);
  63. }
  64. /*--------------------------------------------------------------------
  65. ** Build the preview bitmap.
  66. **
  67. ** both the pattern and the bitmap are drawn each time, but
  68. ** if the flags dictate the need, new pattern and bitmap
  69. ** globals are built as needed.
  70. **--------------------------------------------------------------------*/
  71. void NEAR PASCAL BuildPreviewBitmap(HWND hwnd, WPARAM flags)
  72. {
  73.     HBRUSH hbr = NULL;
  74.     HBITMAP hbmTemp;
  75.     HBITMAP hbmOld;
  76.     BITMAP bm;
  77.     TCHAR szBuf[MAX_PATH];
  78.     COLORREF clrOldBk, clrOldText;
  79.     WORD patbits[CXYDESKPATTERN] = {0, 0, 0, 0, 0, 0, 0, 0};
  80.     int     i;
  81.     HCURSOR hcurOld = NULL;
  82.     int     dxWall;          // size of wallpaper
  83.     int     dyWall;
  84.     if( flags & BP_REINIT )
  85.     {
  86.         if( g_hbmPreview )
  87.             DeleteObject( g_hbmPreview );
  88.         g_hbmPreview = LoadMonitorBitmap( TRUE );
  89.     }
  90.     hbmOld = SelectObject(g_hdcMem, g_hbmPreview);
  91.     /*
  92.     ** first, fill in the pattern all over the bitmap
  93.     */
  94.     if (flags & BP_NEWPAT)
  95.     {
  96.         // get rid of old brush if there was one
  97.         if (g_hbrBack)
  98.             DeleteObject(g_hbrBack);
  99.         if (*g_szCurPattern && lstrcmpi(g_szCurPattern, g_szNone))
  100.         {
  101.             if (GetPrivateProfileString(g_szPatterns, g_szCurPattern, g_szNULL,
  102.                                         szBuf, ARRAYSIZE(szBuf), g_szControlIni))
  103.             {
  104.                 ReadPattern(szBuf, patbits);    
  105.             }
  106.             hbmTemp = CreateBitmap(8, 8, 1, 1, patbits);
  107.             if (hbmTemp)
  108.             {
  109.                 g_hbrBack = CreatePatternBrush(hbmTemp);
  110.                 DeleteObject(hbmTemp);
  111.             }
  112.         }
  113.         else
  114.         {
  115.             g_hbrBack = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
  116.         }
  117.         if (!g_hbrBack)
  118.             g_hbrBack = GetStockObject(BLACK_BRUSH);
  119.     }
  120.     clrOldText = SetTextColor(g_hdcMem, GetSysColor(COLOR_BACKGROUND));
  121.     clrOldBk = SetBkColor(g_hdcMem, GetSysColor(COLOR_WINDOWTEXT));
  122.     hbr = SelectObject(g_hdcMem, g_hbrBack);
  123.     PatBlt(g_hdcMem, MON_X, MON_Y, MON_DX, MON_DY, PATCOPY);
  124.     SelectObject(g_hdcMem, hbr);
  125.     SetTextColor(g_hdcMem, clrOldText);
  126.     SetBkColor(g_hdcMem, clrOldBk);
  127.     /*
  128.     ** now, position the wallpaper appropriately
  129.     */
  130.     if (flags & BP_NEWWALL)
  131.     {
  132.         g_bValidBitmap = TRUE;  // assume the new one is valid
  133.         if (g_hbmWall)
  134.         {
  135.             SelectObject(g_hdcWall, g_hbmDefault);
  136.             DeleteObject(g_hbmWall);
  137.             g_hbmWall = NULL;
  138.             if (g_hpalWall)
  139.             {
  140.                 DeleteObject(g_hpalWall);
  141.                 g_hpalWall = NULL;
  142.             }
  143.         }
  144.         if (!*g_szCurWallpaper || !lstrcmpi(g_szCurWallpaper, g_szNone))
  145.             goto DonePreview;
  146.         g_hbmWall = LoadImage(NULL, g_szCurWallpaper, IMAGE_BITMAP, 0, 0,
  147.             LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  148.         if (!g_hbmWall)
  149.         {
  150.             g_bValidBitmap = FALSE;  // until we know it's not
  151.             goto DonePreview;
  152.         }
  153.         SelectObject(g_hdcWall, g_hbmWall); // bitmap stays in this DC
  154.         GetObject(g_hbmWall, sizeof(bm), &bm);
  155.         if (GetDeviceCaps(g_hdcMem, RASTERCAPS) & RC_PALETTE)
  156.         {
  157.             if (bm.bmBitsPixel * bm.bmPlanes > 8)
  158.                 g_hpalWall = CreateHalftonePalette(g_hdcMem);
  159.             else if (bm.bmBitsPixel * bm.bmPlanes == 8)
  160.                 g_hpalWall = PaletteFromDS(g_hdcWall);
  161.             else
  162.                 g_hpalWall = NULL;  //!!! assume 1 or 4bpp images dont have palettes
  163.         }
  164.     }
  165.     if (g_hbmWall)
  166.     {
  167.         GetObject(g_hbmWall, sizeof(bm), &bm);
  168.         dxWall = MulDiv(bm.bmWidth, MON_DX, GetDeviceCaps(g_hdcMem, HORZRES));
  169.         dyWall = MulDiv(bm.bmHeight, MON_DY, GetDeviceCaps(g_hdcMem, VERTRES));
  170.         if (dxWall < 1) dxWall = 1;
  171.         if (dyWall < 1) dyWall = 1;
  172.         if (g_hpalWall)
  173.         {
  174.             SelectPalette(g_hdcMem, g_hpalWall, TRUE);
  175.             RealizePalette(g_hdcMem);
  176.         }
  177.         IntersectClipRect(g_hdcMem, MON_X, MON_Y, MON_X + MON_DX, MON_Y + MON_DY);
  178.         SetStretchBltMode(g_hdcMem, COLORONCOLOR);
  179.         if (flags & BP_TILE)
  180.         {
  181.             StretchBlt(g_hdcMem, MON_X, MON_Y, dxWall, dyWall,
  182.                 g_hdcWall, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  183.             for (i = MON_X+dxWall; i < (MON_X + MON_DX); i+= dxWall)
  184.                 BitBlt(g_hdcMem, i, MON_Y, dxWall, dyWall, g_hdcMem, MON_X, MON_Y, SRCCOPY);
  185.             for (i = MON_Y; i < (MON_Y + MON_DY); i += dyWall)
  186.                 BitBlt(g_hdcMem, MON_X, i, MON_DX, dyWall, g_hdcMem, MON_X, MON_Y, SRCCOPY);
  187.         }
  188.         else
  189.         {
  190.             StretchBlt(g_hdcMem, MON_X + (MON_DX - dxWall)/2, MON_Y + (MON_DY - dyWall)/2,
  191.                     dxWall, dyWall, g_hdcWall, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  192.         }
  193.         // restore dc
  194.         SelectPalette(g_hdcMem, GetStockObject(DEFAULT_PALETTE), TRUE);
  195.         SelectClipRgn(g_hdcMem, NULL);
  196.     }
  197. DonePreview:
  198.     SelectObject(g_hdcMem, hbmOld);
  199.     if (hcurOld)
  200.         SetCursor(hcurOld);
  201. }
  202. BOOL NEAR PASCAL BP_CreateGlobals(void)
  203. {
  204.     HDC hdc;
  205.     hdc = GetDC(NULL);
  206.     g_hdcWall = CreateCompatibleDC(hdc);
  207.     ReleaseDC(NULL, hdc);
  208.     g_hbmPreview = LoadMonitorBitmap( TRUE );
  209.     if (!g_hdcWall || !g_hbmPreview)
  210.         return FALSE;
  211.     else
  212.         return TRUE;
  213. }
  214. void NEAR PASCAL BP_DestroyGlobals(void)
  215. {
  216.     if (g_hbmPreview)
  217.     {
  218.         DeleteObject(g_hbmPreview);
  219.         g_hbmPreview = NULL;
  220.     }
  221.     if (g_hbmWall)
  222.     {
  223.         SelectObject(g_hdcWall, g_hbmDefault);
  224.         DeleteObject(g_hbmWall);
  225.         g_hbmWall = NULL;
  226.     }
  227.     if (g_hpalWall)
  228.     {
  229.         SelectPalette(g_hdcWall, GetStockObject(DEFAULT_PALETTE), TRUE);
  230.         DeleteObject(g_hpalWall);
  231.         g_hpalWall = NULL;
  232.     }
  233.     if (g_hdcWall)
  234.     {
  235.         DeleteDC(g_hdcWall);
  236.         g_hdcWall = NULL;
  237.     }
  238.     if (g_hbrBack)
  239.     {
  240.         DeleteObject(g_hbrBack);
  241.         g_hbrBack = NULL;
  242.     }
  243. }
  244. LONG CALLBACK  BackPreviewWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  245. {
  246. PAINTSTRUCT     ps;
  247. BITMAP          bm;
  248. HBITMAP         hbmOld;
  249. HPALETTE        hpalOld;
  250. RECT            rc;
  251.     switch(message)
  252.     {
  253.         case WM_CREATE:
  254.             if (!BP_CreateGlobals())
  255.                 return -1L;
  256.             break;
  257.         case WM_DESTROY:
  258.             BP_DestroyGlobals();
  259.             break;
  260.         case WM_SETBACKINFO:
  261.             if (g_hbmPreview)
  262.             {
  263.                 BuildPreviewBitmap(hWnd, wParam);
  264.                 g_bInfoSet = TRUE;
  265.                 // only invalidate the "screen" part of the monitor bitmap
  266.                 GetObject(g_hbmPreview, sizeof(bm), &bm);
  267.                 GetClientRect(hWnd, &rc);
  268.                 rc.left = ( rc.right - bm.bmWidth ) / 2 + MON_X;
  269.                 rc.top = ( rc.bottom - bm.bmHeight ) / 2 + MON_Y;
  270.                 rc.right = rc.left + MON_DX;
  271.                 rc.bottom = rc.top + MON_DY;
  272.                 InvalidateRect(hWnd, &rc, FALSE);
  273.             }
  274.             break;
  275.         case WM_PALETTECHANGED:
  276.             if ((HWND)wParam == hWnd)
  277.                 break;
  278.             //fallthru
  279.         case WM_QUERYNEWPALETTE:
  280.             if (g_hpalWall)
  281.                 InvalidateRect(hWnd, NULL, FALSE);
  282.             break;
  283.         case WM_PAINT:
  284.             BeginPaint(hWnd,&ps);
  285.             if (g_hbmPreview && g_bInfoSet)
  286.             {
  287.                 hbmOld = SelectObject(g_hdcMem, g_hbmPreview);
  288.                 if (g_hpalWall)
  289.                 {
  290.                     hpalOld = SelectPalette(ps.hdc, g_hpalWall, FALSE);
  291.                     RealizePalette(ps.hdc);
  292.                 }
  293.                 GetObject(g_hbmPreview, sizeof(bm), &bm);
  294.                 GetClientRect(hWnd, &rc);
  295.                 rc.left = ( rc.right - bm.bmWidth ) / 2;
  296.                 rc.top = ( rc.bottom - bm.bmHeight ) / 2;
  297.                 BitBlt(ps.hdc, rc.left, rc.top, bm.bmWidth, bm.bmHeight, g_hdcMem,
  298.                     0, 0, SRCCOPY);
  299.                 if (g_hpalWall)
  300.                 {
  301.                     SelectPalette(ps.hdc, hpalOld, TRUE);
  302.                     RealizePalette(ps.hdc);
  303.                 }
  304.                 SelectObject(g_hdcMem, hbmOld);
  305.             }
  306.             EndPaint(hWnd,&ps);
  307.             return 0;
  308.     }
  309.     return DefWindowProc(hWnd,message,wParam,lParam);
  310. }
  311. BOOL FAR PASCAL RegisterBackPreviewClass(HINSTANCE hInst)
  312. {
  313.     WNDCLASS wc;
  314.     if (!GetClassInfo(hInst, BACKPREV_CLASS, &wc)) {
  315.         wc.style = 0;
  316.         wc.lpfnWndProc = BackPreviewWndProc;
  317.         wc.cbClsExtra = 0;
  318.         wc.cbWndExtra = 0;
  319.         wc.hInstance = hInst;
  320.         wc.hIcon = NULL;
  321.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  322.         wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  323.         wc.lpszMenuName = NULL;
  324.         wc.lpszClassName = BACKPREV_CLASS;
  325.         if (!RegisterClass(&wc))
  326.             return FALSE;
  327.     }
  328.     return TRUE;
  329. }