pattern.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 10k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. //#include <shellids.h>
  4. //#include "pattern.h"
  5. //#include "deskstat.h"
  6. //#include "dutil.h"
  7. //#include "editpat.h"
  8. //#include "resource.h"
  9. #include <mluisupp.h>
  10. #define THISCLASS CPattern
  11. #define SetDefaultDialogFont SHSetDefaultDialogFont
  12. #define RemoveDefaultDialogFont SHRemoveDefaultDialogFont
  13. #define c_szHelpFile    TEXT("Update.hlp")
  14. const static DWORD aPatternHelpIDs[] = {  // Context Help IDs
  15.     IDC_PAT_PATTERN,    IDH_DISPLAY_PATTERN,
  16.     IDC_PAT_LIST,       IDH_DISPLAY_PATTERN,
  17.     IDC_PAT_PREVIEW,    IDH_DISPLAY_PATTERN,
  18.     IDC_PAT_SAMPLE,     IDH_DISPLAY_PATTERN,
  19.     IDC_PAT_EDIT,       IDH_EDIT_PATTERN,
  20.     0, 0
  21. };
  22. THISCLASS::CPattern(void)
  23. {
  24. }
  25. LPTSTR GetSection(LPCTSTR pszIniFile, LPCTSTR pszSection)
  26. {
  27.     BOOL fDone = FALSE;
  28.     int cchBuf = 4096;
  29.     LPTSTR pszBuf = (LPTSTR)LocalAlloc(LPTR, cchBuf * SIZEOF(TCHAR));
  30.     while (pszBuf && !fDone)
  31.     {
  32.         int cchRead = GetPrivateProfileString(pszSection, NULL, c_szNULL, pszBuf, cchBuf, pszIniFile);
  33.         if (cchRead > cchBuf-2)
  34.         {
  35.             //
  36.             // Need to grow the buffer.
  37.             //
  38.             cchBuf += 2048;
  39.             LPTSTR pszTemp = pszBuf;
  40.             pszBuf = (LPTSTR)LocalReAlloc((HANDLE)pszBuf, cchBuf * SIZEOF(TCHAR), LMEM_MOVEABLE);
  41.             if (pszBuf == NULL)
  42.             {
  43.                 LocalFree(pszTemp);
  44.             }
  45.         }
  46.         else
  47.         {
  48.             fDone = TRUE;
  49.         }
  50.     }
  51.     return pszBuf;
  52. }
  53. void THISCLASS::_OnInitDialog(HWND hwnd)
  54. {
  55.     //Set the font that can display the strings in the native language.
  56.     SetDefaultDialogFont(hwnd, IDC_PAT_LIST);
  57.     _hwnd = hwnd;
  58.     _hwndLB = GetDlgItem(_hwnd, IDC_PAT_LIST);
  59.     _hwndSample = GetDlgItem(_hwnd, IDC_PAT_SAMPLE);
  60.     _szCurPattern[0] = TEXT('');
  61.     WCHAR   wszCurPatBits[MAX_PATH];
  62.     LPTSTR  pszCurPatBits;
  63.     g_pActiveDesk->GetPattern(wszCurPatBits, ARRAYSIZE(wszCurPatBits), 0);
  64. #ifndef UNICODE
  65.     CHAR    szCurPatBits[MAX_PATH];
  66.     SHUnicodeToAnsi(wszCurPatBits, szCurPatBits, ARRAYSIZE(szCurPatBits));
  67.     pszCurPatBits = szCurPatBits;
  68. #else
  69.     pszCurPatBits = wszCurPatBits;
  70. #endif
  71.     //
  72.     // Populate the listbox.
  73.     //
  74.     LPTSTR pszPatterns = GetSection(c_szControlIni, c_szPatterns);
  75.     if (pszPatterns)
  76.     {
  77.         BOOL fAddedNone = FALSE;
  78.         for (; *pszPatterns; pszPatterns+=lstrlen(pszPatterns)+1)
  79.         {
  80.             TCHAR szBuf[MAX_PATH];
  81.             if (GetPrivateProfileString(c_szPatterns, pszPatterns, c_szNULL, szBuf, ARRAYSIZE(szBuf), c_szControlIni))
  82.             {
  83.                 BOOL fIsNone = !fAddedNone && (lstrcmpi(g_szNone, szBuf) == 0);
  84.                 //
  85.                 // If there's a right-hand side, add it to the list box.
  86.                 //
  87.                 if (fIsNone || IsValidPattern(szBuf))
  88.                 {
  89.                     if (fIsNone)
  90.                     {
  91.                         fAddedNone = TRUE;
  92.                     }
  93.                     SendMessage(_hwndLB, LB_ADDSTRING, 0, (LPARAM)pszPatterns);
  94.                     //
  95.                     // If we haven't found current pattern name, maybe this is it.
  96.                     //
  97.                     if ((_szCurPattern[0] == TEXT('')) && (lstrcmpi(szBuf, pszCurPatBits) == 0))
  98.                     {
  99.                         //
  100.                         // Same pattern bits.  We have a name.
  101.                         //
  102.                         lstrcpy(_szCurPattern, pszPatterns);
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.         LocalFree((HANDLE)pszPatterns);
  108.     }
  109.     //
  110.     // If our pattern's bits weren't in the list, use a fake name.
  111.     //
  112.     if (_szCurPattern[0] == TEXT(''))
  113.     {
  114.         MLLoadString(IDS_PAT_UNLISTED, _szCurPattern, ARRAYSIZE(_szCurPattern));
  115.     }
  116.     //
  117.     // Select the current pattern.
  118.     //
  119.     SendMessage(_hwndLB, LB_SELECTSTRING, (WPARAM)-1, (LPARAM)_szCurPattern);
  120.     //
  121.     // Enable all necessary UI
  122.     //
  123.     _EnableControls();
  124. }
  125. void THISCLASS::_GetPattern(LPTSTR pszPattern, int cchPattern)
  126. {
  127.     int iSel = ListBox_GetCurSel(_hwndLB);
  128.     if (iSel != LB_ERR)
  129.     {
  130.         TCHAR szPatternName[MAX_PATH];
  131.         ListBox_GetText(_hwndLB, iSel, szPatternName);
  132.         GetPrivateProfileString(c_szPatterns, szPatternName, c_szNULL, pszPattern, cchPattern, c_szControlIni);
  133.         if (IsValidPattern(pszPattern) == FALSE)
  134.         {
  135.             pszPattern[0] = TEXT('');
  136.         }
  137.     }
  138.     else
  139.     {
  140.         pszPattern[0] = TEXT('');
  141.     }
  142. }
  143. void THISCLASS::_EnableControls(void)
  144. {
  145.     //
  146.     // Pattern button enabled only when a non-none pattern is selected.
  147.     //
  148.     EnableWindow(GetDlgItem(_hwnd, IDC_PAT_EDIT), ListBox_GetCurSel(_hwndLB) > 0);
  149. }
  150. void THISCLASS::_OnCommand(WORD wNotifyCode, WORD wID, HWND hwndCtl)
  151. {
  152.     int iSel;
  153.     switch (wID)
  154.     {
  155.     case IDC_PAT_LIST:
  156.         switch (wNotifyCode)
  157.         {
  158.         case LBN_SELCHANGE:
  159.             RECT rectSample;
  160.             GetWindowRect(_hwndSample, &rectSample);
  161.             rectSample.left++; rectSample.top++; rectSample.right--; rectSample.bottom--;
  162.             // Use MapWindowPoints instead of ScreenToClient 
  163.             // because it works on mirrored windows and on non mirrored windows.
  164.             MapWindowPoints(NULL, _hwnd, (LPPOINT) &rectSample, 2);
  165.             InvalidateRect(_hwnd, &rectSample, FALSE);
  166.             _EnableControls();
  167.             break;
  168.         }
  169.         break;
  170.     case IDOK:
  171.         TCHAR szPattern[MAX_PATH];
  172.         LPWSTR pwszPattern;
  173.         iSel = ListBox_GetCurSel(_hwndLB);
  174.         if (iSel != LB_ERR)
  175.         {
  176.             _GetPattern(szPattern, ARRAYSIZE(szPattern));
  177. #ifndef UNICODE
  178.             WCHAR   wszPattern[MAX_PATH];
  179.             SHAnsiToUnicode(szPattern, wszPattern, ARRAYSIZE(wszPattern));
  180.             pwszPattern = wszPattern;
  181. #else
  182.             pwszPattern = (LPWSTR)szPattern;
  183. #endif
  184.             g_pActiveDesk->SetPattern(pwszPattern, 0);
  185.             EndDialog(_hwnd, 0);
  186.         }
  187.         break;
  188.     case IDCANCEL:
  189.         EndDialog(_hwnd, -1);
  190.         break;
  191.     case IDC_PAT_EDIT:
  192.         iSel = ListBox_GetCurSel(_hwndLB);
  193.         if (iSel > 0)
  194.         {
  195.             DialogBoxParam(MLGetHinst(), MAKEINTRESOURCE(IDD_EDITPAT),
  196.                 _hwnd, EditPatDlgProc, (LPARAM)_hwndLB);
  197.         }
  198.         _OnCommand(LBN_SELCHANGE, IDC_PAT_LIST, _hwndLB);
  199.         break;
  200.     }
  201. }
  202. HBRUSH THISCLASS::_WordsToBrush(WORD *pwBits)
  203. {
  204.     HBRUSH hbrushRet = NULL;
  205.     HBITMAP hbmDesktop = CreateBitmap(CXYDESKPATTERN, CXYDESKPATTERN, 1, 1, pwBits);
  206.     if (hbmDesktop)
  207.     {
  208.         HDC hdcScreen = GetDC(_hwnd);
  209.         HDC hdcMemSrc = CreateCompatibleDC(hdcScreen);
  210.         if (hdcMemSrc)
  211.         {
  212.             SelectObject(hdcMemSrc, hbmDesktop);
  213.             HBITMAP hbmMem = CreateCompatibleBitmap(hdcScreen, CXYDESKPATTERN, CXYDESKPATTERN);
  214.             if (hbmMem)
  215.             {
  216.                 HDC hdcMemDest = CreateCompatibleDC(hdcScreen);
  217.                 if (hdcMemDest)
  218.                 {
  219.                     SelectObject(hdcMemDest, hbmMem);
  220.                     SetTextColor(hdcMemDest, GetSysColor(COLOR_BACKGROUND));
  221.                     SetBkColor(hdcMemDest, GetSysColor(COLOR_WINDOWTEXT));
  222.                     BitBlt(hdcMemDest, 0, 0, CXYDESKPATTERN, CXYDESKPATTERN,
  223.                             hdcMemSrc, 0, 0, SRCCOPY);
  224.                     hbrushRet = CreatePatternBrush(hbmMem);
  225.                     DeleteDC(hdcMemDest);
  226.                 }
  227.                 DeleteObject(hbmMem);
  228.             }
  229.             DeleteDC(hdcMemSrc);
  230.         }
  231.         ReleaseDC(_hwnd, hdcScreen);
  232.         DeleteObject(hbmDesktop);
  233.     }
  234.     return hbrushRet;
  235. }
  236. void THISCLASS::_OnPaint(void)
  237. {
  238.     PAINTSTRUCT ps;
  239.     BeginPaint(_hwnd, &ps);
  240.     int iOldBkMode = SetBkMode(ps.hdc, TRANSPARENT);
  241.     RECT rectSample;
  242.     GetWindowRect(_hwndSample, &rectSample);
  243.     rectSample.left++; rectSample.top++; rectSample.right--; rectSample.bottom--;
  244.     // Use MapWindowPoints instead of ScreenToClient 
  245.     // because it works on mirrored windows and on non mirrored windows.
  246.     MapWindowPoints(NULL, _hwnd, (LPPOINT) &rectSample, 2);
  247.     RECT rectPaint;
  248.     if (IntersectRect(&rectPaint, &ps.rcPaint, &rectSample))
  249.     {
  250.         TCHAR szPattern[MAX_PATH];
  251.         _GetPattern(szPattern, ARRAYSIZE(szPattern));
  252.         WORD awPattern[8];
  253.         PatternToWords(szPattern, awPattern);
  254.         HBRUSH hbrPattern = _WordsToBrush(awPattern);
  255.         if (hbrPattern)
  256.         {
  257.             FillRect(ps.hdc, &rectPaint, hbrPattern);
  258.             DeleteObject(hbrPattern);
  259.         }
  260.     }
  261.     SetBkMode(ps.hdc, iOldBkMode);
  262.     EndPaint(_hwnd, &ps);
  263. }
  264. BOOL CALLBACK PatternDlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  265. {
  266.     BOOL fRet = FALSE;
  267.     CPattern *ppat = (CPattern *)GetWindowLong(hdlg, DWL_USER);
  268.     switch (uMsg)
  269.     {
  270.     case WM_INITDIALOG:
  271.         ppat = new CPattern();
  272.         if (ppat)
  273.         {
  274.             SetWindowLong(hdlg, DWL_USER, (LONG)ppat);
  275.         }
  276.         else
  277.         {
  278.             EndDialog(hdlg, -1);
  279.         }
  280.         ppat->_OnInitDialog(hdlg);
  281.         break;
  282.     case WM_COMMAND:
  283.         ppat->_OnCommand(HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
  284.         break;
  285.     case WM_PAINT:
  286.         ppat->_OnPaint();
  287.         break;
  288.     case WM_HELP:
  289.         SHWinHelpOnDemandWrap((HWND)((LPHELPINFO) lParam)->hItemHandle, c_szHelpFile,
  290.                 HELP_WM_HELP, (DWORD)aPatternHelpIDs);
  291.         break;
  292.     case WM_CONTEXTMENU:
  293.         SHWinHelpOnDemandWrap((HWND) wParam, c_szHelpFile, HELP_CONTEXTMENU,
  294.                 (DWORD)(LPVOID) aPatternHelpIDs);
  295.         break;
  296.     case WM_DESTROY:
  297.         RemoveDefaultDialogFont(hdlg);
  298.         break;
  299.     }
  300.     return fRet;
  301. }