home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 4
/
BUGCD1997_05.BIN
/
aplic
/
clip4win
/
clip4win.exe
/
C4W30E.HUF
/
SOURCE
/
METAFD.PRG
< prev
next >
Wrap
Text File
|
1994-05-27
|
11KB
|
396 lines
////////////////////////////
//
// Clip-4-Win metafile demo
//
// Copyright (C) 1992 Skelton Software, Kendal Cottage, Hillam, Leeds, UK.
// All Rights Reserved.
//
//
// Compile: rmake metafd
//
////////////////////////////
#include "windows.ch"
// handle Clipper 10-char limit on external names
#xtranslate GetMetaFileBits => GetMFBits
#xtranslate SetMetaFileBitsBetter => SetMFBBett
static cAppName := "Clip-4-Win"
static hWnd, hInst, hPrevInst, nCmdShow
static cText := ""
static aWnd := {}, aAction := {} // for event handlers
static hMetaFile := 0 //handle of metafile
static cMetaFile := "UNTITLED"
static nOriginX :=0, nOriginY:=0 //for DoPlay()
function main()
local hMenu, nEvent
hWnd = WinSetup(cAppName, "Clip-4-Win metafile demo")
hMenu = MenuSetup()
HideCaret(hWnd)
AddHandler(hWnd, {|nEvent| MainEvent(nEvent)})
do while .t.
do while (nEvent := ChkEvent()) == EVENT_NONE
// some "background" processing could go here
enddo
HandleEvent(nEvent)
do case
case nEvent == EVENT_QUIT
DoExit()
endcase
enddo
return 0
procedure MainEvent(nEvent)
local hOldWnd ,hDC
do case
case nEvent == EVENT_REDRAW
hOldWnd = SelectWindow(hWnd)
@ 0,0 clear to maxrow(),maxcol()
SelectWindow(hOldWnd)
if hMetaFile != 0
hDC = GetDC(hWnd)
PlayMetaFile(hDC, hMetaFile)
ReleaseDC(hWnd, hDC)
endif
nOriginX = 0 //initialize variables for DoPlay()
nOriginY = 0
endcase
return
procedure DoAbout()
MessageBox( , "Clip-4-Win Metafile Demo", "Info", MB_OK)
return
procedure DoExit()
MessageBox(0, "Thanks for running this Clip-4-Win demo", "Clip-4-Win Demo Exiting", MB_OK)
quit
return
/*
Function: DoNew()
*/
static function DoNew()
local hMenu, hOldWnd
hOldWnd = SelectWindow(hWnd)
@ 0,0 clear to maxrow(),maxcol()
SelectWindow(hOldWnd)
//───── disable the "play", "save" and "saveas" menu items
hMenu := GetMenu(hWnd) // retrieve reference to main menu
EnableMenuItem(hMenu, "play", MF_DISABLED+MF_GRAYED)
EnableMenuItem(hMenu, "save", MF_DISABLED+MF_GRAYED)
EnableMenuItem(hMenu, "saveas", MF_DISABLED+MF_GRAYED)
DrawMenuBar(hWnd)
if hMetaFile != 0
DeleteMetaFile(hMetaFile)
hMetaFile = 0
cMetaFile = "UNTITLED"
endif
return nil
/*
Function: DoOpen()
*/
static function DoOpen()
local cFile := GetOpenFileName(, "*.wmf", "Select a metafile")
local hMenu
local hTempMetaFile
if cFile <> NIL
if (hTempMetaFile := GetMetaFile(cFile)) != 0
//───── enable the "play", "save" and "saveas" menu items
hMenu := GetMenu(hWnd) // retrieve reference to main menu
EnableMenuItem(hMenu, "play", MF_ENABLED)
EnableMenuItem(hMenu, "save", MF_ENABLED)
EnableMenuItem(hMenu, "saveas", MF_ENABLED)
DrawMenuBar(hWnd)
if hMetaFile != 0
DeleteMetaFile(hMetaFile)
endif
hMetaFile = hTempMetaFile
cMetaFile = cFile
else
MessageBox(hWnd, cFile + " is not a valid metafile", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
endif
return nil
/*
Function: DoSave()
*/
static function DoSave()
local hTempMetaFile
local cFile := IIF(cMetaFile == "UNTITLED";
,GetSaveFileName(, "*.wmf", "Enter a metafile name");
,cMetaFile)
if cFile <> NIL
if (hTempMetaFile := CopyMetaFile(hMetaFile, cFile)) != 0
DeleteMetaFile(hMetaFile) // free memory
hMetaFile = hTempMetaFile //use the handle of new metafile
else
MessageBox(hWnd, cFile + " is not a valid filename", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
endif
return nil
/*
Function: DoSaveAs()
*/
static function DoSaveAs()
local cFile := GetSaveFileName(, "*.wmf", "Select a metafile")
local hMenu, hTempMetaFile
if cFile <> NIL
if (hTempMetaFile := CopyMetaFile(hMetaFile, cFile)) != 0
DeleteMetaFile(hMetaFile) // free memory
hMetaFile = hTempMetaFile //use the handle of new metafile
cMetaFile = cFile
else
MessageBox(hWnd, cFile + " is not a valid filename", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
endif
return nil
/*
Function: DoCreate()
*/
function DoCreate()
local hDCMeta, hDC
local nX1:=40, nY1:=150, nX2:=100, nY2:=40, i
local hMenu
hDCMeta = CreateMetaFile() //creates a device context to record
// a metafile
for i= 1 to 12
MoveTo(hDCMeta, nX1 := nX1 + i*5, nY1 := nY1 + i*3)
LineTo(hDCMeta, nX2 := nX2 - i*8, nY2 := nY2 + i*2)
next
if hMetaFile != 0
DeleteMetaFile(hMetaFile)
endif
hMetaFile = CloseMetaFile( hDCMeta )
hDC = GetDC(hWnd)
PlayMetaFile(hDC, hMetaFile)
ReleaseDC(hWnd, hDC)
//───── enable the "play", "save" and "saveas" menu items
hMenu := GetMenu(hWnd) // retrieve reference to main menu
EnableMenuItem(hMenu, "play", MF_ENABLED)
EnableMenuItem(hMenu, "save", MF_ENABLED)
EnableMenuItem(hMenu, "saveas", MF_ENABLED)
DrawMenuBar(hWnd)
return nil
/*
Function: DoPlay()
*/
static function DoPlay()
local hDC
if ((hDC := GetDC(hWnd)) > 0)
SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)
PlayMetaFile(hDC,hMetaFile)
ReleaseDC(hWnd, hDC)
else
MessageBox(hWnd, " Error while creating device context", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
return nil
/*
Function: DoGetSet()
*/
static function DoGetSet()
local hDC
local hGlobal
if ((hDC := GetDC(hWnd)) > 0)
SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)
hGlobal = GetMetaFileBits(hMetaFile)
PlayMetaFile(hDC,hGlobal) // play global memory object
// that is containing the metafile
ReleaseDC(hWnd, hDC)
hMetaFile = SetMetaFileBits( hGlobal) //return to usual metafile handle
else
MessageBox(hWnd, " Error while creating device context", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
return nil
/*
Function: DoGetSetBetter()
*/
static function DoGetSetBetter()
local hDC
local hGlobal
if ((hDC := GetDC(hWnd)) > 0)
SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)
hGlobal = GetMetaFileBits(hMetaFile)
PlayMetaFile(hDC,hGlobal) // play global memory object
// that is containing the metafile
ReleaseDC(hWnd, hDC)
hMetaFile = SetMetaFileBitsBetter( hGlobal) // get metafile handle
//that will be owned by GDI so it
//could be used by multiple applications
//return to usual the metafile handle
hGlobal = GetMetaFileBits(hMetaFile)
hMetaFile = SetMetaFileBits( hGlobal) //return to usual metafile handle
else
MessageBox(hWnd, " Error while creating device context", "Error", ;
MB_ICONEXCLAMATION + MB_OK)
endif
return nil
function MenuSetup()
local hWnd := SelectWindow(), hMenu, hPopupMenu
if (hMenu := GetMenu(hWnd)) != nil
DestroyMenu(hMenu)
endif
// do new one (forget old value)
hMenu = CreateMenu()
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "file", MF_ENABLED + MF_POPUP, "&File", hPopupMenu)
AppendMenu(hPopupMenu, "new", MF_ENABLED + MF_STRING, "&New", {|| DoNew()})
AppendMenu(hPopupMenu, "open", MF_ENABLED + MF_STRING, "&Open", {|| DoOpen()})
AppendMenu(hPopupMenu, "save", MF_GRAYED + MF_STRING, "&Save", {|| DoSave()})
AppendMenu(hPopupMenu, "saveas", MF_GRAYED + MF_STRING, "Save &As", {|| DoSaveAs()})
AppendMenu(hPopupMenu, "", MF_SEPARATOR)
AppendMenu(hPopupMenu, "exit", MF_ENABLED + MF_STRING, "E&xit", {|| DoExit()})
AppendMenu(hMenu, "play", MF_GRAYED + MF_STRING, "&Play", {|| DoPlay()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "create", MF_ENABLED + MF_POPUP, "&Create", hPopupMenu)
AppendMenu(hPopupMenu, "createmetafile \t ALT+C", MF_ENABLED + MF_STRING, "&Create metafile", {|| DoCreate()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "test", MF_ENABLED + MF_POPUP, "&Test", hPopupMenu)
AppendMenu(hPopupMenu, "get/set", MF_ENABLED + MF_STRING, "Get/set metafile bits", {|| DoGetSet()})
AppendMenu(hPopupMenu, "get/setbetter", MF_ENABLED + MF_STRING, "Get/set(better) metafile bits", {|| DoGetSetBetter()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "help", MF_ENABLED + MF_POPUP, "&Help", hPopupMenu)
AppendMenu(hPopupMenu, "about", MF_ENABLED + MF_STRING, "&About", {|| DoAbout()})
SetMenu(hWnd, hMenu)
return hMenu
function AddHandler(hWnd, bAction) // --> nId (for use with DelHandler)
aadd(aWnd, hWnd)
aadd(aAction, bAction)
return len(aWnd)
procedure DelHandler(nId)
adel(aWnd, nId)
asize(aWnd, len(aWnd) - 1)
adel(aAction, nId)
asize(aAction, len(aAction) - 1)
return
procedure HandleEvent(nEvent)
local hWnd := _LasthWnd(), i := 0
do while (i := ascan(aWnd, hWnd, ++i)) != 0
eval(aAction[i], nEvent)
enddo
if nEvent == EVENT_DESTROY
// clean up, so the event handler needn't bother
do while (i := ascan(aWnd, hWnd)) != 0
DelHandler(i)
enddo
endif
return
function WinNew(cAppName, cTitle, nX, nY, nWidth, nHeight)
local hWin, hInst, nCmdShow
hInst = _GetInstance()
nCmdShow = _GetnCmdShow()
hWin = CreateWindow(cAppName, ; // window class
cTitle, ; // caption for title bar
WS_OVERLAPPEDWINDOW,; // window style
nX, ; // x co-ordinate
nY, ; // y co-ordinate
nWidth, ; // width
nHeight, ; // height
hWnd, ; // hWnd of parent
0, ; // hMenu of menu (none yet)
hInst) // our own app instance
if hWin == 0
// probably out of resources
MessageBox( , "Can't create window", "Error", MB_OK)
return nil
endif
HideCaret(hWin)
// make sure it's displayed ...
ShowWindow(hWin, nCmdShow)
// ... and up to date
UpdateWindow(hWin)
return hWin