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

📄 wmfview.cpp

📁 大量windows shell编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL,
                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   if(hFile == INVALID_HANDLE_VALUE)
      return NULL;

   // Read the file to a buffer
   dwSize = GetFileSize(hFile, NULL);
   pb = new BYTE[dwSize];
   ReadFile(hFile, pb, dwSize, &dwSize, NULL);
   CloseHandle(hFile);

   // Check to see if it is a placeable metafile
   if((reinterpret_cast<LPAPMHEADER>(pb))->dwKey != 0x9ac6cdd7l)
   {
      // Don't know how to handle this...
      delete [] pb;
      return NULL;
   }

   // Create an enhanced metafile from the bits
   hEMF = SetWinMetaFileBits(dwSize, &(pb[sizeof(APMHEADER)]), NULL, NULL);

   delete [] pb;
   return hEMF;
}


void PrintMetaFile(LPTSTR szFile)
{
   // Get an EMF handle
   HENHMETAFILE hEMF = GetMetaFileHandle(szFile);
   if(hEMF == NULL)
      return;

   // Get a printer DC
   PRINTDLG pdlg;
   ZeroMemory(&pdlg, sizeof(PRINTDLG));
   pdlg.lStructSize = sizeof(PRINTDLG);
   pdlg.Flags = PD_RETURNDC;

   HDC hDC = NULL;
   if(PrintDlg(&pdlg))
      hDC = pdlg.hDC;
   else
      return;

   // Prepare document printing
   DOCINFO di;
   ZeroMemory(&di, sizeof(DOCINFO));
   di.cbSize = sizeof(DOCINFO);
   di.lpszDocName = "Printing EMF";

   // Start printing
   StartDoc(hDC, &di);
   StartPage(hDC);

   // Scale to fit the entire printed page
   RECT rc;
   SetRect(&rc, 0, 0, GetDeviceCaps(hDC, HORZRES), GetDeviceCaps(hDC, VERTRES));
   PlayEnhMetaFile(hDC, hEMF, &rc);

   // Clean up
   EndPage(hDC);
   EndDoc(hDC);
   DeleteDC(hDC);
   DeleteEnhMetaFile(hEMF);
}


void SaveMetaFile(LPTSTR szFile)
{
   TCHAR szOutputFile[MAX_PATH] = {0};
   HENHMETAFILE hEMF = GetMetaFileHandle(szFile);
   if(hEMF == NULL)
      return;

   // Determine the output format
   lstrcpy(szOutputFile, szFile);
   strlwr(szFile);
   if(strstr(szFile, ".emf"))
   {
      PathRenameExtension(szOutputFile, ".wmf");
      SaveToWMF(hEMF, szOutputFile);
   }
   else if(strstr(szFile, ".wmf"))
   {
      PathRenameExtension(szOutputFile, ".emf");
      SaveToEMF(hEMF, szOutputFile);
   }

   DeleteEnhMetaFile(hEMF);
}


void SaveToEMF(HENHMETAFILE hEMF, LPTSTR szFile)
{
   // Get memory to store the EMF bits
   DWORD dwSize = GetEnhMetaFileBits(hEMF, 0, NULL);
   LPBYTE pb = new BYTE[dwSize];

   // Get the EMF bits
   GetEnhMetaFileBits(hEMF, dwSize, pb);

   // Save to file
   HANDLE hFile = CreateFile(szFile, GENERIC_WRITE,
                         0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
   if(hFile == INVALID_HANDLE_VALUE)
   {
      UINT rc = MessageBox(GetFocus(), "File exists. Overwrite?",
                                       szFile, MB_ICONQUESTION | MB_YESNO);
      if(rc == IDYES)
         hFile = CreateFile(szFile, GENERIC_WRITE,
                      0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
      else
      {
         delete [] pb;
         return;
      }
   }

   DWORD dwBytes;
   WriteFile(hFile, pb, dwSize, &dwBytes, NULL);
   CloseHandle(hFile);
   delete [] pb;
}


void SaveToWMF(HENHMETAFILE hEMF, LPTSTR szFile)
{
   // Get memory to store the WMF bits
   HDC hDC = GetDC(NULL);
   DWORD dwSize = GetWinMetaFileBits(hEMF, 0, NULL, MM_ANISOTROPIC, hDC);
   LPBYTE pb = new BYTE[dwSize];

   // Get the WMF bits from the EMF handle
   GetWinMetaFileBits(hEMF, dwSize, pb, MM_ANISOTROPIC, hDC);
   ReleaseDC(NULL, hDC);

   // Save to file
   HANDLE hFile = CreateFile(szFile, GENERIC_WRITE,
                         0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
   if(hFile == INVALID_HANDLE_VALUE)
   {
      UINT rc = MessageBox(GetFocus(), "File exists. Overwrite?",
                                         szFile, MB_ICONQUESTION|MB_YESNO);
      if(rc == IDYES)
         hFile = CreateFile(szFile, GENERIC_WRITE,
                      0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
      else
      {
         delete [] pb;
         return;
      }
   }

   DWORD dwBytes;
   WriteFile(hFile, pb, dwSize, &dwBytes, NULL);
   CloseHandle(hFile);
   delete [] pb;
}


void OnOpen(HWND hDlg)
{
	TCHAR szFile[MAX_PATH] = {0};

	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.lpstrFilter = "Metafiles\0*.?mf\0WMF\0*.wmf\0Enhanced\0*.emf\0All Files\0*.*\0";
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFile = szFile;
	if(!GetOpenFileName(&ofn))
		return;
	else
	{
		HWND hwndMeta = GetDlgItem(hDlg, IDC_METAFILE);
		DisplayMetaFile(hwndMeta, ofn.lpstrFile);
		RefreshUI(hDlg, ofn.lpstrFile);
	}
}


void OnPrint(HWND hDlg)
{
	if(lstrlen(g_szCurFile))
		PrintMetaFile(g_szCurFile);
	else
		Msg("There's no metafile currently opened.");
}


void OnSave(HWND hDlg)
{
	TCHAR s[1024] = {0};
	TCHAR szOutputFile[MAX_PATH] = {0};
	
	if(!lstrlen(g_szCurFile))
	{
		Msg("There's no metafile currently opened.");
		return;
	}

	// Ask for user's confirmation
	lstrcpy(szOutputFile, g_szCurFile);
	if(strstr(g_szCurFile, ".emf"))
		PathRenameExtension(szOutputFile, ".wmf");
	else if(strstr(g_szCurFile, ".wmf"))
		PathRenameExtension(szOutputFile, ".emf");

	wsprintf(s, "You're about to convert %s to %s.\nAre you really sure?",
		g_szCurFile, szOutputFile);
	UINT rc = MessageBox(hDlg, s, APPTITLE, MB_ICONQUESTION | MB_YESNO);

	// Proceed...
	if(rc == IDYES)
		SaveMetaFile(g_szCurFile);
}


void RefreshUI(HWND hWnd, LPTSTR szFile)
{
   TCHAR szCaption[MAX_PATH] = {0};

   // Refresh the caption bar
   wsprintf(szCaption, "%s - %s", APPTITLE, szFile);
   SetWindowText(hWnd, szCaption);
}


void ParseCommandLine(HWND hwnd, LPTSTR pszCmdLine)
{
   if(!lstrlen(pszCmdLine))
      return;

   // Get the first 2 (+ 1) chars from the command line (it's the switch)
   TCHAR pszSwitch[2] = {0};
   lstrcpyn(pszSwitch, pszCmdLine, 3);
   LPTSTR psz = pszCmdLine + lstrlen(pszSwitch) + 1;

   // Resolve any case by sending a custom message
   if(!lstrcmpi(pszSwitch, "/p"))
      SendMessage(hwnd, WM_EX_PRINTMETA, 0, reinterpret_cast<LPARAM>(psz));
   else if(!lstrcmpi(pszSwitch, "/s"))
      SendMessage(hwnd, WM_EX_SAVEMETA, 0, reinterpret_cast<LPARAM>(psz));
   else
      SendMessage(hwnd, WM_EX_DISPLAYMETA, 0, reinterpret_cast<LPARAM>(pszCmdLine));
}


HWND AnotherInstanceRunning()
{
   HWND hwndFound = NULL;
   EnumWindows(CheckRunningApps, reinterpret_cast<LPARAM>(&hwndFound));

   // hwndFound will get the handle of the matching window, if any
   return hwndFound;
}


BOOL CALLBACK CheckRunningApps(HWND hwnd, LPARAM lParam)
{
   TCHAR szClass[MAX_PATH] = {0};
   GetClassName(hwnd, szClass, MAX_PATH);
   if(!lstrcmpi(szClass, "#32770"))
   {
      TCHAR s[MAX_PATH] = {0};
      TCHAR szTitle[MAX_PATH] = {0};
      GetWindowText(hwnd, szTitle, MAX_PATH);

      lstrcpyn(s, szTitle, 1 + lstrlen(APPTITLE));
      if(!lstrcmpi(s, APPTITLE))
      {
         // Uses the buffer pointed by lParam to return the HWND
         HWND* lphwnd = reinterpret_cast<HWND*>(lParam);
         *lphwnd = hwnd;
         return FALSE;
      }
   }

   return TRUE;
}


void HandleFileDrop(HWND hDlg, HDROP hDrop)
{
   TCHAR szFileName[MAX_PATH] = {0};

   // Since we are an SDI app, it doesn't make sense to receive
   //  more than one file, so extract only the first
   DragQueryFile(hDrop, 0, szFileName, MAX_PATH);
   SendMessage(hDlg, WM_EX_DISPLAYMETA, 0, reinterpret_cast<LPARAM>(szFileName));
   DragFinish(hDrop);
}


/*  End of file: WMFView.cpp  */

⌨️ 快捷键说明

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