Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Custom GDI+ class
Splash Screen for the VFP application
Vertical Label control
Displaying dimmed window behind VFP top-level form
Subclassing CommandButton control to create BackColor property
Storing content of the Clipboard to a bitmap file
How to put a vertical text scrolling on the form (a movie cast)
How to make a VFP form fading out when released (GDI version)
How to put a horizontal text scrolling on the form (a news line, marquee)
Printing text with the Escape function
Bitmap Class for Visual FoxPro application
Custom FTP Class for Visual FoxPro application
Storing screen shot of a form to bitmap file
Playing WAV sounds simultaneously
How to play AVI file on the _screen
How to view icons stored in executable files (Icon Viewer) - II
Pocket PC: custom RAPI class for operating with files and folders on mobile device
CryptoAPI: Collection of Providers class
How to print FoxPro form -- II
Obtaining names and positions for shortcuts located on the Windows Desktop
Enumerating Processes -- WinNT
Copying picture of the active form to the Clipboard using Enhanced Metafile API functions
Passing data records between VFP applications via the Clipboard
Enumerating devices installed on the local machine
Printing Image File, programmatically set print page orientation to landscape

User rating: 7/10 (1 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
The images in this code sample are handled with the help of GDI+ objects instantiated from VFP library of GDI+ classes. Download the library module first and save it in gdiplus.prg file.

See also:

Download ScreenCapture Library

#DEFINE DM_OUT_BUFFER 2
#DEFINE DM_IN_BUFFER  8

#DEFINE DM_ORIENTATION  1
#DEFINE DM_ORIENT_PORTRAIT  1
#DEFINE DM_ORIENT_LANDSCAPE 2
#DEFINE DM_OFFS_DMFIELDS 41  && DEVMODE dmFields offset
#DEFINE DM_OFFS_ORIENTATION 45  && DEVMODE dmOrientation offset

#DEFINE DOCINFO_SIZE 20

SET PROCEDURE TO gdiplus ADDITIVE
DO declare

LOCAL hPrinterDC

* create device context for the default printer
* setting specified printing page orientation
hPrinterDC = GetPrinterDC(DM_ORIENT_LANDSCAPE)

IF hPrinterDC <> 0
    * pass the printer device context
    * and the name of a valid image file
    DO PrintImageFile WITH hPrinterDC, "C:\...\sample.jpg"

    = DeleteDC(hPrinterDC)
ENDIF
* end of main

PROCEDURE PrintImageFile(hPrinterDC As Number, cImgFile As String)

    * an instance of gdiplusinit should be created before
    * instantiating gdi+ object
    PRIVATE gdiplus
    gdiplus = CREATEOBJECT("gdiplusinit")

    LOCAL oImage As gdiimage, oPrnGraphics As graphics

    * create GDIIMAGE object from the image file
    oImage = CREATEOBJECT("gdiimage", cImgFile)
    IF oImage.imgwidth=0
        = MESSAGEBOX("Invalid image file selected!")
        RETURN .F.
    ENDIF

    * create GRAPHICS object from 
    * the printer device context
    oPrnGraphics = CREATEOBJECT("graphics", hPrinterDC)
    IF oPrnGraphics.graphics = 0
        = MESSAGEBOX("Failed to create the GRAPHICS object " +;
            "for the printer device context!")
        RETURN .F.
    ENDIF

    * set the unit of measure for the printing page
    * to the UnitDocument, which is 1/300 inch
    oPrnGraphics.SetPageUnit(5)

    * set the scaling factor for the printing page
    * to print the image at its original size
    oPrnGraphics.SetPageScale(300/oImage.imghorres)

    LOCAL cDocInfo As String, oDocName as PChar
    oDocName = CREATEOBJECT("PChar", "Printing " + JUSTFNAME(m.cImgFile))

    * populate DOCINFO buffer
    cDocInfo = PADR(num2dword(DOCINFO_SIZE) +;
        num2dword(oDocName.GetAddr()), DOCINFO_SIZE, CHR(0))

    = StartDoc(m.hPrinterDC, m.cDocInfo)
    = StartPage(m.hPrinterDC)

    * draw the image on the printer Graphics canvas
    oPrnGraphics.DrawImage(oImage, 10, 10)

    = EndPage(m.hPrinterDC)
    = EndDoc(m.hPrinterDC)

FUNCTION GetPrinterDC(nOrientation As Number) As Number
* returns device context for the default printer

    LOCAL cPrinterName, hPrinter, nErrorCode,;
        oDevmode As PChar, nDevmodeSize, hPrinterDC

    * obtain the default printer name from API call;
    * or use VFP native GETPRINTER() or APRINTERS() functions
    cPrinterName=GetDefaultPrinterName()

    hPrinter=0
    = OpenPrinter(cPrinterName, @hPrinter, 0)
    IF hPrinter = 0
        nErrorCode=GetLastError()
        = MESSAGEBOX("OpenPrinter() failed: " +;
            TRANSFORM(nErrorCode), 48, "Error")
        RETURN 0
    ENDIF

    * first DocumentProperties call is configured 
    * to return the required size for DEVMODE buffer
    nDevmodeSize = DocumentProperties(_screen.HWnd, hPrinter,;
        cPrinterName, 0, 0, 0)

    oDevmode = CREATEOBJECT("PChar",;
        REPLICATE(CHR(0),nDevmodeSize))

    * second DocumentProperties call populates
    * the DEVMODE buffer
    = DocumentProperties(_screen.HWnd, hPrinter,;
        cPrinterName, oDevmode.GetAddr(), 0, DM_OUT_BUFFER)

    * the printer handle is not required anymore
    * and can be closed
    = ClosePrinter(hPrinter)

    * switching to the specified page orientation 
    * by adjusting the DEVMODE structure
    DO SetPageOrientation WITH oDevMode, m.nOrientation

    * the device context for the default printer
    * is created based on the DEVMODE configured 
    * to the sepcified page orientation
    hPrinterDC = CreateDC("winspool", cPrinterName,;
        0, oDevmode.GetAddr())

    IF hPrinterDC = 0
        nErrorCode=GetLastError()
        = MESSAGEBOX("CreateDC() failed: " +;
            TRANSFORM(nErrorCode), 48, "Error")
    ENDIF

RETURN m.hPrinterDC

PROCEDURE SetPageOrientation
PARAMETERS oDevmode As PChar, nOrientation As Number

    LOCAL cDevmode, dmFlags

    * copy DEVMODE bytes from memory block to string buffer
    cDevmode = oDevmode.GetValue()

    * setting DEVMODE members flag and page orientation value
    dmFlags = buf2word(SUBSTR(cDevmode, DM_OFFS_DMFIELDS, 4))
    dmFlags = BITOR(dmFlags, DM_ORIENTATION)

    cDevmode = STUFF(cDevmode, DM_OFFS_DMFIELDS,;
        4, num2dword(dmFlags))

    cDevmode = STUFF(cDevmode, DM_OFFS_ORIENTATION,;
        2, num2word(nOrientation))

    * copy DEVMODE bytes from string buffer back to memory block
    oDevmode.SetValue(m.cDevmode)

FUNCTION GetDefaultPrinterName() As String
    LOCAL cPrinter, nBufsize
    nBufsize=250
    cPrinter=REPLICATE(CHR(0), nBufsize)
RETURN IIF(GetDefaultPrinter(@cPrinter, @nBufsize)=0, "",;
    SUBSTR(cPrinter, 1, AT(CHR(0),cPrinter)-1))

PROCEDURE declare
    DECLARE INTEGER ClosePrinter IN winspool.drv INTEGER hPrinter 
    DECLARE INTEGER GetLastError IN kernel32
    DECLARE INTEGER DeleteDC IN gdi32 INTEGER hdc
    DECLARE INTEGER StartPage IN gdi32 INTEGER hdc
    DECLARE INTEGER EndPage IN gdi32 INTEGER hdc
    DECLARE INTEGER EndDoc IN gdi32 INTEGER hdc
    DECLARE INTEGER StartDoc IN gdi32 INTEGER hdc, STRING lpdi

    DECLARE INTEGER GetDefaultPrinter IN winspool.drv;
        STRING @pszBuffer, INTEGER @pcchBuffer

    DECLARE INTEGER OpenPrinter IN winspool.drv;
        STRING pPrinterName, INTEGER @phPrinter, INTEGER pDefault

    DECLARE INTEGER DocumentProperties IN winspool.drv;
        INTEGER hWnd, INTEGER hPrinter, STRING pDeviceName,;
        INTEGER pDevModeOutput, INTEGER pDevModeInput, INTEGER fMode

    DECLARE INTEGER CreateDC IN gdi32;
        STRING lpszDriver, STRING lpszDevice,;
        INTEGER lpszOutput, INTEGER lpInitData

DEFINE CLASS PChar As Session
PROTECTED hMem

PROCEDURE Init(lcString)
    THIS.hMem = 0
    THIS.setValue(lcString)

PROCEDURE Destroy
    THIS.ReleaseString

FUNCTION GetAddr
RETURN THIS.hMem

FUNCTION GetValue
    LOCAL lnSize, lcBuffer
    lnSize = THIS.getAllocSize()
    lcBuffer = SPACE(lnSize)

    IF THIS.hMem <> 0
        DECLARE RtlMoveMemory IN kernel32 As MemToStr;
            STRING @, INTEGER, INTEGER
        = MemToStr(@lcBuffer, THIS.hMem, lnSize)
    ENDIF
RETURN lcBuffer

FUNCTION GetAllocSize
    DECLARE INTEGER GlobalSize IN kernel32 INTEGER hMem
RETURN Iif(THIS.hMem=0, 0, GlobalSize(THIS.hMem))

PROCEDURE SetValue(lcString)
#DEFINE GMEM_FIXED 0
#DEFINE GMEM_MOVEABLE 2
#DEFINE GMEM_ZEROINIT 0x0040

    THIS.ReleaseString

    DECLARE INTEGER GlobalAlloc IN kernel32 INTEGER, INTEGER
    DECLARE RtlMoveMemory IN kernel32 As StrToMem;
        INTEGER, STRING @, INTEGER

    LOCAL lnSize
    lcString = lcString + Chr(0)
    lnSize = Len(lcString)
    THIS.hMem = GlobalAlloc(GMEM_ZEROINIT, lnSize)
    IF THIS.hMem <> 0
        = StrToMem(THIS.hMem, @lcString, lnSize)
    ENDIF

PROCEDURE ReleaseString
    IF THIS.hMem <> 0
        DECLARE INTEGER GlobalFree IN kernel32 INTEGER
        = GlobalFree (THIS.hMem)
        THIS.hMem = 0
    ENDIF
ENDDEFINE

User rating: 7/10 (1 votes)
Rate this code sample:
  • ~
6783 bytes  
Created: 2009-09-21 16:29:42  
Modified: 2011-03-28 09:41:07  
Visits in 7 days: 208  
Listed functions:
ClosePrinter
CreateDC
DeleteDC
DocumentProperties
EndDoc
EndPage
GetDefaultPrinter
GetLastError
GlobalAlloc
GlobalFree
GlobalSize
OpenPrinter
StartDoc
StartPage
Printer friendly API declarations
My comment:
DEVMODE data for a printer can be obtained by calling either the DocumentProperties or the PrintDlg API function. If required, the latter can display the Print dialog box allowing to adjust the printer job settings manually.

When adjusted accordingly, such DEVMODE structure is used to create a device context (DC) for the printer.
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright 2001-2017 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.6.38), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (54.147.40.153)
17 sec.Example: 'Displaying dimmed window behind VFP top-level form'
38 sec.Function: 'CeCreateFile'
Function group: 'Remote Application Programming (RAPI)'
57 sec.Function: 'NetFileEnum'
Function group: 'Network Management'
1.28 min.Function: 'GetComputerObjectName'
Function group: 'System Information'
1.58 min.Function: 'CeDeleteRecord'
Function group: 'Remote Application Programming (RAPI)'
1.92 min.Function: 'waveOutReset'
Function group: 'Windows Multimedia'
2.25 min.Function: 'CeFindFirstFile'
Function group: 'Remote Application Programming (RAPI)'
2.58 min.Links
2.97 min.Function: 'CeCloseHandle'
Function group: 'Remote Application Programming (RAPI)'
3.32 min.Function: 'SQLGetEnvAttr'
Function group: 'ODBC API'
Google
Advertise here!