MainFrm.cpp
上传用户:jianghp13
上传日期:2020-02-03
资源大小:148k
文件大小:25k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "DesktopCalendar.h"
  5. #include "Calendar.h"
  6. #include "OptionsDlg.h"
  7. #include "MainFrm.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. # define WS_EX_LAYERED 0x80000 
  14. # define LWA_ALPHA 2 
  15. # define LWA_COLORKEY 1 
  16. HBITMAP Create24BPPDIBSection(HDC hDC, int iWidth, int iHeight)
  17. {
  18.     BITMAPINFO bmi;
  19.     HBITMAP hbm;
  20.     LPBYTE pBits;
  21.     // Initialize header to 0s.
  22.     ZeroMemory(&bmi, sizeof(bmi));
  23.     // Fill out the fields you care about.
  24.     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  25.     bmi.bmiHeader.biWidth = iWidth;
  26.     bmi.bmiHeader.biHeight = iHeight;
  27.     bmi.bmiHeader.biPlanes = 1;
  28.     bmi.bmiHeader.biBitCount = 24;
  29.     bmi.bmiHeader.biCompression = BI_RGB;
  30.     // Create the surface.
  31.     hbm = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS,(void **)&pBits, NULL, 0);
  32.       
  33.     return(hbm);
  34. }
  35. BOOL BitmapsCompatible(LPBITMAP lpbm1, LPBITMAP lpbm2)
  36. {
  37.     if (lpbm1->bmBitsPixel != lpbm2->bmBitsPixel) 
  38. return FALSE;
  39.     if (lpbm1->bmPlanes    != lpbm2->bmPlanes)    
  40. return FALSE;
  41.     if (lpbm1->bmWidth     != lpbm2->bmWidth)     
  42. return FALSE;
  43.     if (lpbm1->bmHeight    != lpbm2->bmHeight)    
  44. return FALSE;
  45.     return TRUE;
  46. }
  47. BOOL BlendImages(HBITMAP hbmSrc1, HBITMAP hbmSrc2, HBITMAP hbmDst, DWORD dwWeight1)
  48. {
  49.     BITMAP bmSrc1, bmSrc2, bmDst;
  50.     RGBTRIPLE *lprgbSrc1, *lprgbSrc2, *lprgbDst;
  51.     DWORD dwWidthBytes, dwWeight2;
  52.     int x, y;
  53.     
  54.     // Only values between 0 and 255 are valid.
  55.     if (dwWeight1 > 255) return FALSE;
  56.     
  57.     // Get weighting value for second source image.
  58.     dwWeight2 = 255-dwWeight1;
  59.     
  60.     // Get information about the surfaces you were passed.
  61.     if (!GetObject(hbmSrc1, sizeof(BITMAP), &bmSrc1)) return FALSE;
  62.     if (!GetObject(hbmSrc2, sizeof(BITMAP), &bmSrc2)) return FALSE;
  63.     if (!GetObject(hbmDst,  sizeof(BITMAP), &bmDst))  return FALSE;
  64.     // Make sure you have data that meets your requirements.
  65.     if (!BitmapsCompatible(&bmSrc1, &bmSrc2))
  66. return FALSE;
  67.     if (!BitmapsCompatible(&bmSrc1, &bmDst))
  68. return FALSE;
  69.     if (bmSrc1.bmBitsPixel != 24)
  70. return FALSE;
  71.     if (bmSrc1.bmPlanes != 1)
  72. return FALSE;
  73.     if (!bmSrc1.bmBits || !bmSrc2.bmBits || !bmDst.bmBits)
  74. return FALSE;
  75.     dwWidthBytes = bmDst.bmWidthBytes;
  76.     
  77.     // Initialize the surface pointers.
  78.     lprgbSrc1 = (RGBTRIPLE *)bmSrc1.bmBits; 
  79.     lprgbSrc2 = (RGBTRIPLE *)bmSrc2.bmBits;  
  80.     lprgbDst  = (RGBTRIPLE *)bmDst.bmBits;
  81.     for (y=0; y<bmDst.bmHeight; y++) {
  82.         for (x=0; x<bmDst.bmWidth; x++) {
  83.             lprgbDst[x].rgbtRed   = (BYTE)((((DWORD)lprgbSrc1[x].rgbtRed * dwWeight1) + 
  84.                                             ((DWORD)lprgbSrc2[x].rgbtRed * dwWeight2)) >> 8);
  85.             lprgbDst[x].rgbtGreen = (BYTE)((((DWORD)lprgbSrc1[x].rgbtGreen * dwWeight1) + 
  86.                                             ((DWORD)lprgbSrc2[x].rgbtGreen * dwWeight2)) >> 8);
  87.             lprgbDst[x].rgbtBlue  = (BYTE)((((DWORD)lprgbSrc1[x].rgbtBlue * dwWeight1) + 
  88.                                             ((DWORD)lprgbSrc2[x].rgbtBlue * dwWeight2)) >> 8);
  89.         }
  90.         // Move to next scan line.
  91.         lprgbSrc1 = (RGBTRIPLE *)((LPBYTE)lprgbSrc1 + dwWidthBytes);
  92.         lprgbSrc2 = (RGBTRIPLE *)((LPBYTE)lprgbSrc2 + dwWidthBytes);
  93.         lprgbDst  = (RGBTRIPLE *)((LPBYTE)lprgbDst  + dwWidthBytes);
  94.     }
  95.     return TRUE;
  96. }
  97. BOOL DoAlphaBlend(
  98.   HDC hdcDest,                 // Handle to destination DC.
  99.   int nXOriginDest,            // X-coord of upper-left corner.
  100.   int nYOriginDest,            // Y-coord of upper-left corner.
  101.   int nWidthDest,              // Destination width.
  102.   int nHeightDest,             // Destination height.
  103.   HDC hdcSrc,                  // Handle to source DC.
  104.   int nXOriginSrc,             // X-coord of upper-left corner.
  105.   int nYOriginSrc,             // Y-coord of upper-left corner.
  106.   int nWidthSrc,               // Source width.
  107.   int nHeightSrc,              // Source height.
  108.   DWORD dwSourceWeight)        // Source weighting (between 0 and 255).
  109. {
  110.     HDC      hdcSrc1 = NULL;
  111.     HDC      hdcSrc2 = NULL;
  112.     HDC      hdcDst  = NULL;
  113.     HBITMAP  hbmSrc1 = NULL;
  114.     HBITMAP  hbmSrc2 = NULL;
  115.     HBITMAP  hbmDst  = NULL;
  116.     BOOL     bReturn;
  117.     // Create surfaces for sources and destination images.
  118.     hbmSrc1 = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
  119.     if (!hbmSrc1) goto HANDLEERROR;
  120.     hbmSrc2 = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
  121.     if (!hbmSrc2) goto HANDLEERROR;
  122.     hbmDst  = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
  123.     if (!hbmDst) goto HANDLEERROR;
  124.     
  125.     // Create HDCs to hold our surfaces.
  126.     hdcSrc1 = CreateCompatibleDC(hdcDest);
  127.     if (!hdcSrc1) goto HANDLEERROR;
  128.     
  129.     hdcSrc2 = CreateCompatibleDC(hdcDest);
  130.     if (!hdcSrc2) goto HANDLEERROR;
  131.     
  132.     hdcDst  = CreateCompatibleDC(hdcDest);
  133.     if (!hdcDst) goto HANDLEERROR;
  134.     
  135.     // Prepare the surfaces for drawing.
  136.     SelectObject(hdcSrc1, hbmSrc1);
  137.     SelectObject(hdcSrc2, hbmSrc2);
  138.     SelectObject(hdcDst,  hbmDst);
  139.     SetStretchBltMode(hdcSrc1, COLORONCOLOR);
  140.     SetStretchBltMode(hdcSrc2, COLORONCOLOR);
  141.     
  142.     // Capture a copy of the source area.
  143.     if (!StretchBlt(hdcSrc1, 0,0,nWidthDest,nHeightDest,
  144.                     hdcSrc,  nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, 
  145.                     SRCCOPY))
  146.          goto HANDLEERROR;
  147.     
  148.     // Capture a copy of the destination area.
  149.     if (!StretchBlt(hdcSrc2, 0,0,nWidthDest,nHeightDest,
  150.                     hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, 
  151.                     SRCCOPY))
  152.          goto HANDLEERROR;
  153.                   
  154.     // Blend the two source areas to create the destination image.
  155.     bReturn = BlendImages(hbmSrc1, hbmSrc2, hbmDst, dwSourceWeight);
  156.     
  157.     // Clean up objects you do not need any longer.
  158.     // You cannot delete an object that's selected into an
  159.     // HDC so delete the HDC first. 
  160.     DeleteDC(hdcSrc1);
  161.     DeleteDC(hdcSrc2);
  162.     DeleteObject(hbmSrc1);
  163.     DeleteObject(hbmSrc2);
  164.     
  165.     // Display the blended (destination) image to the target HDC.
  166.     if (bReturn) {
  167.         BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, 
  168.                hdcDst, 0,0, SRCCOPY);
  169.     }
  170.     
  171.     // Clean up the rest of the objects you created.
  172.     DeleteDC(hdcDst);
  173.     DeleteObject(hbmDst);
  174.     return bReturn;
  175. HANDLEERROR:
  176.     if (hdcSrc1) DeleteDC(hdcSrc1);
  177.     if (hdcSrc2) DeleteDC(hdcSrc2);
  178.     if (hdcDst)  DeleteDC(hdcDst);
  179.     if (hbmSrc1) DeleteObject(hbmSrc1);
  180.     if (hbmSrc2) DeleteObject(hbmSrc2);
  181.     if (hbmDst)  DeleteObject(hbmDst);
  182.     return FALSE;
  183. }
  184. const unsigned int CMainFrame::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
  185. /////////////////////////////////////////////////////////////////////////////
  186. // CMainFrame
  187. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  188. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  189. ON_REGISTERED_MESSAGE(CMainFrame::m_nTaskbarCreatedMsg, OnTaskbarCreated)
  190. //{{AFX_MSG_MAP(CMainFrame)
  191. ON_WM_CREATE()
  192. ON_WM_TIMER()
  193. ON_WM_TIMECHANGE()
  194. ON_COMMAND(IDD_CAL_ABOUTBOX, OnCalAboutbox)
  195. ON_COMMAND(IDR_CAL_OPTIONS, OnCalOptions)
  196. //}}AFX_MSG_MAP
  197. END_MESSAGE_MAP()
  198. // CAboutDlg dialog used for App About
  199. class CAboutDlg : public CDialog
  200. {
  201. public:
  202. CAboutDlg();
  203. // Dialog Data
  204. //{{AFX_DATA(CAboutDlg)
  205. enum { IDD = IDD_ABOUTBOX };
  206. //}}AFX_DATA
  207. // ClassWizard generated virtual function overrides
  208. //{{AFX_VIRTUAL(CAboutDlg)
  209. protected:
  210. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  211. //}}AFX_VIRTUAL
  212. // Implementation
  213. protected:
  214. //{{AFX_MSG(CAboutDlg)
  215. virtual BOOL OnInitDialog();
  216. //}}AFX_MSG
  217. DECLARE_MESSAGE_MAP()
  218. };
  219. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  220. {
  221. //{{AFX_DATA_INIT(CAboutDlg)
  222. //}}AFX_DATA_INIT
  223. }
  224. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  225. {
  226. CDialog::DoDataExchange(pDX);
  227. //{{AFX_DATA_MAP(CAboutDlg)
  228. //}}AFX_DATA_MAP
  229. }
  230. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  231. //{{AFX_MSG_MAP(CAboutDlg)
  232. //}}AFX_MSG_MAP
  233. END_MESSAGE_MAP()
  234. BOOL CAboutDlg::OnInitDialog() 
  235. {
  236. CDialog::OnInitDialog();
  237. // TODO: Add extra initialization here
  238. /*SetWindowLong(this->m_hWnd,GWL_EXSTYLE,GetWindowLong(this->m_hWnd,GWL_EXSTYLE)|WS_EX_LAYERED);
  239. HWND hWnd=this->m_hWnd; 
  240. typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);
  241. PSLWA pSetLayeredWindowAttributes;
  242. HMODULE hDLL = LoadLibrary ("user32");
  243. pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");
  244. if (pSetLayeredWindowAttributes != NULL)
  245. {
  246. pSetLayeredWindowAttributes (hWnd,0,225,LWA_ALPHA);
  247. }*/
  248. return TRUE;  // return TRUE unless you set the focus to a control
  249.               // EXCEPTION: OCX Property Pages should return FALSE
  250. }
  251. /////////////////////////////////////////////////////////////////////////////
  252. // CDesktopCalendarApp message handlers
  253. /////////////////////////////////////////////////////////////////////////////
  254. // CMainFrame construction/destruction
  255. CMainFrame::CMainFrame()
  256. {
  257. // TODO: add member initialization code here
  258. hMainBitmap = NULL;
  259. m_nYPos = m_nXPos = 100;
  260. m_nTransparency = 125;
  261. }
  262. CMainFrame::~CMainFrame()
  263. {
  264. }
  265. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  266. {
  267. if( !CFrameWnd::PreCreateWindow(cs) )
  268. return FALSE;
  269. WNDCLASS wndcls;
  270. HINSTANCE hInst = AfxGetInstanceHandle();
  271. if(!(::GetClassInfo(hInst,CAL_CLASS_NAME, &wndcls)))      
  272. {
  273. // get default MFC class settings 
  274. if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))           
  275. {
  276. wndcls.lpszClassName = CAL_CLASS_NAME;                
  277. wndcls.style |= CS_OWNDC;         
  278. wndcls.hbrBackground = NULL;
  279. if (!AfxRegisterClass(&wndcls))             
  280. AfxThrowResourceException();
  281. }
  282. else
  283. AfxThrowResourceException();
  284. }
  285.     cs.lpszClass = CAL_CLASS_NAME; 
  286. return (TRUE);
  287. }
  288. /////////////////////////////////////////////////////////////////////////////
  289. // CMainFrame diagnostics
  290. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  291. {
  292. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  293. return -1;
  294. SetWindowText(WINDOW_NAME);
  295. // Read last saved settings from windows registry
  296. ReadRegistry();
  297. CTime t = CTime::GetCurrentTime();
  298. CString date = t.Format("%A, %B %d, %Y");
  299. strcpy(m_tnd.szTip,date.operator LPCTSTR());
  300. m_hCalIcon = AfxGetApp()->LoadIcon(IDI_ICON1+t.GetDay()-1);
  301. m_tnd.cbSize = sizeof(NOTIFYICONDATA);
  302.     m_tnd.hWnd   = this->m_hWnd;
  303.     m_tnd.uID    = IDR_POP_UP;
  304.     m_tnd.hIcon  = m_hCalIcon;
  305. m_tnd.uCallbackMessage = WM_ICON_NOTIFY;
  306. m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  307. Shell_NotifyIcon(NIM_ADD, &m_tnd);
  308. // Display Calendar
  309. SetCalendar();
  310. m_CurrentTime = CTime::GetCurrentTime();
  311. WriteRegistry();
  312. SetTimer(CAL_TIMER,20000,0);
  313. return 0;
  314. }
  315. LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  316. {
  317. // TODO: Add your specialized code here and/or call the base class
  318. if (message == m_tnd.uCallbackMessage)
  319.         return OnTrayNotification(wParam, lParam);
  320. return CFrameWnd::WindowProc(message, wParam, lParam);
  321. }
  322. LRESULT CMainFrame::OnTrayNotification(UINT wParam, LONG lParam) 
  323. {
  324.     //Return quickly if its not for this tray icon
  325.     if (wParam != m_tnd.uID)
  326.         return (0L);
  327.     CMenu Menu, *pSubMenu;
  328.     CWnd *pTargetWnd = AfxGetMainWnd();
  329.     if (!pTargetWnd)
  330.         return 0L;
  331.     // Clicking with right button brings up a context menu
  332.     if (LOWORD(lParam) == WM_RBUTTONUP)
  333.     {    
  334.         if (!Menu.LoadMenu(m_tnd.uID))
  335.             return (0L);
  336.         
  337. pSubMenu = Menu.GetSubMenu(0);
  338.         if (!pSubMenu)
  339.             return 0;
  340. // CustomizeMenu(pSubMenu);
  341.         // Display and track the popup menu
  342.         CPoint pos;
  343.         GetCursorPos(&pos);
  344.         pTargetWnd->SetForegroundWindow(); 
  345.         
  346. pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
  347.         
  348. // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
  349.         pTargetWnd->PostMessage(WM_NULL, 0, 0);
  350.         Menu.DestroyMenu();
  351.     } 
  352. return (1);
  353. }
  354. void CMainFrame::SetCalendar()
  355. {
  356. KillTimer(CAL_TIMER);
  357. HKEY hRegKey;
  358. LONG lRet = 0;
  359. LONG lWidth=0,lHeight=0;
  360. CString szNewFile;
  361. CHAR szSysDir[MAX_PATH];
  362. GetSystemDirectory(szSysDir,MAX_PATH);
  363. // Delete the last bitmap
  364. if(hMainBitmap != NULL)
  365. {
  366. DeleteObject(hMainBitmap);
  367. hMainBitmap = NULL;
  368. }
  369. // Read the registry key which says about desktop wall paper
  370. lRet = RegOpenKeyEx(HKEY_CURRENT_USER,
  371. "Software\Microsoft\Internet Explorer\Desktop\General",
  372. 0,KEY_ENUMERATE_SUB_KEYS|KEY_EXECUTE|KEY_READ|KEY_WRITE,
  373. &hRegKey);
  374. if(lRet == ERROR_SUCCESS)
  375. {
  376. unsigned long ulType;
  377. BYTE szFileName[MAX_PATH];
  378. unsigned long   ulLength=MAX_PATH;
  379. lRet = RegQueryValueEx(hRegKey,"Wallpaper",NULL,&ulType,szFileName,&ulLength);
  380. if(lRet == ERROR_SUCCESS)
  381. {
  382. CString szFile(szFileName);
  383. szFile.TrimLeft();
  384. szFile.TrimRight();
  385. if(!szFile.IsEmpty())
  386. {
  387. // Expand Sytem Paths
  388. int k = szFile.ReverseFind('%');
  389. if( k > 0)
  390. {
  391. CString Variable = szFile.Left(k);
  392. Variable = Variable.Right(Variable.GetLength()-1);
  393. int nRet = GetEnvironmentVariable(Variable.operator LPCTSTR(),(char *)szFileName,MAX_PATH);
  394. if(nRet > 0)
  395. {
  396. szFile  = szFile.Right(szFile.GetLength()-(k+1));
  397. Variable = szFileName;
  398. szFile   = Variable + _T("\") + szFile;
  399. }
  400. }
  401. k = szFile.ReverseFind('\');
  402. CString szName = szFile.Right(szFile.GetLength()-(k+1));
  403. if(szName == _T("Desktop Calendar.bmp"))
  404. {
  405. szFile = szSysDir;
  406. szFile +=_T("\Desk_Cal");
  407. hMainBitmap = LoadImage(szFile,lWidth,lHeight);
  408. if(hMainBitmap == NULL)
  409. return;
  410. }
  411. else
  412. {
  413. hMainBitmap = LoadImage(szFile,lWidth,lHeight);
  414. szNewFile = szSysDir;
  415. szNewFile += _T("\Desk_Cal");
  416. CopyFile(szFile,szNewFile,FALSE);
  417. }
  418. }
  419. else
  420. {
  421. if(NULL != hMainBitmap)
  422. {
  423. DeleteObject(hMainBitmap);
  424. hMainBitmap = NULL;
  425. }
  426. }
  427. HWND hWnd = this->m_hWnd;
  428. // Screen Resolution //
  429. int nScreenWidth  = GetSystemMetrics(SM_CXSCREEN);
  430. int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  431. HDC hDC = ::GetDC(hWnd);
  432. HDC hBitmapDC = ::CreateCompatibleDC(hDC);
  433. HDC hMemDC    = ::CreateCompatibleDC(hDC);
  434. HBITMAP hBitmap = CreateCompatibleBitmap(hDC,nScreenWidth,nScreenHeight);
  435. ::SelectObject(hMemDC,hBitmap);
  436. if(hMainBitmap)
  437. {
  438. // Image Height and Width
  439. #define HIMETRIC_INCH 2540
  440. lWidth = MulDiv(lWidth, GetDeviceCaps(hDC, LOGPIXELSX),HIMETRIC_INCH);
  441. lHeight = MulDiv(lHeight, GetDeviceCaps(hDC,LOGPIXELSY), HIMETRIC_INCH);
  442. ::SelectObject(hBitmapDC,hMainBitmap);
  443. SetStretchBltMode(hMemDC,HALFTONE);
  444. ::StretchBlt(hMemDC,0,0,nScreenWidth,nScreenHeight,hBitmapDC,0,0,lWidth,lHeight,SRCCOPY);
  445. }
  446. else
  447. {
  448. COLORREF crDeskColor = GetSysColor(COLOR_DESKTOP);
  449. CBrush FillBrush;
  450. FillBrush.CreateSolidBrush(crDeskColor);
  451. CRect Rect(0,0,nScreenWidth,nScreenHeight);
  452. ::FillRect(hMemDC,&Rect,FillBrush.operator HBRUSH());
  453. }
  454. // Making Calendar
  455. C_Calendar * pCalendar;
  456. pCalendar = new C_Calendar();
  457. pCalendar->SetFont(m_szFontName);
  458. pCalendar->SetTextColor(m_crTextColor);
  459. pCalendar->SetBackColor(m_crBackColor);
  460. pCalendar->SetTextSize(m_nFontSize);
  461. HDC hCalDC = pCalendar->DrawCalendar(hDC);
  462. DoAlphaBlend(hMemDC,m_nXPos,m_nYPos,pCalendar->GetWidth(),pCalendar->GetHeight(),
  463. hCalDC,0,0,pCalendar->GetWidth(),pCalendar->GetHeight(),m_nTransparency);
  464. szNewFile = szSysDir;
  465. szNewFile += _T("\Desktop Calendar.bmp");
  466. SaveBitmap(hMemDC,hBitmap,szNewFile);
  467. lRet = RegSetValueEx(hRegKey,"Wallpaper",NULL,REG_SZ,(const BYTE*)szNewFile.operator LPCTSTR(),szNewFile.GetLength());
  468. SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,(void*)szNewFile.operator LPCTSTR(),SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);    
  469. delete(pCalendar);
  470. pCalendar = NULL;
  471. DeleteDC(hMemDC);
  472. DeleteDC(hBitmapDC);
  473. DeleteObject(hMainBitmap);
  474. DeleteObject(hBitmap);
  475. hMainBitmap = NULL;
  476. hBitmap = NULL;
  477. hMemDC= hBitmapDC =  NULL;
  478. ::ReleaseDC(hWnd,hDC);
  479. }
  480. RegCloseKey(hRegKey);
  481. }
  482. CTime tm = CTime::GetCurrentTime();
  483. m_CurrentTime = tm;
  484. SetIcon(tm.GetDay());
  485. CString date = tm.Format("%A, %B %d, %Y");
  486. SetToolTip(date);
  487. SetTimer(CAL_TIMER,20000,0);
  488. }
  489. void CMainFrame::OnTimer(UINT nIDEvent) 
  490. {
  491. // TODO: Add your message handler code here and/or call default
  492. int nResult = CheckDate();
  493. if(nResult == CHANGE_ALL)
  494. {
  495. SetCalendar();
  496. }
  497. else
  498. if(nResult == DAY_CHANGE)
  499. {
  500. CTime tm = CTime::GetCurrentTime();
  501. m_CurrentTime = tm;
  502. SetIcon(tm.GetDay());
  503. CString date = tm.Format("%A, %B %d, %Y");
  504. SetToolTip(date);
  505. }
  506. m_CurrentTime = CTime::GetCurrentTime();
  507. WriteRegistry();
  508. CFrameWnd::OnTimer(nIDEvent);
  509. }
  510. HBITMAP CMainFrame::LoadImage(CString szFile,long & lWidth,long& lHeight)
  511. {
  512. HANDLE hFile;
  513. HBITMAP hBmp;
  514. DWORD dwSize;
  515. DWORD dwRead;
  516. HGLOBAL hMemJpeg;
  517. LPSTREAM lpStream;
  518. OLE_HANDLE hJpegBmp;
  519. HRESULT hr;
  520. LPPICTURE lpPicture = NULL;
  521. void* pMemJpeg;
  522. /* Open the file and get the size. */
  523. if((hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING, 0, NULL)) 
  524. == INVALID_HANDLE_VALUE)
  525. return NULL;
  526. if((dwSize = GetFileSize(hFile, NULL)) == 0xFFFFFFFF)
  527. {
  528. CloseHandle(hFile);
  529. return (NULL);
  530. }
  531. /* Allocate space for file, read it in, and then close the file again. */
  532. if((hMemJpeg = GlobalAlloc(GMEM_MOVEABLE, dwSize)) == NULL)
  533. {
  534. CloseHandle(hFile);
  535. return (NULL);
  536. }
  537. if((pMemJpeg = GlobalLock(hMemJpeg)) == NULL)
  538. {
  539. CloseHandle(hFile);
  540. GlobalFree(hMemJpeg);
  541. return (NULL);
  542. }
  543. if(!ReadFile(hFile, pMemJpeg, dwSize, &dwRead, NULL))
  544. {
  545. CloseHandle(hFile);
  546. GlobalFree(hMemJpeg);
  547. return (NULL);
  548. }
  549. CloseHandle(hFile);
  550. GlobalUnlock(hMemJpeg);
  551. /* Create the stream and load the picture. */
  552. if((hr = CreateStreamOnHGlobal(hMemJpeg, TRUE, &lpStream)) != S_OK)
  553. {
  554. GlobalFree(hMemJpeg);
  555. return (NULL);
  556. }
  557. if(::OleLoadPicture(lpStream, dwSize, FALSE, IID_IPicture,(LPVOID*) &lpPicture) != S_OK)
  558. {
  559. GlobalFree(hMemJpeg);
  560. lpStream->Release();
  561. return (NULL);
  562. }
  563. /* Get the handle to the image, and then copy it. */
  564. if((lpPicture->get_Handle(&hJpegBmp)) != S_OK)
  565. {
  566. GlobalFree(hMemJpeg);
  567. lpStream->Release();
  568. lpPicture->Release();
  569. return (NULL);
  570. }
  571. lWidth = lHeight = 0;
  572. lpPicture->get_Width((OLE_XSIZE_HIMETRIC*)&lWidth);
  573. lpPicture->get_Height((OLE_XSIZE_HIMETRIC*)&lHeight);
  574. if((hBmp = (HBITMAP)CopyImage((HANDLE *) hJpegBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)) == NULL)
  575. {
  576. GlobalFree(hMemJpeg);
  577. lpStream->Release();
  578. lpPicture->Release();
  579. return (NULL);
  580. }
  581. /* Free the original image and memory. */
  582. GlobalFree(hMemJpeg);
  583. lpStream->Release();
  584. lpPicture->Release();
  585. return (hBmp);
  586. }
  587. BOOL CMainFrame::SaveBitmap(HDC hDC,HBITMAP hBitmap,CString szPath)
  588. {
  589. FILE * fp= NULL;
  590. fp = fopen(szPath.GetBuffer(1),"wb");
  591. if(fp == NULL)
  592. return false;
  593. BITMAP Bm;
  594. BITMAPINFO BitInfo;
  595. ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
  596. BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  597. BitInfo.bmiHeader.biBitCount = 0;
  598. if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
  599. return (false);
  600. Bm.bmHeight = BitInfo.bmiHeader.biHeight;
  601. Bm.bmWidth  = BitInfo.bmiHeader.biWidth;
  602. BITMAPFILEHEADER BmHdr;
  603. BmHdr.bfType = 0x4d42;   // 'BM' WINDOWS_BITMAP_SIGNATURE
  604. BmHdr.bfSize = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  605. BmHdr.bfReserved1 = BmHdr.bfReserved2 = 0;
  606. BmHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  607. BitInfo.bmiHeader.biCompression = 0;
  608. // Writing Bitmap File Header ////
  609. fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);
  610. fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
  611. BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
  612. if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight, pData, &BitInfo, DIB_RGB_COLORS))
  613. return (false);
  614. if(pData != NULL)
  615. fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);
  616. fclose(fp);
  617. delete (pData);
  618. return (true);
  619. }
  620. void CMainFrame::OnTimeChange() 
  621. {
  622. CFrameWnd::OnTimeChange();
  623. if(CheckDate() == CHANGE_ALL)
  624. {
  625. SetCalendar();
  626. }
  627. else
  628. if(CheckDate() == DAY_CHANGE)
  629. {
  630. CTime tm = CTime::GetCurrentTime();
  631. m_CurrentTime = tm;
  632. SetIcon(tm.GetDay());
  633. CString date = tm.Format("%A, %B %d, %Y");
  634. SetToolTip(date);
  635. }
  636. m_CurrentTime = CTime::GetCurrentTime();
  637. WriteRegistry();
  638. }
  639. void CMainFrame::ReadRegistry()
  640. {
  641. CString szSection(_T("Configuration"));
  642. CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
  643. m_szFontName = pApp->GetProfileString(szSection,_T("Font Name"),_T("Monotype Corsiva"));
  644. m_nFontSize = pApp->GetProfileInt(szSection,_T("Text Size"),24);
  645. m_crTextColor = pApp->GetProfileInt(szSection,_T("Text Color"),(INT)RGB(0,255,0));
  646. m_crBackColor = pApp->GetProfileInt(szSection,_T("Back Ground Color"),(INT)RGB(255,255,255));
  647. m_nTransparency = pApp->GetProfileInt(szSection,_T("Transparency"),88);
  648. m_nXPos = pApp->GetProfileInt(szSection,_T("StartX"),100);
  649. m_nYPos = pApp->GetProfileInt(szSection,_T("StartY"),100);
  650. }
  651. void CMainFrame::WriteRegistry()
  652. {
  653. CString szSection(_T("Configuration"));
  654. CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
  655. pApp->WriteProfileString(szSection,_T("Font Name"),m_szFontName);
  656. pApp->WriteProfileInt(szSection,_T("Text Size"),m_nFontSize);
  657. pApp->WriteProfileInt(szSection,_T("Text Color"),m_crTextColor);
  658. pApp->WriteProfileInt(szSection,_T("Back Ground Color"),m_crBackColor);
  659. pApp->WriteProfileInt(szSection,_T("Day"),m_CurrentTime.GetDay());
  660. pApp->WriteProfileInt(szSection,_T("Month"),m_CurrentTime.GetMonth());
  661. pApp->WriteProfileInt(szSection,_T("Year"),m_CurrentTime.GetYear());
  662. pApp->WriteProfileInt(szSection,_T("Transparency"),m_nTransparency);
  663. pApp->WriteProfileInt(szSection,_T("StartX"),m_nXPos);
  664. pApp->WriteProfileInt(szSection,_T("StartY"),m_nYPos);
  665. }
  666. void CMainFrame::SetIcon(INT nDate)
  667. {
  668. HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICON1+(nDate-1));
  669. if(hIcon)
  670. {
  671. if(NULL!= m_hCalIcon)
  672. ::DestroyIcon(m_hCalIcon);
  673. m_hCalIcon = hIcon;
  674. m_tnd.hIcon  = m_hCalIcon;
  675. m_tnd.uFlags = NIF_ICON;
  676. Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  677. }
  678. }
  679. void CMainFrame::SetToolTip(CString szToolTip)
  680. {
  681. strcpy(m_tnd.szTip,szToolTip.operator LPCTSTR());
  682.     m_tnd.uFlags = NIF_TIP;
  683. Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
  684. }
  685. int CMainFrame::CheckDate()
  686. {
  687. CString szSection(_T("Configuration"));
  688. CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
  689. int nLastDay   = pApp->GetProfileInt(szSection,_T("Day"),0);
  690. int nLastYear  = pApp->GetProfileInt(szSection,_T("Year"),0);
  691. int nLastMonth = pApp->GetProfileInt(szSection,_T("Month"),0);
  692. CTime tm = CTime::GetCurrentTime();
  693. if(nLastDay == tm.GetDay() &&
  694. nLastMonth == tm.GetMonth() &&
  695. nLastYear == tm.GetYear())
  696. return (NO_CHANGE);
  697. if(nLastDay != tm.GetDay() &&
  698. nLastMonth == tm.GetMonth() &&
  699. nLastYear == tm.GetYear())
  700. return (DAY_CHANGE);
  701. return (CHANGE_ALL);
  702. }
  703. BOOL CMainFrame::DestroyWindow() 
  704. {
  705. m_tnd.uFlags = 0;
  706. // Remove Icon
  707. Shell_NotifyIcon(NIM_DELETE, &m_tnd);
  708. WriteRegistry();
  709. if(NULL!= hMainBitmap)
  710. {
  711. DeleteObject(hMainBitmap);
  712. hMainBitmap = NULL;
  713. }
  714. return CFrameWnd::DestroyWindow();
  715. }
  716. LRESULT CMainFrame::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
  717. {
  718. // Install the icon again
  719. CTime t = CTime::GetCurrentTime();
  720. CString date = t.Format("%A, %B %d, %Y");
  721. if(NULL != m_hCalIcon)
  722. {
  723. DestroyIcon(m_hCalIcon);
  724. }
  725. m_hCalIcon = AfxGetApp()->LoadIcon(IDI_ICON1+t.GetDay()-1);
  726. m_tnd.cbSize = sizeof(NOTIFYICONDATA);
  727.     m_tnd.hWnd   = this->m_hWnd;
  728.     m_tnd.uID    = IDR_POP_UP;
  729.     m_tnd.hIcon  = m_hCalIcon;
  730. strcpy(m_tnd.szTip,date.operator LPCTSTR());
  731. m_tnd.uCallbackMessage = WM_ICON_NOTIFY;
  732. m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  733. Shell_NotifyIcon(NIM_ADD, &m_tnd);
  734. return (0L);
  735. }
  736. void CMainFrame::OnCalAboutbox() 
  737. {
  738. CAboutDlg aboutDlg;
  739. aboutDlg.DoModal();
  740. }
  741. void CMainFrame::OnCalOptions() 
  742. {
  743. // TODO: Add your command handler code here
  744. COptionsDlg Dlg;
  745. Dlg.m_szFontName = m_szFontName;
  746. Dlg.m_crBackColor = m_crBackColor;
  747. Dlg.m_crTextColor = m_crTextColor; 
  748. Dlg.m_nFontSize = m_nFontSize;
  749. Dlg.m_uXPos = m_nXPos;
  750. Dlg.m_uYPos = m_nYPos;
  751. Dlg.m_nTransparency = m_nTransparency;
  752. if(Dlg.DoModal() ==IDOK)
  753. {
  754. m_szFontName = Dlg.m_szFontName;
  755. m_crBackColor = Dlg.m_crBackColor;
  756. m_crTextColor = Dlg.m_crTextColor;
  757. m_nFontSize = Dlg.m_nFontSize;
  758. m_nXPos = Dlg.m_uXPos;
  759. m_nYPos = Dlg.m_uYPos;
  760. m_nTransparency = Dlg.m_nTransparency;
  761. SetCalendar();
  762. }
  763. }