/*----------------------------------------
EMFVIEW.C -- View Enhanced Metafiles
(c) Charles Petzold, 1998
----------------------------------------*/
#include <windows.h>
#include <commdlg.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
TCHAR szAppName[] = TEXT ("EmfView") ;
HBRUSH hBrushBack ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HBITMAP hBitmap;
HACCEL hAccel ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
hBitmap = LoadBitmap(hInstance,TEXT("교쒼"));
if(NULL == hBitmap)
{
MessageBox (NULL, TEXT ("LoadBitmap Failed !\n")
TEXT ("(c) Charles Petzold, 2011"),
szAppName, MB_OK) ;
return -1;
}
hBrushBack = CreatePatternBrush(hBitmap);
DeleteObject(hBitmap);
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, TEXT("ICON")) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = hBrushBack ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindowEx (WS_EX_ACCEPTFILES, szAppName, TEXT ("Enhanced Metafile Viewer"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 400,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators (hInstance, szAppName) ;
while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
DeleteObject(hBrushBack);
return msg.wParam ;
}
HPALETTE CreatePaletteFromMetaFile (HENHMETAFILE hemf)
{
HPALETTE hPalette ;
int iNum ;
LOGPALETTE * plp ;
if (!hemf)
return NULL ;
if (0 == (iNum = GetEnhMetaFilePaletteEntries (hemf, 0, NULL)))
return NULL ;
plp = malloc (sizeof (LOGPALETTE) + (iNum - 1) * sizeof (PALETTEENTRY)) ;
plp->palVersion = 0x0300 ;
plp->palNumEntries = iNum ;
GetEnhMetaFilePaletteEntries (hemf, iNum, plp->palPalEntry) ;
hPalette = CreatePalette (plp) ;
free (plp) ;
return hPalette ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static DOCINFO di = { sizeof (DOCINFO), TEXT ("EmfView: Printing") } ;
static HENHMETAFILE hemf ;
static OPENFILENAME ofn ;
static PRINTDLG printdlg = { sizeof (PRINTDLG) } ;
static TCHAR szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
static TCHAR szFilter[] =
TEXT ("Enhanced Metafiles (*.EMF)\0*.emf\0")
TEXT ("All Files (*.*)\0*.*\0\0") ;
static HBITMAP hBitmapBack ;
static HDC hdcMem ;
static HPALETTE hPalette, hPaletteOld ;
BOOL bSuccess ;
ENHMETAHEADER header ;
HDC hdc, hdcPrn ;
HENHMETAFILE hemfCopy ;
HMENU hMenu ;
int i, iLength, iEnable ;
PAINTSTRUCT ps ;
RECT rect ;
PTSTR pBuffer ;
switch (message)
{
case WM_CREATE:
// Initialize OPENFILENAME structure
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFileName ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = szTitleName ;
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT ("emf") ;
ofn.lCustData = 0 ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmapBack = CreateCompatibleBitmap(hdc,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
SelectObject(hdcMem,hBitmapBack);
ReleaseDC(hwnd,hdc);
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
return 0 ;
case WM_DISPLAYCHANGE:
DeleteDC(hdcMem);
DeleteObject(hBitmapBack);
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmapBack = CreateCompatibleBitmap(hdc,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
SelectObject(hdcMem,hBitmapBack);
ReleaseDC(hwnd,hdc);
if (hPalette)
{
hPaletteOld = SelectPalette (hdcMem, hPalette, FALSE) ;
RealizePalette (hdcMem) ;
}
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
PlayEnhMetaFile (hdcMem, hemf, &rect) ;
return 0;
case WM_SIZE:
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
if(hemf)
PlayEnhMetaFile (hdcMem, hemf, &rect) ;
return 0;
case WM_ERASEBKGND:
return 1;
case WM_INITMENUPOPUP:
hMenu = GetMenu (hwnd) ;
iEnable = hemf ? MF_ENABLED : MF_GRAYED ;
EnableMenuItem (hMenu, IDM_FILE_SAVE_AS, iEnable) ;
EnableMenuItem (hMenu, IDM_FILE_PRINT, iEnable) ;
EnableMenuItem (hMenu, IDM_FILE_PROPERTIES, iEnable) ;
EnableMenuItem (hMenu, IDM_EDIT_CUT, iEnable) ;
EnableMenuItem (hMenu, IDM_EDIT_COPY, iEnable) ;
EnableMenuItem (hMenu, IDM_EDIT_DELETE, iEnable) ;
EnableMenuItem (hMenu, IDM_EDIT_PASTE,
IsClipboardFormatAvailable (CF_ENHMETAFILE) ? MF_ENABLED : MF_GRAYED) ;
return 0 ;
case WM_DROPFILES:
DragQueryFile((HDROP)wParam,0,szFileName,MAX_PATH);
DragFinish((HDROP)wParam);
if (hemf)
{
DeleteEnhMetaFile (hemf) ;
hemf = NULL ;
if(hPalette)
{
SelectPalette (hdcMem, hPaletteOld, FALSE) ;
RealizePalette (hdcMem) ;
DeleteObject(hPalette);
hPalette = NULL;
}
}
SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
ShowCursor (TRUE) ;
hemf = GetEnhMetaFile (szFileName) ;
ShowCursor (FALSE) ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
// Invalidate the client area for later update
InvalidateRect (hwnd, NULL, TRUE) ;
if (hemf == NULL)
{
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
MessageBox (hwnd, TEXT ("Cannot load metafile"),
szAppName, MB_ICONEXCLAMATION | MB_OK) ;
}
else
{
if (hPalette = CreatePaletteFromMetaFile (hemf))
{
hPaletteOld = SelectPalette (hdcMem, hPalette, FALSE) ;
RealizePalette (hdcMem) ;
}
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
PlayEnhMetaFile (hdcMem, hemf, &rect) ;
}
return 0;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDM_FILE_OPEN:
// Show the File Open dialog box
ofn.Flags = 0 ;
if (!GetOpenFileName (&ofn))
return 0 ;
// If there's an existing EMF, get rid of it.
if (hemf)
{
DeleteEnhMetaFile (hemf) ;
hemf = NULL ;
if(hPalette)
{
SelectPalette (hdcMem, hPaletteOld, FALSE) ;
RealizePalette (hdcMem) ;
DeleteObject(hPalette);
hPalette = NULL;
}
}
// Load the EMF into memory
SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
ShowCursor (TRUE) ;
hemf = GetEnhMetaFile (szFileName) ;
ShowCursor (FALSE) ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
// Invalidate the client area for later update
InvalidateRect (hwnd, NULL, TRUE) ;
if (hemf == NULL)
{
GetClientRect (hwnd, &rect) ;
FillRect(hdcMem,&rect,hBrushBack);
MessageBox (hwnd, TEXT ("Cannot load metafile"),
szAppName, MB_ICONEXCLAMATION
- 1
- 2
- 3
前往页