⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedialogex.cpp

📁 从CDialog派生的扩展对话框例子
💻 CPP
字号:
// FileDialogEx.cpp : implementation file
//

#include "stdafx.h"
#include "PreviewDialog.h"
#include "FileDialogEx.h"
#include "Picture.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



//前置声明---------------
BOOL NEAR CALLBACK HandleNotify(HWND hDlg, LPOFNOTIFY pofn);
LRESULT CALLBACK ComDlgPreviewProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

/////////////////////////////////////////////////////////////////////////////
// CFileDialogEx
IMPLEMENT_DYNAMIC(CFileDialogEx, CFileDialog)

CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
							 DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
	//这个四个常用标识位
	m_ofn.Flags |= OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE |OFN_HIDEREADONLY; 
	m_ofn.lpstrTitle   =  _T("打开图片预览对话框");
	m_ofn.hInstance = AfxGetInstanceHandle();
	m_ofn.lpstrFilter  =  _T(".JPG .JPEG\0*.jpg;*.jpeg\0.STL\0*.stl\0所有文件 \0 *.*\0 ");
	m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_DIALOG_HOOK);
	m_ofn.lpfnHook =  (LPOFNHOOKPROC)ComDlgPreviewProc;
}

void CFileDialogEx::ShowImagepreview(HWND hDlg, char *ImagePath)
{
//	CString strTemp;

	RECT rect;
	
	//如果用的是Win32的SDK,前面要加:: 
	HDC hDC = ::GetDC(::GetDlgItem(hDlg, IDC_PIC));        //Get the DC for the CPicture Box
	::GetClientRect(::GetDlgItem(hDlg, IDC_PIC), &rect);   //Get dimensions of it
	
	CPicture  pic;
	//Try to load the Image..if not then we should return
	if(!pic.LoadPicture(ImagePath))
		return;
	//set the width & height labels
	::SetDlgItemInt(hDlg, IDC_W, pic._GetWidth(), FALSE);
	::SetDlgItemInt(hDlg, IDC_H, pic._GetHeight(), FALSE);
	
	//Draw the image in the picture box
	//(could be updated to maintain aspect ratio)
	pic.DrawPicture(hDC, 0, 0, rect.right - rect.left ,rect.bottom - rect.top );
	::ReleaseDC(::GetDlgItem(hDlg, IDC_PIC), hDC);
}

BEGIN_MESSAGE_MAP(CFileDialogEx, CFileDialog)
	//{{AFX_MSG_MAP(CFileDialogEx)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//Hook function for the Comm Dlg
//在这里处理我们感兴趣的消息,想要哪个,截哪个!
LRESULT CALLBACK ComDlgPreviewProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:   //初始化对话框上的控件	
		if(SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK,0,0) == BST_UNCHECKED)
		{   
			HWND hWnd;
			hWnd = GetDlgItem(hDlg, IDC_PIC);
			::SetWindowText(hWnd, "\n\n\n\n    点击图象文件进行预览!");
		}
		break;
	
	case WM_DESTROY:    //消毁对话框
		{
			LPOPENFILENAME lpOFN = (LPOPENFILENAME)GetWindowLong(hDlg, DWL_USER);
			
		}
		break;
		
	case WM_COMMAND:   //这里处理,IDC_CHECK1命令
		if ((HIWORD(wParam) == BN_CLICKED) && (LOWORD(wParam) == IDC_CHECK1))
		{
			if(SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK, 0, 0) == BST_CHECKED)
			{
				InvalidateRect(GetDlgItem(hDlg, IDC_PIC), NULL, FALSE);
				::SetDlgItemInt(hDlg, IDC_W, 0, FALSE); 
				::SetDlgItemInt(hDlg, IDC_H, 0, FALSE); 
			}
		}
     
		//不选中,
		if(SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK,0,0) == BST_UNCHECKED)
		{   
			HWND hWnd;
			hWnd = GetDlgItem(hDlg, IDC_PIC);
			::SetWindowText(hWnd, "\n\n\n\n    点击图象文件进行预览!");
		}
		
		//选中
		if(SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK, 0, 0) == BST_CHECKED)
		{
			HWND hWnd;
			hWnd = GetDlgItem(hDlg, IDC_PIC);
			::SetWindowText(hWnd, "\n\n\n\n      处于非预览状态!");
		}
		
		if(LOWORD(wParam) == IDOK)
		{	
		}
		break;
		
	case WM_NOTIFY:
		HandleNotify(hDlg, (LPOFNOTIFY)lParam);
		
	default:		
		return FALSE;
	}
	return TRUE;
}

//这里处理notify 消息
BOOL NEAR CALLBACK HandleNotify(HWND hDlg, LPOFNOTIFY pofn)
{
	CFileDialogEx dlg(TRUE);
	switch (pofn->hdr.code)
	{
	case CDN_SELCHANGE:
		{
			char szFile[MAX_PATH];
			// Get the path of the selected file.
			if (CommDlg_OpenSave_GetFilePath(GetParent(hDlg), szFile, sizeof(szFile)) <= sizeof(szFile))
			{
				if(GetFileAttributes(szFile) !=  FILE_ATTRIBUTE_DIRECTORY)
				{			
					//Should we load the Pic
					if(SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK,0,0) == BST_UNCHECKED)
						dlg.ShowImagepreview(hDlg, szFile);	
				}
			}
		}
		break;
		
	case CDN_FILEOK:
		return FALSE;
		break;
		
	}
	
	return(TRUE);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -