VS2012下自定义打开文件对话框,MFC的CFileDialog封装了太多,太复杂,绕得头晕,自己封装一个得了

#pragma once

#include <objbase.h>
#include <commdlg.h> #include "ImagePreviewStatic.h" // XFileDialog class XFileDialog : public CWnd
{
DECLARE_DYNAMIC(XFileDialog) public:
XFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL, LPCTSTR lpszInitFolder = NULL,
CWnd* pParentWnd = NULL);
virtual ~XFileDialog(); public:
void EndDialog(int nResult); virtual BOOL OnInitDialog();
virtual long DoModal();
virtual void DoDataExchange(CDataExchange *pDX);
virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult); virtual void ProcFileChange(TCHAR* strFullName); protected:
OPENFILENAME m_ofn;
BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
CString m_strFilter; // filter string
TCHAR m_szFileTitle[]; // contains file title after return
TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return CWnd* m_pParentWnd; // parent/owner window
HWND m_hWndTop; // top level parent window (may be disabled) CImagePreviewStatic m_preview; DECLARE_MESSAGE_MAP()
};
 // XFileDialog.cpp : implementation file
// #include "stdafx.h"
#include "XFileDialog.h"
#include "resource.h" // XFileDialog UINT_PTR CALLBACK OFNHookProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); UINT_PTR CALLBACK OFNHookProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (hWnd == NULL)
return ;
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
if (pThreadState->m_pAlternateWndInit != NULL && CWnd::FromHandlePermanent(hWnd) == NULL)
{
ASSERT_KINDOF(XFileDialog, pThreadState->m_pAlternateWndInit);
pThreadState->m_pAlternateWndInit->SubclassWindow(hWnd);
pThreadState->m_pAlternateWndInit = NULL;
} if (message == WM_INITDIALOG)
{
/*
_afxMsgLBSELCHANGE = ::RegisterWindowMessage(LBSELCHSTRING);
_afxMsgSHAREVI = ::RegisterWindowMessage(SHAREVISTRING);
_afxMsgFILEOK = ::RegisterWindowMessage(FILEOKSTRING);
_afxMsgCOLOROK = ::RegisterWindowMessage(COLOROKSTRING);
_afxMsgHELP = ::RegisterWindowMessage(HELPMSGSTRING);
_afxMsgSETRGB = ::RegisterWindowMessage(SETRGBSTRING);
*/ XFileDialog* pDlg = DYNAMIC_DOWNCAST(XFileDialog, CWnd::FromHandlePermanent(hWnd));
if (pDlg != NULL)
return pDlg->OnInitDialog();
else
return ;
} return ;
} BEGIN_MESSAGE_MAP(XFileDialog, CWnd)
END_MESSAGE_MAP() XFileDialog::XFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, LPCTSTR lpszInitFolder, CWnd* pParentWnd) :
CWnd()
{
m_szFileName[] = '\0';
m_szFileTitle[] = '\0';
m_bOpenFileDialog = bOpenFileDialog; m_pParentWnd = NULL;
m_hWndTop = NULL; memset(&m_ofn, , sizeof(OPENFILENAME)); // initialize structure to 0/NULL m_ofn.Flags |= dwFlags | OFN_ENABLETEMPLATE |OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK;
m_ofn.lpstrTitle = _T("图像文件预览对话框");
m_ofn.lpstrInitialDir = lpszInitFolder; m_ofn.lStructSize = sizeof(OPENFILENAME);
m_ofn.lpstrFile = m_szFileName;
m_ofn.nMaxFile = _countof(m_szFileName);
m_ofn.lpstrDefExt = lpszDefExt;
m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
m_ofn.nMaxFileTitle = _countof(m_szFileTitle);
if(dwFlags & OFN_ENABLETEMPLATE)
m_ofn.Flags &= ~OFN_ENABLESIZING;
m_ofn.hInstance = AfxGetResourceHandle();
m_ofn.lpfnHook = (LPOFNHOOKPROC)OFNHookProc;
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_IMAGEPREVIEWDLG); // setup initial file name
if (lpszFileName != NULL)
Checked::tcsncpy_s(m_szFileName, _countof(m_szFileName), lpszFileName, _TRUNCATE); // Translate filter into commdlg format (lots of \0)
if (lpszFilter != NULL)
{
m_strFilter = lpszFilter;
LPTSTR pch = m_strFilter.GetBuffer(); // modify the buffer in place
// MFC delimits with '|' not '\0'
while ((pch = _tcschr(pch, '|')) != NULL)
*pch++ = '\0';
m_ofn.lpstrFilter = m_strFilter;
// do not call ReleaseBuffer() since the string contains '\0' characters
}
} XFileDialog::~XFileDialog()
{
} void XFileDialog::EndDialog(int nResult)
{
ASSERT(::IsWindow(m_hWnd)); ::EndDialog(m_hWnd, nResult);
} BOOL XFileDialog::OnInitDialog()
{
// transfer data into the dialog from member variables
#if 1
if (!UpdateData(FALSE))
{
TRACE(traceAppMsg, , "Warning: UpdateData failed during dialog init.\n");
EndDialog(-);
return FALSE;
}
#endif GetDlgItem(IDC_IMAGEPREVIEW)->ModifyStyle ( SS_TYPEMASK, SS_OWNERDRAW ); return TRUE; // set focus to first one
} void XFileDialog::DoDataExchange(CDataExchange *pDX)
{
CWnd::DoDataExchange(pDX);
DDX_Control(pDX, IDC_IMAGEPREVIEW, m_preview);
} void XFileDialog::ProcFileChange(TCHAR* strFullName){
//如果是文件名
DWORD nFileAtts = GetFileAttributes(strFullName);
if ((FILE_ATTRIBUTE_NORMAL == nFileAtts) ||
( == (nFileAtts & (FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_DIRECTORY )) ) ){
m_preview.SetFilename(strFullName);
} else {
//m_preview.SetFilename(NULL);
}
} BOOL XFileDialog::OnNotify(WPARAM, LPARAM lp, LRESULT *pResult)
{
LPOFNOTIFY of = (LPOFNOTIFY) lp;
CString csTemp;
TCHAR strFileName[_MAX_PATH]; HWND hParent;
UINT nfiles; switch (of->hdr.code)
{
case CDN_SELCHANGE:
hParent = GetParent()->GetSafeHwnd();
nfiles = CommDlg_OpenSave_GetFilePath(hParent, strFileName, _MAX_PATH);
if (nfiles > ) {
ProcFileChange(strFileName);
//MessageBox(strFileName);
}
break; case CDN_FOLDERCHANGE:
// Once we get this notification our old subclassing of
// the SHELL window is lost, so we have to
// subclass it again. (Changing the folder causes a
// destroy and recreate of the SHELL window).
//if (m_wndHook.GetSafeHwnd() != HWND(NULL))
// m_wndHook.UnsubclassWindow(); //m_wndHook.SubclassWindow(GetParent()->GetDlgItem(lst2)->GetSafeHwnd());
//UpdatePreview(_T(""));
break;
} *pResult = ;
return FALSE;
} long XFileDialog::DoModal()
{
HWND hWndFocus = ::GetFocus();
BOOL bEnableParent = FALSE;
// allow OLE servers to disable themselves
CWinApp* pApp = AfxGetApp();
if (pApp != NULL)
pApp->EnableModeless(FALSE); _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
pThreadState->m_pAlternateWndInit = this;
//AfxHookWindowCreate(this); HWND hWndParent = CWnd::GetSafeOwner_(m_pParentWnd->GetSafeHwnd(), &m_hWndTop);;
if (hWndParent && hWndParent != ::GetDesktopWindow() && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
} INT_PTR nResult = ; if (m_bOpenFileDialog)
nResult = GetOpenFileName(&m_ofn);
else
nResult = GetSaveFileName(&m_ofn); if (nResult)
ASSERT(pThreadState->m_pAlternateWndInit == NULL); // Second part of special case for file open/save dialog.
if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (::IsWindow(hWndFocus))
::SetFocus(hWndFocus); AfxUnhookWindowCreate(); // just in case
Detach(); // just in case if (pApp != NULL)
pApp->EnableModeless(TRUE); return nResult ? nResult : IDCANCEL;
} IMPLEMENT_DYNAMIC(XFileDialog, CWnd) // XFileDialog message handlers
ImagePreviewStatic.h其实就是一个CStatic的继承类,实现图片预览
 /*
* $Header: $
*
* $History: $
*/
#pragma once #include <atlimage.h> // CImagePrieviewStatic
class CImagePreviewStatic : public CStatic
{
DECLARE_DYNAMIC(CImagePreviewStatic)
public:
CImagePreviewStatic();
virtual ~CImagePreviewStatic(); virtual BOOL Create();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); void SetFilename(LPCTSTR szFilename); protected:
WCHAR m_wsFilename[_MAX_PATH];
Image *m_img;
Graphics *m_graphics;
CImage *m_img2; DECLARE_MESSAGE_MAP()
};
 /*
* $Header: $
*
* $History: $
*/
#include "stdafx.h"
#include "ImagePreviewStatic.h" // CImagePrieviewStatic
IMPLEMENT_DYNAMIC(CImagePreviewStatic, CStatic) CImagePreviewStatic::CImagePreviewStatic() : CStatic()
{
m_img = (Image *) NULL;
m_graphics = (Graphics *) NULL;
m_img2 = (CImage*) NULL;
} CImagePreviewStatic::~CImagePreviewStatic()
{
if (m_img) {
delete m_img;
}
if (m_graphics) {
delete m_graphics;
}
if (m_img2) {
delete m_img2;
}
} BOOL CImagePreviewStatic::Create()
{
if (GetSafeHwnd() != HWND(NULL))
{
m_img = new Image(m_wsFilename);
m_graphics = new Graphics(GetSafeHwnd());
return TRUE;
} return FALSE;
} void CImagePreviewStatic::SetFilename(LPCTSTR szFilename)
{
#ifndef _UNICODE
USES_CONVERSION;
#endif ASSERT(szFilename);
ASSERT(AfxIsValidString(szFilename)); TRACE("%s\n", szFilename); #ifndef _UNICODE
wcscpy(m_wsFilename, A2W(szFilename));
#else
wcscpy(m_wsFilename, szFilename);
#endif //delete m_img;
//m_img = new Image(m_wsFilename, FALSE);
if (m_img2) {
delete m_img2;
}
m_img2 = new CImage();
m_img2->Load(szFilename); Invalidate();
} void CImagePreviewStatic::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{
Unit units;
CRect rect; /*
if (m_img != NULL)
{
GetClientRect(&rect); RectF destRect(REAL(rect.left), REAL(rect.top), REAL(rect.Width()), REAL(rect.Height())),
srcRect;
m_img->GetBounds(&srcRect, &units);
m_graphics->DrawImage(m_img, destRect, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, UnitPixel, NULL);
}
*/
if (m_img2 != NULL) {
HWND hWnd = GetParent()->m_hWnd;
HDC hDc = ::GetDC(hWnd);
GetWindowRect(&rect);
::ScreenToClient(hWnd, (LPPOINT)&rect);
::ScreenToClient(hWnd, (LPPOINT)(&rect) + );
//获取到HDC
m_img2->Draw(hDc,rect);
}
} BEGIN_MESSAGE_MAP(CImagePreviewStatic, CStatic)
END_MESSAGE_MAP() // CImagePrieviewStatic message handlers
其中的对话框资源
IDD_IMAGEPREVIEWDLG DIALOGEX 0, 0, 365, 177
STYLE DS_SETFONT | DS_3DLOOK | DS_FIXEDSYS | DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
CONTROL "",IDC_IMAGEPREVIEW,"Static",SS_OWNERDRAW,7,7,351,163
END

