📄 wmfview.cpp
字号:
/*****************************************************************
*
* Project.....: Metafile Viewer
* Application.: WMFVIEW.exe
* Module......: WMFVIEW.cpp
* Description.: Application main module
* Compiler....: MS Visual C++
* Written by..: D. Esposito
* Environment.: Windows 9x/NT
*
******************************************************************/
/*---------------------------------------------------------------*/
// PRAGMA section
/*---------------------------------------------------------------*/
// Force the linker to add the following libraries.
#ifdef _MSC_VER
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "comdlg32.lib")
#endif
/*---------------------------------------------------------------*/
// INCLUDE section
/*---------------------------------------------------------------*/
#include "WMFView.h"
#include <commctrl.h>
#include <commdlg.h>
#include <shellapi.h>
#include <shlwapi.h>
#include "resource.h"
/*---------------------------------------------------------------*/
// GLOBAL section
/*---------------------------------------------------------------*/
// Structures
// Needed to handle 16-bit placeable metafiles
#pragma pack(push)
#pragma pack(2)
typedef struct
{
DWORD dwKey;
WORD hmf;
SMALL_RECT bbox;
WORD wInch;
DWORD dwReserved;
WORD wCheckSum;
} APMHEADER, *LPAPMHEADER;
#pragma pack(pop)
// Data
HICON g_hIconLarge;
HICON g_hIconSmall;
TCHAR g_szCurFile[MAX_PATH];
// Functions
void OnInitDialog(HWND, LPARAM);
void OnOK(HWND);
void DisplayMetaFile(HWND, LPTSTR);
HENHMETAFILE GetMetaFileHandle(LPTSTR);
void PrintMetaFile(LPTSTR);
void SaveMetaFile(LPTSTR);
void SaveToEMF(HENHMETAFILE, LPTSTR);
void SaveToWMF(HENHMETAFILE, LPTSTR);
void OnOpen(HWND);
void OnPrint(HWND);
void OnSave(HWND);
void RefreshUI(HWND, LPTSTR);
void ParseCommandLine(HWND, LPTSTR);
HWND AnotherInstanceRunning();
void HandleFileDrop(HWND, HDROP);
// Callbacks
BOOL CALLBACK APP_DlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK CheckRunningApps(HWND, LPARAM);
/*---------------------------------------------------------------*/
// Procedure....: WinMain()
// Description..: Entry point in any Windows program
// Input........: HINSTANCE, HINSTANCE, LPSTR, int
// Output.......: int
/*---------------------------------------------------------------*/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevious,
LPTSTR lpsz, int iCmd)
{
// Is there another running instance of this?
HWND hwnd = AnotherInstanceRunning();
if(IsWindow(hwnd))
{
// Bring the previous window to the top
if(IsIconic(hwnd))
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
// Parse "this" command line but send messages to the prev. window
ParseCommandLine(hwnd, lpsz);
// Now we can exit
return 1;
}
// Save global data
g_hIconLarge = static_cast<HICON>(
LoadImage(hInstance, "APP_ICON", IMAGE_ICON,
GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0));
g_hIconSmall = static_cast<HICON>(
LoadImage(hInstance, "APP_ICON", IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0));
lstrcpy(g_szCurFile, "");
// Enable common controls
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&iccex);
// Run main dialog
BOOL b = DialogBoxParam(hInstance, "DLG_MAIN", NULL, APP_DlgProc, reinterpret_cast<LPARAM>(lpsz));
// Exit
DestroyIcon(g_hIconLarge);
DestroyIcon(g_hIconSmall);
return b;
}
/*---------------------------------------------------------------*/
// Procedure....: APP_DlgProc()
// Description..: Responds to all messages sent to the dialog
// Input........: HWND, UINT, WPARAM, LPARAM
// Output.......: BOOL
/*---------------------------------------------------------------*/
BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch(uiMsg)
{
case WM_INITDIALOG:
OnInitDialog(hDlg, lParam);
break;
case WM_DROPFILES:
HandleFileDrop(hDlg, reinterpret_cast<HDROP>(wParam));
break;
case WM_EX_DISPLAYMETA:
DisplayMetaFile(GetDlgItem(hDlg, IDC_METAFILE), reinterpret_cast<LPTSTR>(lParam));
RefreshUI(hDlg, reinterpret_cast<LPTSTR>(lParam));
break;
case WM_EX_PRINTMETA:
PrintMetaFile(reinterpret_cast<LPTSTR>(lParam));
break;
case WM_EX_SAVEMETA:
SaveMetaFile(reinterpret_cast<LPTSTR>(lParam));
break;
case WM_COMMAND:
switch(wParam)
{
case ID_FILE_OPEN:
OnOpen(hDlg);
return FALSE;
case ID_FILE_PRINT:
OnPrint(hDlg);
return FALSE;
case ID_FILE_SAVEAS:
OnSave(hDlg);
return FALSE;
case ID_FILE_EXIT:
case IDCANCEL:
EndDialog(hDlg, FALSE);
return FALSE;
}
break;
}
return FALSE;
}
/*****************************************************************
*
* Internals:
* - OnOK()
* - OnInitDialog()
*
******************************************************************/
/*---------------------------------------------------------------*/
// Procedure...: OnOK()
// Description.: Do something
// INPUT.......: HWND
// OUTPUT......: void
/*---------------------------------------------------------------*/
void OnOK(HWND hDlg)
{
return;
}
/*---------------------------------------------------------------*/
// Procedure...: OnInitDialog()
// Description.: Initialize the dialog
// INPUT.......: HWND
// OUTPUT......: void
/*---------------------------------------------------------------*/
void OnInitDialog(HWND hDlg, LPARAM lParam)
{
// Set the icons (T/F as to Large/Small icon)
SendMessage(hDlg, WM_SETICON, FALSE, reinterpret_cast<LPARAM>(g_hIconSmall));
SendMessage(hDlg, WM_SETICON, TRUE, reinterpret_cast<LPARAM>(g_hIconLarge));
if(lstrlen(reinterpret_cast<LPTSTR>(lParam)))
ParseCommandLine(hDlg, reinterpret_cast<LPTSTR>(lParam));
}
void DisplayMetaFile(HWND hwndMeta, LPTSTR szFile)
{
// Get the metafile handle
HENHMETAFILE hemf = GetMetaFileHandle(szFile);
if(hemf == NULL)
{
MessageBox(NULL, __TEXT("Unable to handle the file."), szFile, MB_OK | MB_ICONSTOP);
return;
}
// Free the old file and display the new one
HENHMETAFILE hemfOld = reinterpret_cast<HENHMETAFILE>(
SendMessage(hwndMeta, STM_GETIMAGE, IMAGE_ENHMETAFILE, 0));
if(hemfOld)
DeleteEnhMetaFile(hemfOld);
// hwndMeta is a Picture control
SendMessage(hwndMeta, STM_SETIMAGE, IMAGE_ENHMETAFILE, reinterpret_cast<LPARAM>(hemf));
lstrcpy(g_szCurFile, szFile);
}
HENHMETAFILE GetMetaFileHandle(LPTSTR szFile)
{
DWORD dwSize = 0;
LPBYTE pb = NULL;
// Try to read it as an EMF
HENHMETAFILE hEMF = GetEnhMetaFile(szFile);
if(hEMF)
return hEMF;
// Try to read it as a WMF
HMETAFILE hWMF = GetMetaFile(szFile);
if(hWMF)
{
dwSize = GetMetaFileBitsEx(hWMF, 0, NULL);
if(dwSize == 0)
{
DeleteMetaFile(hWMF);
return NULL;
}
// Allocate enough memory
pb = new BYTE[dwSize];
if(pb == NULL)
{
DeleteMetaFile(hWMF);
return NULL;
}
// Get the metafile bits
dwSize = GetMetaFileBitsEx(hWMF, dwSize, pb);
if(dwSize == 0)
{
delete [] pb;
DeleteMetaFile(hWMF);
return NULL;
}
// Convert to EMF
hEMF = SetWinMetaFileBits(dwSize, pb, NULL, NULL);
// Clean up
DeleteMetaFile(hWMF);
delete [] pb;
return hEMF;
}
// Try to handle the input as a placeable metafile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -