Using Win32 functions in Visual FoxPro Image Gallery
Error Handling
..msdn
Beep
FatalAppExit
FlashWindow
FlashWindowEx
FormatMessage
GetErrorMode
GetLastError
MessageBeep
SetErrorMode
SetLastError
Code examples:
A class that encrypts and decrypts files using Cryptography API Functions
Adding printer to the list of supported printers for the specified server
Basic Volume information
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Closing Windows
Compressing and decompressing files with Windows API Runtime Library routines
Converting command-line string to a set of Unicode argument strings (WinNT only)
Converting long file names to the short format and vice versa
Converting Unicode data from the Clipboard to a character string using a given code page
Copying files as a transacted operation (Vista)
Creating a console window for Visual FoxPro application
Creating a device context for the specified printer
Creating a folder
Creating a mailslot
CryptoAPI: Collection of Providers class
Custom FTP Class for Visual FoxPro application
Custom HttpRequest class (WinHTTP)
Custom HttpRequest class (WinINet)
Determining if an Active Network Connection is Available
DiskFreeSpace class
Displaying bitmap using the AlphaBlend function
Displaying dimmed window behind VFP top-level form
Encapsulating access to the Windows Services in a class
Enumerating forms supported by a specified printer
Enumerating ports that are available for printing on a specified server
Enumerating print jobs and retrieving information for default printer (JOB_INFO_1 structures)
Enumerating print processors and supporting data types installed on the specified server
Enumerating printer drivers installed
Enumerating Processes -- WinNT
Enumerating the subkeys of a user-specific key
Finding parameters for the region specified
GetFileOwner - Get the owner of an NTFS file
Getting a bit more than the _CLIPTEXT offers
How to access a file using not its name but an alias (hard link)
How to adjust monitor brightness (Vista, monitor with DDC support)
How to block the ALT+TAB shortcut (WinXP)
How to block the PrintScreen key
How to create a service object
How to create MD-5 and SHA-1 hash values from a string
How to delete a service object
How to delete all print jobs for a printer
How to delete IE cookies, clear IE history and delete files in Temporary Internet Files directory
How to display a user-defined icon in the MessageBox dialog
How to display the port-configuration dialog box for a port on the specified server
How to display the Properties dialog box for a file (ShellExecuteEx)
How to download this reference`s archive through WinInet functions using InternetOpenUrl
How to enable the SE_SHUTDOWN_NAME privilege for the application
How to enumerate cookies and URL History entries in the cache of the local computer
How to enumerate sessions and processes on a specified terminal server
How to enumerate terminal servers within the specified Windows domain
How to extract frames from AVI files
How to fill a buffer with random bytes using Cryptography API Functions
How to find the application associated with a file name
How to find when the application started
How to find which fonts Windows uses for drawing captions, menus and message boxes
How to initiate System shutdown
How to make application automatically close all documents it opened
How to ping a remote site using IP Helper API calls
How to play AVI file on the _screen
How to prevent users from accessing the Windows Desktop and from switching to other applications
How to retrieve configuration data for a specified printer stored in the registry (PrinterDriverData key)
How to retrieve information about a cache entry (Internet Explorer)
How to retrieve the number of print jobs queued for the printer
How to retrieve version information for the specified file
How to run FoxPro application under different user name (impersonating user)
How to save registry key including its subkeys and values to a file
How to set Creation Date/Time for a folder (WinNT)
How to Start a Process as Another User (NT/XP/2K)
How to test file attributes (key method for FileExists and DirectoryExists routines)
How to write and read Window Properties for the specified window
HOWTO: Use the Win32 API to Access File Dates and Times
Locking and unlocking file of a VFP table
Locking the workstation
Monitoring changes occurring within a directory
Obtaining I/O counts for the current process
Obtaining physical parameters for a drive: sectors, clusters, cylinders...
Peer-to-peer LAN messenger built with Mailslot API functions
Placing an arbitrary rectangular area of main VFP window on the Clipboard
Printing Image File, programmatically set print page orientation to landscape
Reading and setting system access privileges for the current process
Reading current hardware profile
Reading entries from Event logs
Reading security permissions for NTFS files and folders
Reading the structure of VFP main menu
Removing FTP directory
Retrieveing information about the active window (even if it is not owned by the calling process)
Retrieving a handle to DLL and address of an exported function in it
Retrieving graphic capabilities of default printer
Retrieving local computer and user names
Retrieving the name of the default printer for the current user on the local computer (Win NT/XP)
Retrieving the System Time adjustment
Scanning a hierarchy of child windows down from the Windows Desktop
Setting the date and time that a file was created
Setting the last-error code for the FoxPro
Setting the volume label
Setting the Window Region for a form
Simple printer queue monitor: deletes, pauses, resumes print jobs for local printer
Start an executable from VFP application by using the CreateProcess
Starting external program from VFP and waiting for its termination
Storing registration key in the resources of an executable file
Testing serial ports
The DetectAutoProxyUrl function identifies the auto-config script location
Using Change Notification Objects to monitor changes to the printer or print server
Using EnumPrinters function to enumerate locally installed printers
Using File Mapping for enumerating files opened by Visual FoxPro
Using FtpCommand
Using LoadLibrary
Using named pipes for interprocess communication
Using shared memory to exchange data between two FoxPro applications
Using the CreateFile
Using the NetMessageBufferSend to send messages on the network
Using the Semaphore object
Using the Semaphore object to allow only one instance of VFP application running
Validating the heap of the calling process
Wininet last error description
Winsock: retrieving information from a host database for a given host name
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: 202  
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-2013 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.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (50.16.108.167)
4 sec.
Function group: 'Extensible Storage Engine (ESE, Jet Blue)'
7 sec.Example: 'How to position the GETPRINTER() dialog'
7.4 min.Example: 'Retrieving file information for the VFP executable running'
7.48 min.Example: 'Scanning a hierarchy of child windows down from the Windows Desktop'
41.12 min.Example: 'How to run FoxPro application under different user name (impersonating user)'
1.34 hrs.Function: 'DestroyMenu'
 Function: 'ToAscii'
 Example: 'Simulating DOEVENTS'
2.87 hrs.Example: 'Extensible Storage Engine class library'
Language: 'Visual FoxPro'
 Function: 'CeFindNextDatabaseEx'
Function group: 'Remote Application Programming (RAPI)'
Google
Advertise here!