SendDlg.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:6k
- // SendDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "SimpleMail.h"
- #include "ExpandEditCtrl.h"
- #include "SendDlg.h"
- #include "ImageDataObject.h"
- // CSendDlg dialog
- IMPLEMENT_DYNAMIC(CSendDlg, CDialog)
- CSendDlg::CSendDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSendDlg::IDD, pParent)
- , m_strRcps(_T(""))
- , m_strTopic(_T(""))
- , m_strContent(_T(""))
- , m_strCCRcps(_T(""))
- , m_strBCCRcps(_T(""))
- , m_pEditAttach(NULL)
- , m_pRichEditOle(NULL)
- {
- }
- CSendDlg::~CSendDlg()
- {
- if (NULL != m_pRichEditOle)
- {
- //m_pRichEditOle->Release();
- }
-
- if (NULL != m_pEditAttach)
- {
- delete m_pEditAttach;
- m_pEditAttach = NULL;
- }
- }
- void CSendDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDIT_RCP, m_strRcps);
- DDX_Text(pDX, IDC_EDIT_TOPIC, m_strTopic);
- DDX_Text(pDX, IDC_CONTENT, m_strContent);
- DDX_Text(pDX, IDC_EDIT_CC, m_strCCRcps);
- DDX_Text(pDX, IDC_EDIT_BCC, m_strBCCRcps);
- }
- BEGIN_MESSAGE_MAP(CSendDlg, CDialog)
- ON_BN_CLICKED(IDC_BTN_SEND, &CSendDlg::OnBnClickedBtnSend)
- ON_BN_CLICKED(IDC_BTN_ACCESSORY, &CSendDlg::OnBnClickedBtnAccessory)
- ON_WM_DROPFILES()
- ON_MESSAGE(WM_DEL_FILE, &CSendDlg::DeleteAttachFile)
- END_MESSAGE_MAP()
- // CSendDlg message handlers
- void CSendDlg::OnBnClickedBtnSend()
- {
- // TODO: Add your control notification handler code here
- CDialog::OnOK();
- }
- void CSendDlg::OnBnClickedBtnAccessory()
- {
- char szFilters[]=
- "All Files (*.*)|*.*||";
- CFileDialog fileDlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, szFilters);
- if(fileDlg.DoModal() == IDOK)
- {
- CString strPathName = fileDlg.GetPathName();
- m_arrAttachFiles.Add(strPathName);
- //create a edit to display the file attached
- if (NULL == m_pEditAttach)
- {
- CreateEdit();
- }
- UpdateEditContent(strPathName);
- }
- }
- BOOL CSendDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- DragAcceptFiles();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CSendDlg::OnDropFiles(HDROP hDropInfo)
- {
- //obtain the path
- TCHAR szFile[MAX_PATH] = {0};
- DragQueryFile(hDropInfo, 0, szFile, MAX_PATH);
- m_arrAttachFiles.Add(szFile);
- DragFinish(hDropInfo);
- //create a edit to display the file attached
- if (NULL == m_pEditAttach)
- {
- CreateEdit();
- }
- UpdateEditContent(szFile);
- CDialog::OnDropFiles(hDropInfo);
- }
- ///<summary>
- /// delete the specified file
- ///</summary>
- LRESULT CSendDlg::DeleteAttachFile(WPARAM wParam, LPARAM lParam)
- {
- ASSERT((wParam >= 0) && (wParam <= (UINT)m_arrAttachFiles.GetUpperBound()));
- m_arrAttachFiles.RemoveAt(wParam);
- return HRESULT_SUCCESS;
- }
- ///<summary>
- /// create the edit for the accessory
- ///</summary>
- void CSendDlg::CreateEdit()
- {
- //reduce the origin edit
- CRect contentRect;
- GetDlgItem(IDC_CONTENT)->GetWindowRect(&contentRect);
- ScreenToClient(&contentRect);
- GetDlgItem(IDC_CONTENT)->SetWindowPos(&wndTop, contentRect.left, contentRect.top,
- contentRect.Size().cx, contentRect.Size().cy * 2 / 3, SWP_SHOWWINDOW);
- CRect attachRect(contentRect.left, contentRect.bottom - contentRect.Height() / 3,
- contentRect.right, contentRect.bottom);
- m_pEditAttach = new CExpandEditCtrl;
- m_pEditAttach->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE
- | WS_TABSTOP | WS_BORDER | WS_VSCROLL,
- attachRect, this, IDC_ATTACH);
- ::RevokeDragDrop(m_pEditAttach->m_hWnd);
- if (NULL == m_pRichEditOle)
- {
- m_pRichEditOle = m_pEditAttach->GetIRichEditOle();
- ASSERT(m_pRichEditOle != NULL);
- if (NULL == m_pRichEditOle)
- {
- return;
- }
- }
- }
- ///<summary>
- /// update the edit of the accessory
- ///</summary>
- void CSendDlg::UpdateEditContent(LPCTSTR pszPath)
- {
- ASSERT(pszPath != NULL);
- if (NULL == pszPath)
- {
- return;
- }
- //get the file name
- SHFILEINFO stFileInfo;
- memset(&stFileInfo, 0, sizeof(stFileInfo));
- if (SHGetFileInfo(pszPath, 0 , &stFileInfo, sizeof(stFileInfo), SHGFI_DISPLAYNAME) == 0)
- {
- ASSERT(FALSE);
- return;
- }
- //get the file size
- WIN32_FIND_DATA fileInfo;
- HANDLE hFind = NULL;
- int nFileSize = 0;
- hFind = FindFirstFile(pszPath, &fileInfo);
- if(hFind != INVALID_HANDLE_VALUE)
- {
- nFileSize = fileInfo.nFileSizeLow / 1024 + 1;
- }
- else
- {
- ASSERT(FALSE);
- return;
- }
- FindClose(hFind);
- //Draw the file icon with the file name and the file size
- CString strName;
- strName.Format("%s (%dK)rn", stFileInfo.szDisplayName, nFileSize);
- DrawFileIcon(pszPath, strName);
- }
- ///<summary>
- /// format the image with the file infomation
- ///</summary>
- HBITMAP CSendDlg::FormatImage(CImageList& list, const int num, LPCTSTR pszName)
- {
- //Get the image infomation to decide the size
- IMAGEINFO ii;
- list.GetImageInfo(num, &ii);
- int nWidth = ii.rcImage.right - ii.rcImage.left;
- int nHeight = ii.rcImage.bottom - ii.rcImage.top;
- //Draw the bmp in the memory
- CBitmap dist;
- CClientDC dc(NULL);
- CDC memDC;
- memDC.CreateCompatibleDC(&dc);
- CFont Font;
- Font.CreatePointFont(80, "Microsoft Sans Serif");
- CFont* pFont = memDC.SelectObject(&Font);
- pFont->DeleteObject();
- CSize sizeName = memDC.GetTextExtent(pszName);
- dist.CreateCompatibleBitmap(&dc, sizeName.cx + nWidth * 3, nHeight);//nWidth * 3 is used to insert some blanks
- CBitmap* pOldBitmap = memDC.SelectObject(&dist);
- memDC.FillSolidRect(0, 0, sizeName.cx + nWidth * 3, nHeight,
- m_pEditAttach->SetBackgroundColor(TRUE, 0));
- list.Draw(&memDC, num, CPoint(0, 0), ILD_NORMAL);
- memDC.DrawText(pszName, CRect(nWidth + 1, 0, sizeName.cx, nHeight), DT_NOCLIP);
- memDC.SelectObject(pOldBitmap);
- return (HBITMAP)dist.Detach();
- }
- ///<summary>
- /// Draw the file icon with the file name and the file size
- ///</summary>
- void CSendDlg::DrawFileIcon(LPCTSTR pszPath, LPCTSTR pszName)
- {
- m_pEditAttach->PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
- //extract the file icon
- SHFILEINFO stFileInfo;
- memset(&stFileInfo, 0, sizeof(stFileInfo));
- HIMAGELIST s_himl = NULL;
- s_himl = (HIMAGELIST)SHGetFileInfo(pszPath, 0, &stFileInfo,
- sizeof(stFileInfo), SHGFI_SYSICONINDEX| SHGFI_SMALLICON);
- if (NULL == s_himl)
- {
- return;
- }
- //draw the icon
- CImageList tmpList;
- tmpList.Attach(s_himl);
- HBITMAP hBmp = NULL;
- hBmp = FormatImage(tmpList, stFileInfo.iIcon, pszName);
- if (hBmp)
- {
- CImageDataObject::InsertBitmap(m_pRichEditOle, hBmp);
- }
- tmpList.Detach();
- }