VS2012下自定义打开文件对话框的更多相关文章

  1. java中文件保存、打开文件对话框

    package com.soft.test; //AWT: FileDialog类 + FilenameFilter类 可以实现本功能 //Swing: JFileChooser类 + FileFil ...

  2. WebBrowser自动填充打开文件对话框

    WebBrowser自动填充打开文件对话框   在使用WebBrowser编写自动表单填写软件的时候,不知道大家是否遇到国填写文件选择表单的情况.遇到这种情况的时候,无法直接队Html元素赋值,必须模 ...

  3. MFC程序打开文件对话框出错的问题解决

    前几天从网上下了个图像分析的mfc小程序,是VC6的 用VC6在本地编译生成都没问题.执行起来弹出一个未处理的错误,程序崩溃退出. 想起来原来遇到过打开文件对话框方面的问题,当时项目时间紧张未能深究. ...

  4. QT 打开文件对话框汇总

    Qstring fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ",  tr( ...

  5. C# OpenFileDialog打开文件对话框(详解)

    一.打开文件对话框(OpenFileDialog) 1. OpenFileDialog控件的基本属性 InitialDirectory:对话框的初始目录 Filter: 获取或设置当前文件名筛选器字符 ...

  6. [javaSE] GUI(打开文件对话框)

    使用FileDialog可以打开文件对话框,根据模式不同,可以分为打开文件和保存文件对话框 获取FileDialog对象,new出来,参数:所属Frame对象,String的标题,FileDialog ...

  7. Qt打开文件对话框

    项目中需要打开文件对话框,就查了一下,不得不说Qt的帮助文档做的真好,非常详细.要实现这个功能有两种方式,使用QFileDialog的静态方法,实例化QFileDialog对象. 基本算是照抄帮助文档 ...

  8. win10 只要打开文件对话框就卡死解决方法

    我电脑的问题是:win10系统,只要打开 文件对话框就卡死,假死,cpu100% 一直没有解决,但是只要把缩略图关了,就ok. 但是又想要留着缩略图,还是得显示,于是乎一直在找解决办法. 此方法好像可 ...

  9. 打开文件对话框在xp和win7上的实现文件任意多选

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在xp系统上进行文件多选,实际上其文件字符串数组的缓冲区是有限,并不能支持选择任意多个文件,为此以前我还写过一篇文章: ...

