[comp.windows.ms] copying metafile to clipboard

papa@pollux.usc.edu (Marco Papa) (01/08/90)

I've been trying to copying metafiles to "disk files" with no problem,
while I had no success with copying a metafile to the Clipboard.  The
clipboard recognizes that it received a Picture, but nothing is shown.
The following is the sample code I use, mainly taken verbatim from
Charles Petzold book "Programming Windows". When SaveClip is false,
the etafile is correctly saved on a disk file.  When SaveClip is
true an "empty" picture shows up in the clipboard. Any ideas?


void SetBarMetafile(HWND hWnd)
{
	HDC hMetaDC;
	HANDLE hMF;
    	HANDLE hDiskMF;
    	GLOBALHANDLE hGMem;
	LPMETAFILEPICT lpMFP;

	if (SaveClip) {
		hMetaDC = CreateMetaFile(NULL);
	} else {
		hMetaDC = CreateMetaFile(szMetaFileName);
	}
	SetWindowExt(hMetaDC, 1024, 780);

	<<< do the GDI graphics using hMetaDC >>>

    	hMF = CloseMetaFile(hMetaDC);
	/* copy memory metafile to disk metafile or Clipboard */
	if (hMF) {
		if (SaveClip) {
			hGMem = GlobalAlloc(GHND,(DWORD)sizeof(METAFILEPICT));
			lpMFP = (LPMETAFILEPICT) GlobalLock(hGMem);

			lpMFP->mm = MM_ANISOTROPIC;
			lpMFP->xExt = 0;
			lpMFP->yExt = 0;

			lpMFP->hMF = hMF;
			GlobalUnlock(hGMem);

			OpenClipboard(hWnd);
			EmptyClipboard();
			SetClipboardData(CF_METAFILEPICT, hGMem);
			CloseClipboard();
		} 
		DeleteMetaFile(hMF);
	} else
		MessageBox(hWnd, "Error creating Picture", szAppName,
			MB_OK | MB_ICONHAND);
    }
}

What am I doing wrong?

-- Marco
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Xerox sues somebody for copying?" -- David Letterman
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

papa@pollux.usc.edu (Marco Papa) (01/08/90)

In article <22139@usc.edu| papa@pollux.usc.edu (Marco Papa) writes:
|I've been trying to copying metafiles to "disk files" with no problem,
|while I had no success with copying a metafile to the Clipboard.  The
|clipboard recognizes that it received a Picture, but nothing is shown.
|The following is the sample code I use, mainly taken verbatim from
|Charles Petzold book "Programming Windows". When SaveClip is false,
|the etafile is correctly saved on a disk file.  When SaveClip is
|true an "empty" picture shows up in the clipboard. Any ideas?

Gee, I should have waited a couple of hours, since I just got it running.
The Clipboard seems to want a "copy" of the metafile, generated with
CopyMetafile(.., NULL).  The following is the edited code that works for 
the clipboard, too.

|void SetBarMetafile(HWND hWnd)
|{
|	HDC hMetaDC;
|	HANDLE hMF;
|    	HANDLE hDiskMF;
|    	GLOBALHANDLE hGMem;
|	LPMETAFILEPICT lpMFP;

***	HANDLE hClipMF;

|
|	if (SaveClip) {
|		hMetaDC = CreateMetaFile(NULL);
|	} else {
|		hMetaDC = CreateMetaFile(szMetaFileName);
|	}
|	SetWindowExt(hMetaDC, 1024, 780);
|
|	<<< do the GDI graphics using hMetaDC >>>
|
|    	hMF = CloseMetaFile(hMetaDC);
|	/* copy memory metafile to disk metafile or Clipboard */
|	if (hMF) {
|		if (SaveClip) {

***			hClipMF = CopyMetaFile(hMF, NULL);

|			hGMem = GlobalAlloc(GHND,(DWORD)sizeof(METAFILEPICT));
|			lpMFP = (LPMETAFILEPICT) GlobalLock(hGMem);
|
|			lpMFP->mm = MM_ANISOTROPIC;
|			lpMFP->xExt = 0;
|			lpMFP->yExt = 0;
|
|**			lpMFP->hMF = hMF;  (old)

***			lpMFP->hMF = hClipMF;  (new);

|			GlobalUnlock(hGMem);
|
|			OpenClipboard(hWnd);
|			EmptyClipboard();
|			SetClipboardData(CF_METAFILEPICT, hGMem);
|			CloseClipboard();
|		} 
|		DeleteMetaFile(hMF);
|	} else
|		MessageBox(hWnd, "Error creating Picture", szAppName,
|			MB_OK | MB_ICONHAND);
|    }
|}

Enjoy.

-- Marco Papa 'Doc'
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Xerox sues somebody for copying?" -- David Letterman
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

pearce@hpccc.HP.COM (Lori Pearce) (01/09/90)

Are you using MM_ISOTROPIC map mode.  If so, try MM_ANISOTROPIC and 
seee if it works.  I had a problem with ISOTROPIC mapping.

Lori Pearce
pearce@hpccc.hp.com

papa@pollux.usc.edu (Marco Papa) (01/09/90)

In article <5380002@hpccc.HP.COM> pearce@hpccc.HP.COM (Lori Pearce) writes:
>Are you using MM_ISOTROPIC map mode.  If so, try MM_ANISOTROPIC and 
>seee if it works.  I had a problem with ISOTROPIC mapping.

The code clearly used MM_ANISOTROPIC.  See my followup message on how I
got it to work.

-- Marco
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Xerox sues somebody for copying?" -- David Letterman
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

nhj@cs.bham.ac.uk (Nick Jurascheck <JurascheckNH>) (01/17/90)

In article <22139@usc.edu> you write:
> I've been trying to copying metafiles to "disk files" with no problem,
> while I had no success with copying a metafile to the Clipboard.  The
> clipboard recognizes that it received a Picture, but nothing is shown.
> The following is the sample code I use, mainly taken verbatim from
> Charles Petzold book "Programming Windows". When SaveClip is false,
> the etafile is correctly saved on a disk file.  When SaveClip is
> true an "empty" picture shows up in the clipboard. Any ideas?


  Yes. The code you gave is correct except that you deleted the metafile 
handle after copying the metafile to the clipboard.

  Remove this line and you should be OK:
> 		DeleteMetaFile(hMF);

  Like the global memory handle you pass to the clipboard, the handle
to the metafile (hMF) is no longer valid in your program and will be 
freed by the clipboard (or to be more exact the Windows kernel, since 
the clipboard is just a global memory area).

Nick			
JANET: JurascheckNH@uk.ac.bham.cs  |  UUCP: ...!mcvax!bhamcs!JurascheckNH
BITNET: JurascheckNH%cs.bham.ac.uk@ukacrl.bitnet
INTERNET: JurascheckNH%cs.bham.ac.uk@cunyvm.cuny.edu