Using Win32 functions in Visual FoxPro Image Gallery
Memory Management
..msdn
CopyMemory
FillMemory
GetProcessHeap
GetProcessHeaps
GlobalAlloc
GlobalFree
GlobalLock
GlobalMemoryStatus
GlobalReAlloc
GlobalSize
GlobalUnlock
HeapAlloc
HeapCompact
HeapFree
HeapLock
HeapReAlloc
HeapSize
HeapUnlock
HeapValidate
HeapWalk
LocalAlloc
LocalFree
LocalSize
VirtualAllocEx
VirtualFreeEx
ZeroMemory
Code examples:
Accessing LSA Policy object (Local Security Authority)
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Adding and deleting User Accounts
Adding printer to the list of supported printers for the specified server
Adding user-defined items to the Control Menu of VFP form (requires VFP9)
Attaching menu to a top-level form
Browsing Windows Known Folders (Special Folders)
Compressing and decompressing files with Windows API Runtime Library routines
Converting command-line string to a set of Unicode argument strings
Creating the Open dialog box to specify the drive, directory, and name of a file to open
Creating the Save dialog box to specify the drive, directory, and name of a file to save
Custom HttpRequest class (WinHTTP)
Deleting files into the Recycle Bin
Displaying dimmed window behind VFP top-level form
Displaying standard progress dialog box when copying files
Displaying system dialog that selects a folder
Dynamic strings implemented through VFP Custom class
Enumerating data formats currently available on the clipboard
Enumerating files opened on the network
Extensible Storage Engine class library
How to assemble an array of strings and pass it to external function
How to display a user-defined icon in the MessageBox dialog
How to display advanced Task Dialog (Vista)
How to display the Print property sheet
How to display the Properties dialog box for a file (ShellExecuteEx)
How to enumerate sessions and processes on a specified terminal server
How to enumerate terminal servers within the specified Windows domain
How to enumerate, add and delete shares on the local computer (WinNT/XP)
How to extract frames from AVI files
How to ping a remote site using ICMP API calls
How to prevent users from accessing the Windows Desktop and from switching to other applications
How to remove a directory that is not empty
How to write and read Window Properties for the specified window
Loading a string resource from an executable file
MapiSendMail class for Visual FoxPro application
Mapping and disconnecting network drives
Obtaining names of local and global groups for current user (WinNT/XP/2K)
Passing data records between VFP applications via the Clipboard
Printing Image File, programmatically set print page orientation to landscape
Reading and setting explicit Application User Model ID for the current process (Win7)
Reading entries from Event logs
Retrieving configuration information for the specified server (Win2000/XP)
Retrieving the command line for the VFP session
Sending email messages with Simple MAPI
Shortcut Menu Class
Starting a dialog box for connecting to network resources and passing input parameters
Storing the environment strings in cursor
URL: splitting into its component parts
Using Change Notification Objects to monitor changes to the printer or print server
Using WM_COPYDATA for interprocess communication (VFP9)
Windows Shell Icons displayed and exported to ICO files (Vista)
Winsock: connecting to a news server (NNTP, port 119)
Writing entries to custom Event Log
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: 101  
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-2014 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.1.68), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (50.17.79.35)
20 sec.Function: 'GetForegroundWindow'
Function group: 'Window'
Google
Advertise here!