随机推荐

  1. install stackless python on ubuntu

    前言 我准备用stackless模拟游戏玩家登陆/注册等行为,测试游戏服务器的性能. 但是在安装stackless的过程中遇到了很多问题,特此记录下来,也分享给需要的朋友. 关于stackless S ...

  2. 6.css3定位--position

    ⑴Static默认值,没有定位. ⑵Absolute绝对定位.后面的元素会补上原来偏移的位置. ⑶Relative相对定位.后面的元素不会补上原来偏移的位置. ⑷Fixed绝对定位.相对于浏览器窗口固 ...

  3. 关于express 连接 mongodb数据库报错

    关于express 连接 mongodb数据库报错 nodejs DeprecationWarning: current URL string parser is deprecated, and wi ...

  4. centos虚拟机配置网卡连接

    本地连接 centos虚拟机连接设置: 换过ip之后需要重启网络服务新ip才生效 #service network restart 修改dns:

  5. .net Core——SqlSugar使用

    一.DbContext配置 public class DbContext { public DbContext() { Db = new SqlSugarClient(new ConnectionCo ...

  6. springboot之学习搭建

    什么是**SpringBoot?** Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配 ...

  7. Decision Trees 决策树

    Decision Trees (DT)是用于分类和回归的非参数监督学习方法. 目标是创建一个模型,通过学习从数据特征推断出的简单决策规则来预测目标变量的值. 例如,在下面的例子中,决策树从数据中学习用 ...

  8. 组件的属性props

    组件的属性props 是不能自己改的,一般是从外面传进来.在组件的视图中用,this.props.XXX 表示该组件的XXX 属性

  9. mysql CHECK约束 语法

    mysql CHECK约束 语法 作用:CHECK 约束用于限制列中的值的范围. 直线电机 说明:如果对单个列定义 CHECK 约束,那么该列只允许特定的值.如果对一个表定义 CHECK 约束,那么此 ...

  10. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...