📄 imagebrowse.cpp
字号:
#include "ImageBrowse.h"
#include "cPic.h"
#include "resource.h"
//Forward declarations
BOOL CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL NEAR CALLBACK HandleNotify(HWND , LPOFNOTIFY);
LRESULT CALLBACK ComDlgPreviewProc(HWND , UINT, WPARAM , LPARAM );
BOOL ShowOpenDialogPreview( HWND,TCHAR *);
void CenterScreen(HWND hDlg);
void ShowImagePreview(HWND,char *);
//global hInstance
HINSTANCE g_hInst;
//picture box background brush handle
HBRUSH hbrush;
//WinMain..Program Enty Point
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
g_hInst=hInstance; //save this in our global hInstance
hbrush = CreateSolidBrush(RGB(100,100,100)); //backcolor of picture box
//Modal Dialog box makes things easier
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_MAIN_DIALOG), HWND_DESKTOP, MainDlgProc);
//clean up
DeleteObject(hbrush);
return 0;
}
BOOL CALLBACK MainDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
TCHAR szFile[MAX_PATH] = "\0";
switch(iMessage)
{
case WM_INITDIALOG:
//Center our Dlg on the Screen
CenterScreen(hDlg);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
//Show the Comm Dialog
if(ShowOpenDialogPreview(hDlg,szFile))
//if we selected a file than let's show it
MessageBox(hDlg,szFile,"File Name",MB_OK);
break;
case IDCANCEL:
//Destroy Dlg
EndDialog(hDlg,0);
return TRUE;
}
return FALSE;
}
return FALSE;
}
//Center our Dlg on the Screen
void CenterScreen(HWND hDlg)
{
HDC tDC =GetDC(NULL);
RECT rect;
int w = GetDeviceCaps(tDC,HORZRES);
int h = GetDeviceCaps(tDC,VERTRES);
GetWindowRect(hDlg,&rect); //Get the whole window rect
int ww = rect.right - rect.left ;
int wh = rect.bottom - rect.top ;
SetWindowPos(hDlg,HWND_TOP,(w - ww)/2,(h - wh)/2,0,0,SWP_NOSIZE);//do not change dimensions just center screen
ReleaseDC(NULL,tDC);
}
//Hook function for the Comm Dlg
LRESULT CALLBACK ComDlgPreviewProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
// Save off the long pointer to the OPENFILENAME structure.
SetWindowLong(hDlg, DWL_USER, lParam);
break;
case WM_DESTROY:
{
LPOPENFILENAME lpOFN = (LPOPENFILENAME)GetWindowLong(hDlg, DWL_USER);
}
break;
//if the check box is checked than we don't need to load the Image
//just clean the picture box
case WM_COMMAND:
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); //width = 0
SetDlgItemInt(hDlg,IDC_H,0,FALSE); //height= 0
}
}
break;
//Sets the Picture box backcolor
case WM_CTLCOLORSTATIC:
if((HWND)lParam == GetDlgItem(hDlg,IDC_PIC))
{
SetBkColor((HDC)wParam,RGB(100,100,100));
//don't forget to return the Brush handle.
return (BOOL) hbrush;
}
break;
case WM_NOTIFY:
//Handles Notify messages
HandleNotify(hDlg, (LPOFNOTIFY)lParam);
default:
return FALSE;
}
return TRUE;
}
//Call back to handle notify messages
BOOL NEAR CALLBACK HandleNotify(HWND hDlg, LPOFNOTIFY pofn)
{
switch (pofn->hdr.code)
{
// The selection has changed.
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 it is a dirctory than we don't care
if(GetFileAttributes(szFile) != FILE_ATTRIBUTE_DIRECTORY)
{
//Should we load the Pic
if(SendDlgItemMessage(hDlg,IDC_CHECK1,BM_GETCHECK,0,0) == BST_UNCHECKED)
//oh yes..go on..
ShowImagePreview(hDlg,szFile);
}
}
}
break;
case CDN_FILEOK:
//return FALSE to close the Comm Dlg
return FALSE;
break;
}
return(TRUE);
}
//Shows the Comm Dialog
BOOL ShowOpenDialogPreview(HWND hWnd,TCHAR * szFile)
{
OPENFILENAME OpenFileName;
// Fill in the OPENFILENAME structure to support a template and hook.
OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.hwndOwner = hWnd;
OpenFileName.hInstance = g_hInst;
OpenFileName.lpstrFilter = "All supported types\0*.JPG;*.BMP;*.GIF;*.JPE;*.JPEG;*.WMF\0JPEG Images\0*.JPG;*.JPE;*.JPEG\0GIF Images\0*.GIF\0BMP Images\0*.BMP\0\0";
OpenFileName.lpstrCustomFilter = NULL;
OpenFileName.nMaxCustFilter = 0;
OpenFileName.nFilterIndex = 0;
OpenFileName.lpstrFile = szFile;
OpenFileName.nMaxFile = MAX_PATH;
OpenFileName.lpstrFileTitle = NULL;
OpenFileName.nMaxFileTitle = 0;
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.lpstrTitle = "Open an Image File";
OpenFileName.nFileOffset = 0;
OpenFileName.nFileExtension = 0;
OpenFileName.lpstrDefExt = NULL;
OpenFileName.lCustData = NULL;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)ComDlgPreviewProc;
OpenFileName.lpTemplateName = MAKEINTRESOURCE(IDD_DIALOG_HOOK);
OpenFileName.Flags = OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE |OFN_HIDEREADONLY;
return GetOpenFileName(&OpenFileName);
}
//Loads the Image and draws it in the Picture Box(static control)
void ShowImagePreview(HWND hDlg,char * ImagePath)
{
RECT rect;
HDC tDC = GetDC(GetDlgItem(hDlg,IDC_PIC)); //Get the DC for the Picture Box
GetClientRect(GetDlgItem(hDlg,IDC_PIC),&rect);//Get dimensions of it
_PICTURE_ 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(tDC,0,0,rect.right - rect.left ,rect.bottom - rect.top );
ReleaseDC(GetDlgItem(hDlg,IDC_PIC),tDC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -