質問

I'm trying to create an enhanced metafile like this:

// Obtain a handle to a reference device context.  

HDC hdcRef = GetDC(hwnd); 

// Determine the picture frame dimensions.  

int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE); 
int iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE); 
int iWidthPels = GetDeviceCaps(hdcRef, HORZRES); 
int iHeightPels = GetDeviceCaps(hdcRef, VERTRES); 

// Retrieve the coordinates of the client  
// rectangle, in pixels.  
RECT rect;
GetClientRect(hwnd, &rect); 

// Convert client coordinates to .01-mm units.  
// Use iWidthMM, iWidthPels, iHeightMM, and  
// iHeightPels to determine the number of  
// .01-millimeter units per pixel in the x-  
//  and y-directions.  

rect.left = (rect.left * iWidthMM * 100)/iWidthPels; 
rect.top = (rect.top * iHeightMM * 100)/iHeightPels; 
rect.right = (rect.right * iWidthMM * 100)/iWidthPels; 
rect.bottom = (rect.bottom * iHeightMM * 100)/iHeightPels; 


// Create the metafile device context.  

CreateEnhMetaFile(hdcRef, (LPTSTR)"temp.emf", &rect, NULL); 

// Release the reference device context.  

ReleaseDC(hwnd, hdcRef); 

Took the code here

All I got in the end is some 0 bytes non-extension file with strangely encoded name, smth like 整灭攮晭.

What can be the problem?

P.S. Also, I'm calling it in a mixed-mode app, from c# through a c++/cli object.

EDIT The problem with the strange encoding is solved, but the file created is still 0 bytes length. How can it be solved?

役に立ちましたか?

解決

The cast is the problem, don't use casts unless you really have to.

Amazing that you got that cast from a Microsoft web site! You have to wonder about the quality of people MS hire. But in their code it's not wrong just superfluous. When you translated it to your code it is wrong.

CreateEnhMetaFile(hdcRef, _T("temp.emf"), &rect, NULL);

The _T macro is the official way to write a string literal that will be interpreted as either a Unicode string or a ANSI string depending on your compiler settings.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top