dbackp.cpp
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 17k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /*  DBACKP.CPP
  2. **
  3. **  Copyright (C) Microsoft, 1997, 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 "stdafx.h"
  14. #pragma hdrstop
  15. #ifdef POSTSPLIT
  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_hbmDefault = NULL;   // default bitmap
  21. HBITMAP  g_hbmWall = NULL;      // bitmap image of wallpaper
  22. HDC      g_hdcWall = NULL;      // memory DC with g_hbmWall selected
  23. HDC      g_hdcMemory = NULL;    // memory DC
  24. HPALETTE g_hpalWall = NULL;     // palette that goes with hbmWall bitmap
  25. HBRUSH   g_hbrBack = NULL;      // brush for the desktop background
  26. IThumbnail *g_pthumb = NULL;    // Html to Bitmap converter
  27. DWORD    g_dwWallpaperID = 0;   // ID to identify which bitmap we received
  28. #define WM_HTML_BITMAP  (WM_USER + 100)
  29. #define WM_ASYNC_BITMAP (WM_HTML_BITMAP + 1)
  30. /*----------------------------------------------------------------------------*
  31. *----------------------------------------------------------------------------*/
  32. HPALETTE PaletteFromDS(HDC hdc)
  33. {
  34.     DWORD adw[257];
  35.     int i,n;
  36.     n = GetDIBColorTable(hdc, 0, 256, (LPRGBQUAD)&adw[1]);
  37.     adw[0] = MAKELONG(0x300, n);
  38.     for (i=1; i<=n; i++)
  39.         adw[i] = RGB(GetBValue(adw[i]),GetGValue(adw[i]),GetRValue(adw[i]));
  40.     if (n == 0)
  41.         return NULL;
  42.     else
  43.         return CreatePalette((LPLOGPALETTE)&adw[0]);
  44. }
  45. typedef struct{
  46.     HWND hwnd;
  47.     HBITMAP hbmp;
  48.     DWORD id;
  49.     WPARAM flags;
  50.     TCHAR szFile[MAX_PATH];
  51. } ASYNCWALLPARAM, * PASYNCWALLPARAM;
  52. DWORD CALLBACK UpdateWallProc(LPVOID pv)
  53. {
  54.     ASSERT(pv);
  55.     PASYNCWALLPARAM pawp = (PASYNCWALLPARAM) pv;
  56.     pawp->hbmp = (HBITMAP)LoadImage(NULL, pawp->szFile,
  57.                                       IMAGE_BITMAP, 0, 0,
  58.                                       LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  59.     if (pawp->hbmp)
  60.     {
  61.         // if all is good, then the window will handle cleaning up
  62.         if (IsWindow(pawp->hwnd) && PostMessage(pawp->hwnd, WM_ASYNC_BITMAP, 0, (LPARAM)pawp))
  63.             return TRUE;
  64.         DeleteObject(pawp->hbmp);
  65.     }
  66.     LocalFree(pawp);
  67.     return TRUE;
  68. }
  69. void LoadWallpaperAsync(LPCTSTR pszFile, HWND hwnd, DWORD dwID, WPARAM flags)
  70. {
  71.     PASYNCWALLPARAM pawp = (PASYNCWALLPARAM) LocalAlloc(LPTR, SIZEOF(ASYNCWALLPARAM));
  72.     if (pawp)
  73.     {
  74.         pawp->hwnd = hwnd;
  75.         pawp->flags = flags;
  76.         pawp->id = dwID;
  77.         StrCpyN(pawp->szFile, pszFile, SIZECHARS(pawp->szFile));
  78.         if (!SHQueueUserWorkItem(UpdateWallProc, pawp, 0, (DWORD_PTR)0, (DWORD_PTR *)NULL, NULL, 0))
  79.             LocalFree(pawp);
  80.     }
  81. }
  82. void _InitPreview(void)
  83. {
  84.     if( g_hbmPreview )
  85.         DeleteObject( g_hbmPreview );
  86.     g_hbmPreview = LoadMonitorBitmap();
  87. }
  88. void _BuildPattern(void)
  89. {
  90.     WCHAR wszBuf[MAX_PATH];
  91.     HBITMAP hbmTemp;
  92.     COLORREF clrOldBk, clrOldText;
  93.     HBRUSH hbr = NULL;
  94.     WORD patbits[CXYDESKPATTERN] = {0, 0, 0, 0, 0, 0, 0, 0};
  95.     // get rid of old brush if there was one
  96.     if (g_hbrBack)
  97.         DeleteObject(g_hbrBack);
  98.     g_pActiveDesk->GetPattern(wszBuf, ARRAYSIZE(wszBuf), 0);
  99.     if (wszBuf[0] != 0L)
  100.     {
  101.         LPTSTR   pszPatternBuf;
  102. #ifndef UNICODE
  103.         CHAR    szTemp[MAX_PATH];
  104.         SHUnicodeToAnsi(wszBuf, szTemp, ARRAYSIZE(szTemp));
  105.         pszPatternBuf = szTemp;
  106. #else
  107.         pszPatternBuf = wszBuf;
  108. #endif
  109.         PatternToWords(pszPatternBuf, patbits);
  110.         hbmTemp = CreateBitmap(8, 8, 1, 1, patbits);
  111.         if (hbmTemp)
  112.         {
  113.             g_hbrBack = CreatePatternBrush(hbmTemp);
  114.             DeleteObject(hbmTemp);
  115.         }
  116.     }
  117.     else
  118.     {
  119.         g_hbrBack = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
  120.     }
  121.     if (!g_hbrBack)
  122.     {
  123.         g_hbrBack = (HBRUSH)GetStockObject(BLACK_BRUSH);
  124.     }
  125.     clrOldText = SetTextColor(g_hdcMemory, GetSysColor(COLOR_BACKGROUND));
  126.     clrOldBk = SetBkColor(g_hdcMemory, GetSysColor(COLOR_WINDOWTEXT));
  127.     hbr = (HBRUSH)SelectObject(g_hdcMemory, g_hbrBack);
  128.     PatBlt(g_hdcMemory, MON_X, MON_Y, MON_DX, MON_DY, PATCOPY);
  129.     SelectObject(g_hdcMemory, hbr);
  130.     SetTextColor(g_hdcMemory, clrOldText);
  131.     SetBkColor(g_hdcMemory, clrOldBk);
  132. }
  133. void _InitWall(void)
  134. {
  135.     if (g_hbmWall)
  136.     {
  137.         SelectObject(g_hdcWall, g_hbmDefault);
  138.         DeleteObject(g_hbmWall);
  139.         g_hbmWall = NULL;
  140.         if (g_hpalWall)
  141.         {
  142.             DeleteObject(g_hpalWall);
  143.             g_hpalWall = NULL;
  144.         }
  145.     }
  146. }
  147. void _GetWallpaperAsync(HWND hwnd, WPARAM flags)
  148. {
  149.     WCHAR wszWallpaper[INTERNET_MAX_URL_LENGTH];
  150.     LPTSTR pszWallpaper;
  151.     g_pActiveDesk->GetWallpaper(wszWallpaper, ARRAYSIZE(wszWallpaper), 0);
  152. #ifndef UNICODE
  153.     CHAR  szWallpaper[ARRAYSIZE(wszWallpaper)];
  154.     SHUnicodeToAnsi(wszWallpaper, szWallpaper, ARRAYSIZE(szWallpaper));
  155.     pszWallpaper = szWallpaper;
  156. #else
  157.     pszWallpaper = wszWallpaper;
  158. #endif
  159.     g_dwWallpaperID++;
  160.     if (!*pszWallpaper || !lstrcmpi(pszWallpaper, g_szNone))
  161.         return;
  162.     {
  163.         if (IsNormalWallpaper(pszWallpaper))
  164.         {
  165.             LoadWallpaperAsync(pszWallpaper, hwnd, g_dwWallpaperID, flags);
  166.         }
  167.         else
  168.         {
  169.             if(IsWallpaperPicture(pszWallpaper))
  170.             {
  171.                 // This is a picture (GIF, JPG etc.,)
  172.                 // We need to generate a small HTML file that has this picture
  173.                 // as the background image.
  174.                 //
  175.                 // Compute the filename for the Temporary HTML file.
  176.                 //
  177.                 GetTempPath(ARRAYSIZE(wszWallpaper), pszWallpaper);
  178.                 lstrcat(pszWallpaper, PREVIEW_PICTURE_FILENAME);
  179. #ifndef UNICODE
  180.                 SHAnsiToUnicode(szWallpaper, wszWallpaper, ARRAYSIZE(wszWallpaper));
  181. #endif
  182.                 //
  183.                 // Generate the preview picture html file.
  184.                 //
  185.                 g_pActiveDesk->GenerateDesktopItemHtml(wszWallpaper, NULL, 0);
  186.             }
  187.             //
  188.             // Will cause a WM_HTML_BITMAP to get sent to us.
  189.             //
  190.             g_pthumb->GetBitmap(wszWallpaper, g_dwWallpaperID, MON_DX, MON_DY);
  191.         }
  192.     }
  193. }
  194. void _DrawWall(HBITMAP hbm, WPARAM flags)
  195. {
  196.     int     dxWall;          // size of wallpaper
  197.     int     dyWall;
  198.     BITMAP bm;
  199.     //  init the global
  200.     g_hbmWall = hbm;
  201.     SelectObject(g_hdcWall, g_hbmWall); // bitmap stays in this DC
  202.     GetObject(g_hbmWall, sizeof(bm), &bm);
  203.     TraceMsg(TF_ALWAYS, "for bitmap %08X we have bpp=%d and planes=%d", g_hbmWall, bm.bmBitsPixel, bm.bmPlanes);
  204.     if (GetDeviceCaps(g_hdcMemory, RASTERCAPS) & RC_PALETTE)
  205.     {
  206.         if (bm.bmBitsPixel * bm.bmPlanes > 8)
  207.             g_hpalWall = CreateHalftonePalette(g_hdcMemory);
  208.         else if (bm.bmBitsPixel * bm.bmPlanes == 8)
  209.             g_hpalWall = PaletteFromDS(g_hdcWall);
  210.         else
  211.             g_hpalWall = NULL;  //!!! assume 1 or 4bpp images dont have palettes
  212.     }
  213.     GetObject(g_hbmWall, sizeof(bm), &bm);
  214.     if(flags & BP_EXTERNALWALL)
  215.     {
  216.         //For external wallpapers, we ask the image extractor to generate
  217.         // bitmaps the size that we want to show (NOT the screen size).
  218.         dxWall = MON_DX;
  219.         dyWall = MON_DY;
  220.     }
  221.     else
  222.     {
  223.         dxWall = MulDiv(bm.bmWidth, MON_DX, GetDeviceCaps(g_hdcMemory, HORZRES));
  224.         dyWall = MulDiv(bm.bmHeight, MON_DY, GetDeviceCaps(g_hdcMemory, VERTRES));
  225.     }
  226.     if (dxWall < 1) dxWall = 1;
  227.     if (dyWall < 1) dyWall = 1;
  228.     if (g_hpalWall)
  229.     {
  230.         SelectPalette(g_hdcMemory, g_hpalWall, TRUE);
  231.         RealizePalette(g_hdcMemory);
  232.     }
  233.     IntersectClipRect(g_hdcMemory, MON_X, MON_Y, MON_X + MON_DX, MON_Y + MON_DY);
  234.     SetStretchBltMode(g_hdcMemory, COLORONCOLOR);
  235.     if (flags & BP_TILE)
  236.     {
  237.         int i;
  238.         StretchBlt(g_hdcMemory, MON_X, MON_Y, dxWall, dyWall,
  239.             g_hdcWall, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  240.         for (i = MON_X+dxWall; i < (MON_X + MON_DX); i+= dxWall)
  241.             BitBlt(g_hdcMemory, i, MON_Y, dxWall, dyWall, g_hdcMemory, MON_X, MON_Y, SRCCOPY);
  242.         for (i = MON_Y; i < (MON_Y + MON_DY); i += dyWall)
  243.             BitBlt(g_hdcMemory, MON_X, i, MON_DX, dyWall, g_hdcMemory, MON_X, MON_Y, SRCCOPY);
  244.     }
  245.     else
  246.     {
  247.         //We want to stretch the Bitmap to the preview monitor size ONLY for new platforms.
  248.         if((flags & BP_STRETCH) && (g_bRunOnMemphis || g_bRunOnNT5))
  249.         {
  250.             //Stretch the bitmap to the whole preview monitor.
  251.             dxWall = MON_DX;
  252.             dyWall = MON_DY;
  253.         }
  254.         //Center the bitmap in the preview monitor
  255.         StretchBlt(g_hdcMemory, MON_X + (MON_DX - dxWall)/2, MON_Y + (MON_DY - dyWall)/2,
  256.                 dxWall, dyWall, g_hdcWall, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  257.     }
  258.     // restore dc
  259.     SelectPalette(g_hdcMemory, (HPALETTE)GetStockObject(DEFAULT_PALETTE), TRUE);
  260.     SelectClipRgn(g_hdcMemory, NULL);
  261. }
  262. /*--------------------------------------------------------------------
  263. ** Build the preview bitmap.
  264. **
  265. ** both the pattern and the bitmap are drawn each time, but
  266. ** if the flags dictate the need, new pattern and bitmap
  267. ** globals are built as needed.
  268. **--------------------------------------------------------------------*/
  269. void NEAR PASCAL BuildPreviewBitmap(HWND hwnd, HBITMAP hbmp, WPARAM flags)
  270. {
  271.     _InitPreview();
  272.     HBITMAP hbmOld = (HBITMAP)SelectObject(g_hdcMemory, g_hbmPreview);
  273.     _BuildPattern();
  274.     _InitWall();
  275.     /*
  276.     ** now, position the wallpaper appropriately
  277.     */
  278.     if (hbmp)
  279.     {
  280.         //  use the one that was passed in
  281.         _DrawWall(hbmp, flags);
  282.     }
  283.     else
  284.     {
  285.         //  this means that we need to set up the stuff
  286.         //  to get the bmp ASYNC
  287.         _GetWallpaperAsync(hwnd, flags);
  288.     }
  289.     SelectObject(g_hdcMemory, hbmOld);
  290. }
  291. BOOL NEAR PASCAL BP_CreateGlobals(HWND hwnd)
  292. {
  293.     HDC hdc;
  294.     hdc = GetDC(NULL);
  295.     g_hdcWall = CreateCompatibleDC(hdc);
  296.     g_hdcMemory = CreateCompatibleDC(hdc);
  297.     ReleaseDC(NULL, hdc);
  298.     g_hbmPreview = LoadMonitorBitmap();
  299.     HBITMAP hbm;
  300.     hbm = CreateBitmap(1, 1, 1, 1, NULL);
  301.     g_hbmDefault = (HBITMAP)SelectObject(g_hdcMemory, hbm);
  302.     SelectObject(g_hdcMemory, g_hbmDefault);
  303.     DeleteObject(hbm);
  304.     HRESULT hr = E_FAIL;
  305.     hr = CoCreateInstance(CLSID_Thumbnail, NULL, CLSCTX_INPROC_SERVER, IID_IThumbnail, (void **)&g_pthumb);
  306.     if(SUCCEEDED(hr))
  307.     {
  308.         g_pthumb->Init(hwnd, WM_HTML_BITMAP);
  309.     }
  310.     if (!g_hdcWall || !g_hbmPreview || !SUCCEEDED(hr))
  311.         return FALSE;
  312.     else
  313.         return TRUE;
  314. }
  315. void NEAR PASCAL BP_DestroyGlobals(void)
  316. {
  317.     if (g_hbmPreview)
  318.     {
  319.         DeleteObject(g_hbmPreview);
  320.         g_hbmPreview = NULL;
  321.     }
  322.     if (g_hbmWall)
  323.     {
  324.         SelectObject(g_hdcWall, g_hbmDefault);
  325.         DeleteObject(g_hbmWall);
  326.         g_hbmWall = NULL;
  327.     }
  328.     if (g_hpalWall)
  329.     {
  330.         SelectPalette(g_hdcWall, (HPALETTE)GetStockObject(DEFAULT_PALETTE), TRUE);
  331.         DeleteObject(g_hpalWall);
  332.         g_hpalWall = NULL;
  333.     }
  334.     if (g_hdcWall)
  335.     {
  336.         DeleteDC(g_hdcWall);
  337.         g_hdcWall = NULL;
  338.     }
  339.     if (g_hbrBack)
  340.     {
  341.         DeleteObject(g_hbrBack);
  342.         g_hbrBack = NULL;
  343.     }
  344.     if (g_hdcMemory)
  345.     {
  346.         DeleteDC(g_hdcMemory);
  347.         g_hdcMemory = NULL;
  348.     }
  349.     if (g_hbmDefault)
  350.     {
  351.         DeleteObject(g_hbmDefault);
  352.         g_hbmDefault = NULL;
  353.     }
  354.     if (g_pthumb)
  355.     {
  356.         g_pthumb->Release();
  357.         g_pthumb = NULL;
  358.     }
  359. }
  360. void InvalidateBackPrevContents(HWND hwnd)
  361. {
  362.     BITMAP bm;
  363.     RECT rc;
  364.     //
  365.     // Only invalidate the "screen" part of the monitor bitmap.
  366.     //
  367.     GetObject(g_hbmPreview, SIZEOF(bm), &bm);
  368.     GetClientRect(hwnd, &rc);
  369.     rc.left = ( rc.right - bm.bmWidth ) / 2 + MON_X;
  370.     rc.top = ( rc.bottom - bm.bmHeight ) / 2 + MON_Y;
  371.     rc.right = rc.left + MON_DX;
  372.     rc.bottom = rc.top + MON_DY;
  373.     InvalidateRect(hwnd, &rc, FALSE);
  374. }
  375. LRESULT BackPreviewWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  376. {
  377.     PAINTSTRUCT     ps;
  378.     BITMAP          bm;
  379.     RECT            rc;
  380.     HBITMAP         hbmOld;
  381.     HPALETTE        hpalOld;
  382.     switch(message)
  383.     {
  384.         case WM_CREATE:
  385.             if (!BP_CreateGlobals(hWnd))
  386.                 return -1L;
  387.             break;
  388.         case WM_DESTROY:
  389.             MSG msg;
  390.             BP_DestroyGlobals();
  391.             while (PeekMessage(&msg, hWnd, WM_HTML_BITMAP, WM_ASYNC_BITMAP, PM_REMOVE))
  392.             {
  393.                 if ( msg.lParam )
  394.                 {
  395.                     if (msg.message == WM_ASYNC_BITMAP)
  396.                     {
  397.                         //  clean up this useless crap
  398.                         DeleteObject(((PASYNCWALLPARAM)(msg.lParam))->hbmp);
  399.                         LocalFree((PASYNCWALLPARAM)(msg.lParam));
  400.                     }
  401.                     else // WM_HTML_BITMAP
  402.                         DeleteObject((HBITMAP)msg.lParam);
  403.                 }
  404.             }
  405.             break;
  406.         case WM_SETBACKINFO:
  407.             if (g_hbmPreview)
  408.             {
  409.                 BuildPreviewBitmap(hWnd, NULL, wParam);
  410.                 g_bInfoSet = TRUE;
  411.                 InvalidateBackPrevContents(hWnd);
  412.             }
  413.             break;
  414.         case WM_ASYNC_BITMAP:
  415.             if (lParam)
  416.             {
  417.                 PASYNCWALLPARAM pawp = (PASYNCWALLPARAM) lParam;
  418.                 ASSERT(pawp->hbmp);
  419.                 if (pawp->id == g_dwWallpaperID)
  420.                 {
  421.                     BuildPreviewBitmap(hWnd, pawp->hbmp, pawp->flags);
  422.                     InvalidateBackPrevContents(hWnd);
  423.                 }
  424.                 else
  425.                 {
  426.                     //  clean up this useless crap
  427.                     DeleteObject(pawp->hbmp);
  428.                     LocalFree(pawp);
  429.                 }
  430.             }
  431.             break;
  432.         case WM_HTML_BITMAP:
  433.             // may come through with NULL if the image extraction failed....
  434.             if (wParam == g_dwWallpaperID && lParam)
  435.             {
  436.                 BuildPreviewBitmap(hWnd, (HBITMAP)lParam, BP_EXTERNALWALL);
  437.                 InvalidateBackPrevContents(hWnd);
  438.             }
  439.             else
  440.             {
  441.                 // Bitmap for something no longer selected
  442.                 DeleteObject((HBITMAP)lParam);
  443.             }
  444.             break;
  445.         case WM_PALETTECHANGED:
  446.             if ((HWND)wParam == hWnd)
  447.                 break;
  448.             //fallthru
  449.         case WM_QUERYNEWPALETTE:
  450.             if (g_hpalWall)
  451.                 InvalidateRect(hWnd, NULL, FALSE);
  452.             break;
  453.         case WM_PAINT:
  454.             BeginPaint(hWnd,&ps);
  455.             if (g_hbmPreview && g_bInfoSet)
  456.             {
  457.                 hbmOld = (HBITMAP)SelectObject(g_hdcMemory, g_hbmPreview);
  458.                 if (g_hpalWall)
  459.                 {
  460.                     hpalOld = SelectPalette(ps.hdc, g_hpalWall, FALSE);
  461.                     RealizePalette(ps.hdc);
  462.                 }
  463.                 GetObject(g_hbmPreview, sizeof(bm), &bm);
  464.                 GetClientRect(hWnd, &rc);
  465.                 rc.left = ( rc.right - bm.bmWidth ) / 2;
  466.                 rc.top = ( rc.bottom - bm.bmHeight ) / 2;
  467.                 BitBlt(ps.hdc, rc.left, rc.top, bm.bmWidth, bm.bmHeight, g_hdcMemory,
  468.                     0, 0, SRCCOPY);
  469.                 if (g_hpalWall)
  470.                 {
  471.                     SelectPalette(ps.hdc, hpalOld, TRUE);
  472.                     RealizePalette(ps.hdc);
  473.                 }
  474.                 SelectObject(g_hdcMemory, hbmOld);
  475.             }
  476.             EndPaint(hWnd,&ps);
  477.             return 0;
  478.     }
  479.     return DefWindowProc(hWnd,message,wParam,lParam);
  480. }
  481. BOOL RegisterBackPreviewClass()
  482. {
  483.     WNDCLASS wc;
  484.     if (!GetClassInfo(HINST_THISDLL, c_szBackgroundPreview2, &wc)) {
  485.         wc.style = 0;
  486.         wc.lpfnWndProc = BackPreviewWndProc;
  487.         wc.cbClsExtra = 0;
  488.         wc.cbWndExtra = 0;
  489.         wc.hInstance = HINST_THISDLL;
  490.         wc.hIcon = NULL;
  491.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  492.         wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  493.         wc.lpszMenuName = NULL;
  494.         wc.lpszClassName = c_szBackgroundPreview2;
  495.         if (!RegisterClass(&wc))
  496.             return FALSE;
  497.     }
  498.     return TRUE;
  499. }
  500. #endif