MainFrm.cpp
上传用户:jianghp13
上传日期:2020-02-03
资源大小:148k
文件大小:25k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "DesktopCalendar.h"
- #include "Calendar.h"
- #include "OptionsDlg.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- # define WS_EX_LAYERED 0x80000
- # define LWA_ALPHA 2
- # define LWA_COLORKEY 1
- HBITMAP Create24BPPDIBSection(HDC hDC, int iWidth, int iHeight)
- {
- BITMAPINFO bmi;
- HBITMAP hbm;
- LPBYTE pBits;
- // Initialize header to 0s.
- ZeroMemory(&bmi, sizeof(bmi));
- // Fill out the fields you care about.
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = iWidth;
- bmi.bmiHeader.biHeight = iHeight;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 24;
- bmi.bmiHeader.biCompression = BI_RGB;
- // Create the surface.
- hbm = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS,(void **)&pBits, NULL, 0);
-
- return(hbm);
- }
- BOOL BitmapsCompatible(LPBITMAP lpbm1, LPBITMAP lpbm2)
- {
- if (lpbm1->bmBitsPixel != lpbm2->bmBitsPixel)
- return FALSE;
- if (lpbm1->bmPlanes != lpbm2->bmPlanes)
- return FALSE;
- if (lpbm1->bmWidth != lpbm2->bmWidth)
- return FALSE;
- if (lpbm1->bmHeight != lpbm2->bmHeight)
- return FALSE;
- return TRUE;
- }
- BOOL BlendImages(HBITMAP hbmSrc1, HBITMAP hbmSrc2, HBITMAP hbmDst, DWORD dwWeight1)
- {
- BITMAP bmSrc1, bmSrc2, bmDst;
- RGBTRIPLE *lprgbSrc1, *lprgbSrc2, *lprgbDst;
- DWORD dwWidthBytes, dwWeight2;
- int x, y;
-
- // Only values between 0 and 255 are valid.
- if (dwWeight1 > 255) return FALSE;
-
- // Get weighting value for second source image.
- dwWeight2 = 255-dwWeight1;
-
- // Get information about the surfaces you were passed.
- if (!GetObject(hbmSrc1, sizeof(BITMAP), &bmSrc1)) return FALSE;
- if (!GetObject(hbmSrc2, sizeof(BITMAP), &bmSrc2)) return FALSE;
- if (!GetObject(hbmDst, sizeof(BITMAP), &bmDst)) return FALSE;
- // Make sure you have data that meets your requirements.
- if (!BitmapsCompatible(&bmSrc1, &bmSrc2))
- return FALSE;
- if (!BitmapsCompatible(&bmSrc1, &bmDst))
- return FALSE;
- if (bmSrc1.bmBitsPixel != 24)
- return FALSE;
- if (bmSrc1.bmPlanes != 1)
- return FALSE;
- if (!bmSrc1.bmBits || !bmSrc2.bmBits || !bmDst.bmBits)
- return FALSE;
- dwWidthBytes = bmDst.bmWidthBytes;
-
- // Initialize the surface pointers.
- lprgbSrc1 = (RGBTRIPLE *)bmSrc1.bmBits;
- lprgbSrc2 = (RGBTRIPLE *)bmSrc2.bmBits;
- lprgbDst = (RGBTRIPLE *)bmDst.bmBits;
- for (y=0; y<bmDst.bmHeight; y++) {
- for (x=0; x<bmDst.bmWidth; x++) {
- lprgbDst[x].rgbtRed = (BYTE)((((DWORD)lprgbSrc1[x].rgbtRed * dwWeight1) +
- ((DWORD)lprgbSrc2[x].rgbtRed * dwWeight2)) >> 8);
- lprgbDst[x].rgbtGreen = (BYTE)((((DWORD)lprgbSrc1[x].rgbtGreen * dwWeight1) +
- ((DWORD)lprgbSrc2[x].rgbtGreen * dwWeight2)) >> 8);
- lprgbDst[x].rgbtBlue = (BYTE)((((DWORD)lprgbSrc1[x].rgbtBlue * dwWeight1) +
- ((DWORD)lprgbSrc2[x].rgbtBlue * dwWeight2)) >> 8);
- }
- // Move to next scan line.
- lprgbSrc1 = (RGBTRIPLE *)((LPBYTE)lprgbSrc1 + dwWidthBytes);
- lprgbSrc2 = (RGBTRIPLE *)((LPBYTE)lprgbSrc2 + dwWidthBytes);
- lprgbDst = (RGBTRIPLE *)((LPBYTE)lprgbDst + dwWidthBytes);
- }
- return TRUE;
- }
- BOOL DoAlphaBlend(
- HDC hdcDest, // Handle to destination DC.
- int nXOriginDest, // X-coord of upper-left corner.
- int nYOriginDest, // Y-coord of upper-left corner.
- int nWidthDest, // Destination width.
- int nHeightDest, // Destination height.
- HDC hdcSrc, // Handle to source DC.
- int nXOriginSrc, // X-coord of upper-left corner.
- int nYOriginSrc, // Y-coord of upper-left corner.
- int nWidthSrc, // Source width.
- int nHeightSrc, // Source height.
- DWORD dwSourceWeight) // Source weighting (between 0 and 255).
- {
- HDC hdcSrc1 = NULL;
- HDC hdcSrc2 = NULL;
- HDC hdcDst = NULL;
- HBITMAP hbmSrc1 = NULL;
- HBITMAP hbmSrc2 = NULL;
- HBITMAP hbmDst = NULL;
- BOOL bReturn;
- // Create surfaces for sources and destination images.
- hbmSrc1 = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
- if (!hbmSrc1) goto HANDLEERROR;
- hbmSrc2 = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
- if (!hbmSrc2) goto HANDLEERROR;
- hbmDst = Create24BPPDIBSection(hdcDest, nWidthDest,nHeightDest);
- if (!hbmDst) goto HANDLEERROR;
-
- // Create HDCs to hold our surfaces.
- hdcSrc1 = CreateCompatibleDC(hdcDest);
- if (!hdcSrc1) goto HANDLEERROR;
-
- hdcSrc2 = CreateCompatibleDC(hdcDest);
- if (!hdcSrc2) goto HANDLEERROR;
-
- hdcDst = CreateCompatibleDC(hdcDest);
- if (!hdcDst) goto HANDLEERROR;
-
- // Prepare the surfaces for drawing.
- SelectObject(hdcSrc1, hbmSrc1);
- SelectObject(hdcSrc2, hbmSrc2);
- SelectObject(hdcDst, hbmDst);
- SetStretchBltMode(hdcSrc1, COLORONCOLOR);
- SetStretchBltMode(hdcSrc2, COLORONCOLOR);
-
- // Capture a copy of the source area.
- if (!StretchBlt(hdcSrc1, 0,0,nWidthDest,nHeightDest,
- hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
- SRCCOPY))
- goto HANDLEERROR;
-
- // Capture a copy of the destination area.
- if (!StretchBlt(hdcSrc2, 0,0,nWidthDest,nHeightDest,
- hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
- SRCCOPY))
- goto HANDLEERROR;
-
- // Blend the two source areas to create the destination image.
- bReturn = BlendImages(hbmSrc1, hbmSrc2, hbmDst, dwSourceWeight);
-
- // Clean up objects you do not need any longer.
- // You cannot delete an object that's selected into an
- // HDC so delete the HDC first.
- DeleteDC(hdcSrc1);
- DeleteDC(hdcSrc2);
- DeleteObject(hbmSrc1);
- DeleteObject(hbmSrc2);
-
- // Display the blended (destination) image to the target HDC.
- if (bReturn) {
- BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
- hdcDst, 0,0, SRCCOPY);
- }
-
- // Clean up the rest of the objects you created.
- DeleteDC(hdcDst);
- DeleteObject(hbmDst);
- return bReturn;
- HANDLEERROR:
- if (hdcSrc1) DeleteDC(hdcSrc1);
- if (hdcSrc2) DeleteDC(hdcSrc2);
- if (hdcDst) DeleteDC(hdcDst);
- if (hbmSrc1) DeleteObject(hbmSrc1);
- if (hbmSrc2) DeleteObject(hbmSrc2);
- if (hbmDst) DeleteObject(hbmDst);
- return FALSE;
- }
- const unsigned int CMainFrame::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- ON_REGISTERED_MESSAGE(CMainFrame::m_nTaskbarCreatedMsg, OnTaskbarCreated)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_WM_TIMER()
- ON_WM_TIMECHANGE()
- ON_COMMAND(IDD_CAL_ABOUTBOX, OnCalAboutbox)
- ON_COMMAND(IDR_CAL_OPTIONS, OnCalOptions)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- virtual BOOL OnInitDialog();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
- /*SetWindowLong(this->m_hWnd,GWL_EXSTYLE,GetWindowLong(this->m_hWnd,GWL_EXSTYLE)|WS_EX_LAYERED);
- HWND hWnd=this->m_hWnd;
- typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);
- PSLWA pSetLayeredWindowAttributes;
- HMODULE hDLL = LoadLibrary ("user32");
- pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");
- if (pSetLayeredWindowAttributes != NULL)
- {
- pSetLayeredWindowAttributes (hWnd,0,225,LWA_ALPHA);
- }*/
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDesktopCalendarApp message handlers
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- hMainBitmap = NULL;
- m_nYPos = m_nXPos = 100;
- m_nTransparency = 125;
- }
- CMainFrame::~CMainFrame()
- {
-
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- WNDCLASS wndcls;
- HINSTANCE hInst = AfxGetInstanceHandle();
-
- if(!(::GetClassInfo(hInst,CAL_CLASS_NAME, &wndcls)))
- {
- // get default MFC class settings
- if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))
- {
- wndcls.lpszClassName = CAL_CLASS_NAME;
- wndcls.style |= CS_OWNDC;
- wndcls.hbrBackground = NULL;
- if (!AfxRegisterClass(&wndcls))
- AfxThrowResourceException();
- }
- else
- AfxThrowResourceException();
- }
- cs.lpszClass = CAL_CLASS_NAME;
-
- return (TRUE);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- SetWindowText(WINDOW_NAME);
- // Read last saved settings from windows registry
- ReadRegistry();
-
- CTime t = CTime::GetCurrentTime();
- CString date = t.Format("%A, %B %d, %Y");
- strcpy(m_tnd.szTip,date.operator LPCTSTR());
- m_hCalIcon = AfxGetApp()->LoadIcon(IDI_ICON1+t.GetDay()-1);
- m_tnd.cbSize = sizeof(NOTIFYICONDATA);
- m_tnd.hWnd = this->m_hWnd;
- m_tnd.uID = IDR_POP_UP;
- m_tnd.hIcon = m_hCalIcon;
- m_tnd.uCallbackMessage = WM_ICON_NOTIFY;
- m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- Shell_NotifyIcon(NIM_ADD, &m_tnd);
- // Display Calendar
- SetCalendar();
- m_CurrentTime = CTime::GetCurrentTime();
- WriteRegistry();
-
- SetTimer(CAL_TIMER,20000,0);
- return 0;
- }
- LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- if (message == m_tnd.uCallbackMessage)
- return OnTrayNotification(wParam, lParam);
- return CFrameWnd::WindowProc(message, wParam, lParam);
- }
- LRESULT CMainFrame::OnTrayNotification(UINT wParam, LONG lParam)
- {
- //Return quickly if its not for this tray icon
- if (wParam != m_tnd.uID)
- return (0L);
- CMenu Menu, *pSubMenu;
- CWnd *pTargetWnd = AfxGetMainWnd();
- if (!pTargetWnd)
- return 0L;
- // Clicking with right button brings up a context menu
- if (LOWORD(lParam) == WM_RBUTTONUP)
- {
- if (!Menu.LoadMenu(m_tnd.uID))
- return (0L);
-
- pSubMenu = Menu.GetSubMenu(0);
- if (!pSubMenu)
- return 0;
- // CustomizeMenu(pSubMenu);
- // Display and track the popup menu
- CPoint pos;
- GetCursorPos(&pos);
- pTargetWnd->SetForegroundWindow();
-
- pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
-
- // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
- pTargetWnd->PostMessage(WM_NULL, 0, 0);
- Menu.DestroyMenu();
- }
-
- return (1);
- }
- void CMainFrame::SetCalendar()
- {
- KillTimer(CAL_TIMER);
- HKEY hRegKey;
- LONG lRet = 0;
- LONG lWidth=0,lHeight=0;
- CString szNewFile;
- CHAR szSysDir[MAX_PATH];
- GetSystemDirectory(szSysDir,MAX_PATH);
-
- // Delete the last bitmap
- if(hMainBitmap != NULL)
- {
- DeleteObject(hMainBitmap);
- hMainBitmap = NULL;
- }
- // Read the registry key which says about desktop wall paper
- lRet = RegOpenKeyEx(HKEY_CURRENT_USER,
- "Software\Microsoft\Internet Explorer\Desktop\General",
- 0,KEY_ENUMERATE_SUB_KEYS|KEY_EXECUTE|KEY_READ|KEY_WRITE,
- &hRegKey);
- if(lRet == ERROR_SUCCESS)
- {
- unsigned long ulType;
- BYTE szFileName[MAX_PATH];
- unsigned long ulLength=MAX_PATH;
-
- lRet = RegQueryValueEx(hRegKey,"Wallpaper",NULL,&ulType,szFileName,&ulLength);
-
- if(lRet == ERROR_SUCCESS)
- {
- CString szFile(szFileName);
- szFile.TrimLeft();
- szFile.TrimRight();
-
- if(!szFile.IsEmpty())
- {
- // Expand Sytem Paths
- int k = szFile.ReverseFind('%');
- if( k > 0)
- {
- CString Variable = szFile.Left(k);
- Variable = Variable.Right(Variable.GetLength()-1);
- int nRet = GetEnvironmentVariable(Variable.operator LPCTSTR(),(char *)szFileName,MAX_PATH);
- if(nRet > 0)
- {
- szFile = szFile.Right(szFile.GetLength()-(k+1));
- Variable = szFileName;
- szFile = Variable + _T("\") + szFile;
- }
- }
-
- k = szFile.ReverseFind('\');
- CString szName = szFile.Right(szFile.GetLength()-(k+1));
- if(szName == _T("Desktop Calendar.bmp"))
- {
- szFile = szSysDir;
- szFile +=_T("\Desk_Cal");
- hMainBitmap = LoadImage(szFile,lWidth,lHeight);
- if(hMainBitmap == NULL)
- return;
- }
- else
- {
- hMainBitmap = LoadImage(szFile,lWidth,lHeight);
- szNewFile = szSysDir;
- szNewFile += _T("\Desk_Cal");
- CopyFile(szFile,szNewFile,FALSE);
- }
- }
- else
- {
- if(NULL != hMainBitmap)
- {
- DeleteObject(hMainBitmap);
- hMainBitmap = NULL;
- }
- }
- HWND hWnd = this->m_hWnd;
- // Screen Resolution //
- int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
- HDC hDC = ::GetDC(hWnd);
-
- HDC hBitmapDC = ::CreateCompatibleDC(hDC);
- HDC hMemDC = ::CreateCompatibleDC(hDC);
-
- HBITMAP hBitmap = CreateCompatibleBitmap(hDC,nScreenWidth,nScreenHeight);
- ::SelectObject(hMemDC,hBitmap);
-
- if(hMainBitmap)
- {
- // Image Height and Width
- #define HIMETRIC_INCH 2540
- lWidth = MulDiv(lWidth, GetDeviceCaps(hDC, LOGPIXELSX),HIMETRIC_INCH);
- lHeight = MulDiv(lHeight, GetDeviceCaps(hDC,LOGPIXELSY), HIMETRIC_INCH);
- ::SelectObject(hBitmapDC,hMainBitmap);
- SetStretchBltMode(hMemDC,HALFTONE);
- ::StretchBlt(hMemDC,0,0,nScreenWidth,nScreenHeight,hBitmapDC,0,0,lWidth,lHeight,SRCCOPY);
- }
- else
- {
- COLORREF crDeskColor = GetSysColor(COLOR_DESKTOP);
- CBrush FillBrush;
- FillBrush.CreateSolidBrush(crDeskColor);
- CRect Rect(0,0,nScreenWidth,nScreenHeight);
- ::FillRect(hMemDC,&Rect,FillBrush.operator HBRUSH());
- }
- // Making Calendar
- C_Calendar * pCalendar;
- pCalendar = new C_Calendar();
- pCalendar->SetFont(m_szFontName);
- pCalendar->SetTextColor(m_crTextColor);
- pCalendar->SetBackColor(m_crBackColor);
- pCalendar->SetTextSize(m_nFontSize);
- HDC hCalDC = pCalendar->DrawCalendar(hDC);
- DoAlphaBlend(hMemDC,m_nXPos,m_nYPos,pCalendar->GetWidth(),pCalendar->GetHeight(),
- hCalDC,0,0,pCalendar->GetWidth(),pCalendar->GetHeight(),m_nTransparency);
-
- szNewFile = szSysDir;
- szNewFile += _T("\Desktop Calendar.bmp");
- SaveBitmap(hMemDC,hBitmap,szNewFile);
-
- lRet = RegSetValueEx(hRegKey,"Wallpaper",NULL,REG_SZ,(const BYTE*)szNewFile.operator LPCTSTR(),szNewFile.GetLength());
- SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,(void*)szNewFile.operator LPCTSTR(),SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
- delete(pCalendar);
- pCalendar = NULL;
- DeleteDC(hMemDC);
- DeleteDC(hBitmapDC);
- DeleteObject(hMainBitmap);
- DeleteObject(hBitmap);
- hMainBitmap = NULL;
- hBitmap = NULL;
- hMemDC= hBitmapDC = NULL;
- ::ReleaseDC(hWnd,hDC);
- }
- RegCloseKey(hRegKey);
- }
-
- CTime tm = CTime::GetCurrentTime();
- m_CurrentTime = tm;
- SetIcon(tm.GetDay());
- CString date = tm.Format("%A, %B %d, %Y");
- SetToolTip(date);
- SetTimer(CAL_TIMER,20000,0);
- }
- void CMainFrame::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- int nResult = CheckDate();
- if(nResult == CHANGE_ALL)
- {
- SetCalendar();
- }
- else
- if(nResult == DAY_CHANGE)
- {
- CTime tm = CTime::GetCurrentTime();
- m_CurrentTime = tm;
- SetIcon(tm.GetDay());
- CString date = tm.Format("%A, %B %d, %Y");
- SetToolTip(date);
- }
- m_CurrentTime = CTime::GetCurrentTime();
- WriteRegistry();
- CFrameWnd::OnTimer(nIDEvent);
- }
- HBITMAP CMainFrame::LoadImage(CString szFile,long & lWidth,long& lHeight)
- {
- HANDLE hFile;
- HBITMAP hBmp;
- DWORD dwSize;
- DWORD dwRead;
- HGLOBAL hMemJpeg;
- LPSTREAM lpStream;
- OLE_HANDLE hJpegBmp;
- HRESULT hr;
- LPPICTURE lpPicture = NULL;
- void* pMemJpeg;
- /* Open the file and get the size. */
- if((hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING, 0, NULL))
- == INVALID_HANDLE_VALUE)
- return NULL;
-
- if((dwSize = GetFileSize(hFile, NULL)) == 0xFFFFFFFF)
- {
- CloseHandle(hFile);
- return (NULL);
- }
- /* Allocate space for file, read it in, and then close the file again. */
- if((hMemJpeg = GlobalAlloc(GMEM_MOVEABLE, dwSize)) == NULL)
- {
- CloseHandle(hFile);
- return (NULL);
- }
-
- if((pMemJpeg = GlobalLock(hMemJpeg)) == NULL)
- {
- CloseHandle(hFile);
- GlobalFree(hMemJpeg);
- return (NULL);
- }
-
- if(!ReadFile(hFile, pMemJpeg, dwSize, &dwRead, NULL))
- {
- CloseHandle(hFile);
- GlobalFree(hMemJpeg);
- return (NULL);
- }
-
- CloseHandle(hFile);
- GlobalUnlock(hMemJpeg);
-
- /* Create the stream and load the picture. */
- if((hr = CreateStreamOnHGlobal(hMemJpeg, TRUE, &lpStream)) != S_OK)
- {
- GlobalFree(hMemJpeg);
- return (NULL);
- }
- if(::OleLoadPicture(lpStream, dwSize, FALSE, IID_IPicture,(LPVOID*) &lpPicture) != S_OK)
- {
- GlobalFree(hMemJpeg);
- lpStream->Release();
- return (NULL);
- }
- /* Get the handle to the image, and then copy it. */
- if((lpPicture->get_Handle(&hJpegBmp)) != S_OK)
- {
- GlobalFree(hMemJpeg);
- lpStream->Release();
- lpPicture->Release();
- return (NULL);
- }
- lWidth = lHeight = 0;
- lpPicture->get_Width((OLE_XSIZE_HIMETRIC*)&lWidth);
- lpPicture->get_Height((OLE_XSIZE_HIMETRIC*)&lHeight);
- if((hBmp = (HBITMAP)CopyImage((HANDLE *) hJpegBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)) == NULL)
- {
- GlobalFree(hMemJpeg);
- lpStream->Release();
- lpPicture->Release();
- return (NULL);
- }
- /* Free the original image and memory. */
- GlobalFree(hMemJpeg);
- lpStream->Release();
- lpPicture->Release();
-
- return (hBmp);
- }
- BOOL CMainFrame::SaveBitmap(HDC hDC,HBITMAP hBitmap,CString szPath)
- {
- FILE * fp= NULL;
- fp = fopen(szPath.GetBuffer(1),"wb");
- if(fp == NULL)
- return false;
-
- BITMAP Bm;
- BITMAPINFO BitInfo;
- ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
- BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- BitInfo.bmiHeader.biBitCount = 0;
- if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
- return (false);
- Bm.bmHeight = BitInfo.bmiHeader.biHeight;
- Bm.bmWidth = BitInfo.bmiHeader.biWidth;
- BITMAPFILEHEADER BmHdr;
-
- BmHdr.bfType = 0x4d42; // 'BM' WINDOWS_BITMAP_SIGNATURE
- BmHdr.bfSize = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- BmHdr.bfReserved1 = BmHdr.bfReserved2 = 0;
- BmHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
-
- BitInfo.bmiHeader.biCompression = 0;
- // Writing Bitmap File Header ////
- fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);
- fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
- BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
- if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight, pData, &BitInfo, DIB_RGB_COLORS))
- return (false);
- if(pData != NULL)
- fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);
- fclose(fp);
- delete (pData);
- return (true);
- }
- void CMainFrame::OnTimeChange()
- {
- CFrameWnd::OnTimeChange();
- if(CheckDate() == CHANGE_ALL)
- {
- SetCalendar();
- }
- else
- if(CheckDate() == DAY_CHANGE)
- {
- CTime tm = CTime::GetCurrentTime();
- m_CurrentTime = tm;
- SetIcon(tm.GetDay());
- CString date = tm.Format("%A, %B %d, %Y");
- SetToolTip(date);
- }
- m_CurrentTime = CTime::GetCurrentTime();
- WriteRegistry();
- }
- void CMainFrame::ReadRegistry()
- {
- CString szSection(_T("Configuration"));
- CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
- m_szFontName = pApp->GetProfileString(szSection,_T("Font Name"),_T("Monotype Corsiva"));
- m_nFontSize = pApp->GetProfileInt(szSection,_T("Text Size"),24);
- m_crTextColor = pApp->GetProfileInt(szSection,_T("Text Color"),(INT)RGB(0,255,0));
- m_crBackColor = pApp->GetProfileInt(szSection,_T("Back Ground Color"),(INT)RGB(255,255,255));
- m_nTransparency = pApp->GetProfileInt(szSection,_T("Transparency"),88);
- m_nXPos = pApp->GetProfileInt(szSection,_T("StartX"),100);
- m_nYPos = pApp->GetProfileInt(szSection,_T("StartY"),100);
- }
- void CMainFrame::WriteRegistry()
- {
- CString szSection(_T("Configuration"));
- CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
- pApp->WriteProfileString(szSection,_T("Font Name"),m_szFontName);
- pApp->WriteProfileInt(szSection,_T("Text Size"),m_nFontSize);
- pApp->WriteProfileInt(szSection,_T("Text Color"),m_crTextColor);
- pApp->WriteProfileInt(szSection,_T("Back Ground Color"),m_crBackColor);
- pApp->WriteProfileInt(szSection,_T("Day"),m_CurrentTime.GetDay());
- pApp->WriteProfileInt(szSection,_T("Month"),m_CurrentTime.GetMonth());
- pApp->WriteProfileInt(szSection,_T("Year"),m_CurrentTime.GetYear());
- pApp->WriteProfileInt(szSection,_T("Transparency"),m_nTransparency);
- pApp->WriteProfileInt(szSection,_T("StartX"),m_nXPos);
- pApp->WriteProfileInt(szSection,_T("StartY"),m_nYPos);
- }
- void CMainFrame::SetIcon(INT nDate)
- {
- HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICON1+(nDate-1));
-
- if(hIcon)
- {
- if(NULL!= m_hCalIcon)
- ::DestroyIcon(m_hCalIcon);
- m_hCalIcon = hIcon;
- m_tnd.hIcon = m_hCalIcon;
- m_tnd.uFlags = NIF_ICON;
- Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
- }
- }
- void CMainFrame::SetToolTip(CString szToolTip)
- {
- strcpy(m_tnd.szTip,szToolTip.operator LPCTSTR());
- m_tnd.uFlags = NIF_TIP;
- Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
- }
- int CMainFrame::CheckDate()
- {
- CString szSection(_T("Configuration"));
- CDesktopCalendarApp * pApp = (CDesktopCalendarApp*)AfxGetApp();
- int nLastDay = pApp->GetProfileInt(szSection,_T("Day"),0);
- int nLastYear = pApp->GetProfileInt(szSection,_T("Year"),0);
- int nLastMonth = pApp->GetProfileInt(szSection,_T("Month"),0);
- CTime tm = CTime::GetCurrentTime();
- if(nLastDay == tm.GetDay() &&
- nLastMonth == tm.GetMonth() &&
- nLastYear == tm.GetYear())
- return (NO_CHANGE);
- if(nLastDay != tm.GetDay() &&
- nLastMonth == tm.GetMonth() &&
- nLastYear == tm.GetYear())
- return (DAY_CHANGE);
-
- return (CHANGE_ALL);
- }
- BOOL CMainFrame::DestroyWindow()
- {
- m_tnd.uFlags = 0;
- // Remove Icon
- Shell_NotifyIcon(NIM_DELETE, &m_tnd);
- WriteRegistry();
- if(NULL!= hMainBitmap)
- {
- DeleteObject(hMainBitmap);
- hMainBitmap = NULL;
- }
-
- return CFrameWnd::DestroyWindow();
- }
- LRESULT CMainFrame::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
- {
- // Install the icon again
- CTime t = CTime::GetCurrentTime();
- CString date = t.Format("%A, %B %d, %Y");
- if(NULL != m_hCalIcon)
- {
- DestroyIcon(m_hCalIcon);
- }
- m_hCalIcon = AfxGetApp()->LoadIcon(IDI_ICON1+t.GetDay()-1);
- m_tnd.cbSize = sizeof(NOTIFYICONDATA);
- m_tnd.hWnd = this->m_hWnd;
- m_tnd.uID = IDR_POP_UP;
- m_tnd.hIcon = m_hCalIcon;
- strcpy(m_tnd.szTip,date.operator LPCTSTR());
- m_tnd.uCallbackMessage = WM_ICON_NOTIFY;
- m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- Shell_NotifyIcon(NIM_ADD, &m_tnd);
- return (0L);
- }
- void CMainFrame::OnCalAboutbox()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
- void CMainFrame::OnCalOptions()
- {
- // TODO: Add your command handler code here
- COptionsDlg Dlg;
- Dlg.m_szFontName = m_szFontName;
- Dlg.m_crBackColor = m_crBackColor;
- Dlg.m_crTextColor = m_crTextColor;
- Dlg.m_nFontSize = m_nFontSize;
- Dlg.m_uXPos = m_nXPos;
- Dlg.m_uYPos = m_nYPos;
- Dlg.m_nTransparency = m_nTransparency;
- if(Dlg.DoModal() ==IDOK)
- {
- m_szFontName = Dlg.m_szFontName;
- m_crBackColor = Dlg.m_crBackColor;
- m_crTextColor = Dlg.m_crTextColor;
- m_nFontSize = Dlg.m_nFontSize;
- m_nXPos = Dlg.m_uXPos;
- m_nYPos = Dlg.m_uYPos;
- m_nTransparency = Dlg.m_nTransparency;
- SetCalendar();
- }
- }