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
Reading the structure of VFP main menu

User rating: 10/10 (2 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Before you begin:
This code reads structure of main VFP window menu and saves it in an XML document (the Microsoft.XMLDOM object is used).

This is an output this code produces -- the structure of VFP9 main menu.

See also:
  • Transparent Menu on top-level form (requires VFP9)
  • How to hot-track menu item selection in top-level form
  • Adding user-defined items to the Control Menu of VFP form

  •  
    PRIVATE objXml, cTargetFile
    cTargetFile = "c:\temp\mnu.xml"  && replace with valid file name
     
    DECLARE INTEGER GetActiveWindow IN user32
    objXml = CREATEOBJECT("Tmenu", GetActiveWindow(), cTargetFile)
     
    IF TYPE("objXML") = "O"
        objXml.SaveXML
        objXml.ViewXml
    ELSE
        = MessageB("Invalid input parameters. ", 48, " Error")
    ENDIF
     
    DEFINE CLASS Tmenu As Custom
    PROTECTED hWindow, hMenu, xmldoc, rootnode, targetfile
     
    PROCEDURE Init(hwnd, cTargetFile)
        THIS.declare
     
        cTargetFile = FULLPATH(cTargetFile)
        IF PathFileExists(SUBS(cTargetFile, 1,RAT("\",cTargetFile))) = 0
            RETURN .F.  && invalid path
        ENDIF
     
        THIS.hWindow = hwnd
        THIS.hMenu = GetMenu(hwnd)
        IF IsMenu(THIS.hMenu) = 0
            RETURN .F.  && no menu found
        ENDIF
     
        WITH THIS
            .targetfile = cTargetFile
            .xmldoc = CreateObject("Microsoft.XMLDOM")
     
            .xmldoc.LoadXML(CHR(60)+[?xml version="1.0" ?]+CHR(62)+;
                CHR(60)+[TopMenu ]+CHR(47)+CHR(62))
     
            .rootnode = .xmldoc.documentElement()
            .SetProperty(.rootnode, "hWnd", m.hwnd)
            .DescribeNode(-1, .rootnode, .hMenu, 0,0)  && recursion
        ENDWITH
     
    PROCEDURE SaveXML
        THIS.xmldoc.Save(THIS.targetfile)
     
    PROCEDURE ViewXml
        = ShellExecute(0,"open", THIS.targetfile, "", "", 3)
     
    PROCEDURE DescribeNode(hParent, objNode, hMenu, lnItemIndex, lnItemID)
        LOCAL lnItemCount, objSubnode, hSubitem, lnIndex, lnID,;
            lnItemType, lnDataType, lnChecked, lnUnchecked, lnBitmap, lcItemType
     
        THIS.SetProperty(objNode, "hMenu", m.hMenu)
        THIS.SetProperty(objNode, "ItemIndex", m.lnItemIndex)
        THIS.SetProperty(objNode, "ItemID", m.lnItemID)
     
        IF hParent = -1  && root
            THIS.SetProperty(objNode, "WinCaption", THIS.GetWinCaption())
        ELSE
            STORE 0 TO lnItemType, lnDataType, lnBitmap, lnChecked, lnUnchecked
            THIS.GetItemData(hParent, lnItemIndex, @lnItemType,;
                @lnDataType, @lnChecked, @lnUnchecked, @lnBitmap)
     
            THIS.SetProperty(objNode, "ItemType", lnItemType)
            IF lnItemType <> 0
                lcItemType = THIS.GetItemTypeStr(lnItemType)
                THIS.SetProperty(objNode, "ItemTypeStr", lcItemType)
            ENDIF
            THIS.SetProperty(objNode, "DataType", lnDataType)
        ENDIF
     
        IF hMenu = -1
            RETURN
        ENDIF
     
        lnItemCount = GetMenuItemCount(m.hMenu)
        THIS.SetProperty(objNode, "ItemCount", m.lnItemCount)
     
        FOR lnIndex = 0 TO lnItemCount-1
            lnID = GetMenuItemID(m.hMenu, lnIndex)
            IF lnID = -1  && submenu
                objSubnode = THIS.xmldoc.CreateElement("SubMenu")
                hSubitem = THIS.GetSubmenuHandle(m.hMenu, m.lnIndex)
            ELSE  && menu item
                objSubnode = THIS.xmldoc.CreateElement("MenuItem")
                hSubitem = -1
            ENDIF
            objNode.AppendChild(objSubnode)
            THIS.DescribeNode(hMenu, objSubnode, hSubitem, lnIndex, lnID)
        ENDFOR
    RETURN
     
    PROCEDURE SetProperty(objNode, lcProperty, lvValue)
        LOCAL loProperty, loAttr, lcType
        loProperty = objNode.SelectSingleNode(lcProperty)
     
        IF TYPE("loProperty.Value") <> "C"
            loProperty = THIS.xmldoc.CreateElement(lcProperty)
            objNode.AppendChild(loProperty)
        ENDIF
     
        lcType = TYPE("lvValue")
        DO CASE
        CASE lcType = "C"
            lvValue = ALLTRIM(lvValue)
        CASE lcType = "L"
            lvValue = Iif(lvValue, "true","false")
        CASE lcType = "D"
            lvValue = DTOC(lvValue)
        CASE lcType = "T"
            lvValue = TTOC(lvValue)
        CASE lcType = "N"
            lvValue = LTRIM(STR(lvValue, 20))
        ENDCASE
        loProperty.Text = lvValue
     
    PROCEDURE declare
    #DEFINE MENUITEMINFO_SIZE 48
    #DEFINE MIIM_TYPE  16
    #DEFINE MIIM_SUBMENU 4
    #DEFINE MIIM_DATA 32
    #DEFINE MF_STRING 0
    #DEFINE MF_BITMAP 4
    #DEFINE MF_MENUBARBREAK 0x20
    #DEFINE MF_MENUBREAK 0x40
    #DEFINE MIIM_BITMAP 0x80
    #DEFINE MF_OWNERDRAW 0x100
    #DEFINE MFT_RADIOCHECK 0x200
    #DEFINE MF_SEPARATOR 0x800
    #DEFINE MFT_RIGHTORDER 0x2000
    #DEFINE MF_RIGHTJUSTIFY 0x4000
     
        DECLARE INTEGER GetObjectType IN gdi32 INTEGER h
        DECLARE INTEGER GetLastError IN kernel32
        DECLARE INTEGER GetMenu IN user32 INTEGER hWnd
        DECLARE INTEGER IsMenu IN user32 INTEGER hMenu
        DECLARE INTEGER PathFileExists IN shlwapi STRING pszPath
        DECLARE INTEGER GetMenuItemCount IN user32 INTEGER hMenu
        DECLARE INTEGER GetMenuItemID IN user32 INTEGER hMenu, INTEGER nPos
     
        DECLARE INTEGER ShellExecute IN shell32;
            INTEGER hwnd, STRING lpOperation, STRING lpFile,;
            STRING lpParams, STRING lpDir, INTEGER nShowCmd
     
        DECLARE INTEGER GetMenuItemInfo IN user32;
            INTEGER hMenu, INTEGER uItem, INTEGER fByPosition, STRING @lpmii
     
        DECLARE INTEGER GetMenuString IN user32;
            INTEGER hMenu, INTEGER uIDItem, STRING @lpString,;
            INTEGER @nMaxCount, INTEGER uFlag
     
        DECLARE INTEGER GetWindowText IN user32;
            INTEGER hwnd, STRING @lpString, INTEGER cch
     
    FUNCTION GetWinCaption
        LOCAL lcCaption
        lcCaption = SPACE(250)
        = GetWindowText(THIS.hWindow, @lcCaption, LEN(lcCaption))
    RETURN ALLTRIM(STRTRAN(lcCaption, Chr(0), ""))
     
    FUNCTION GetSubmenuHandle(hMenu, lnIndex)
        LOCAL lcBuffer
        lcBuffer = num2dword(MENUITEMINFO_SIZE) +;
            num2dword(MIIM_SUBMENU) + Repli(Chr(0), 40)
        = GetMenuItemInfo(hMenu, lnIndex, 1, @lcBuffer)
    RETURN buf2dword(SUBSTR(lcBuffer, 21,4))
     
    PROCEDURE GetItemData
    PARAM hMenu, lnIndex, lnIType, lnDType, lnChecked, lnUnchecked, lnBMP
        LOCAL lcBuffer, loItemData
        loItemData = CreateObject("PChar", Repli(Chr(0), 250))
        lcBuffer = num2dword(MENUITEMINFO_SIZE) +;
            num2dword(MIIM_TYPE+MIIM_DATA) + Repli(Chr(0), 28) +;
            num2dword(loItemData.hMem) + Repli(Chr(0), 8)
        = GetMenuItemInfo(hMenu, lnIndex, 1, @lcBuffer), lcBuffer
        lnIType = buf2dword(SUBSTR(lcBuffer, 9,4))
        lnDType = buf2dword(SUBSTR(lcBuffer, 37,4))
        lnBMP = buf2dword(SUBSTR(lcBuffer, 45,4))
     
    FUNCTION BTest(lnExpr, lnTest)
    RETURN BitAnd(lnExpr, lnTest) = lnTest
     
    FUNCTION GetItemTypeStr(lnType)
        LOCAL cType
        cType = ""
        cType = cType + Iif(THIS.BTest(lnType, MF_BITMAP),       "bitmap;", "")
        cType = cType + Iif(THIS.BTest(lnType, MF_MENUBARBREAK), "barbreak;","")
        cType = cType + Iif(THIS.BTest(lnType, MF_MENUBREAK),    "break;","")
        cType = cType + Iif(THIS.BTest(lnType, MIIM_BITMAP),     "xbitmap;","")
        cType = cType + Iif(THIS.BTest(lnType, MF_OWNERDRAW),    "owner;","")
        cType = cType + Iif(THIS.BTest(lnType, MFT_RADIOCHECK),  "radio;","")
        cType = cType + Iif(THIS.BTest(lnType, MF_SEPARATOR),    "separator;","")
        cType = cType + Iif(THIS.BTest(lnType, MFT_RIGHTORDER),  "rightord;","")
        cType = cType + Iif(THIS.BTest(lnType, MF_RIGHTJUSTIFY), "rightjust;","")
    RETURN cType
    ENDDEFINE
     
    DEFINE CLASS PChar As Custom
        hMem=0
     
    PROCEDURE Init(lcString)
        THIS.setValue(lcString)
    PROCEDURE Destroy
        THIS.ReleaseString
     
    PROCEDURE SetValue(lcString) && assigns new string value
    #DEFINE GMEM_FIXED 0 
        DECLARE INTEGER GlobalAlloc IN kernel32 INTEGER, INTEGER
        DECLARE RtlMoveMemory IN kernel32 As Str2Heap;
            INTEGER, STRING @, INTEGER
     
        LOCAL lnSize
     
        THIS.ReleaseString
        lcString = lcString + Chr(0)
        lnSize = Len(lcString)
     
        THIS.hMem = GlobalAlloc(GMEM_FIXED, lnSize)
        IF THIS.hMem <> 0
            = Str2Heap(THIS.hMem, @lcString, lnSize)
        ENDIF
     
    PROCEDURE ReleaseString  && releases allocated memory
        IF THIS.hMem <> 0
            DECLARE INTEGER GlobalFree IN kernel32 INTEGER
            = GlobalFree(THIS.hMem)
            THIS.hMem = 0
        ENDIF
    ENDDEFINE
     
    FUNCTION buf2dword(cBuffer)
    RETURN Asc(SUBSTR(cBuffer, 1,1)) + ;
        BitLShift(Asc(SUBSTR(cBuffer, 2,1)),  8) +;
        BitLShift(Asc(SUBSTR(cBuffer, 3,1)), 16) +;
        BitLShift(Asc(SUBSTR(cBuffer, 4,1)), 24)
     
    FUNCTION num2dword(lnValue)
    #DEFINE m0 0x0000100
    #DEFINE m1 0x0010000
    #DEFINE m2 0x1000000
        IF lnValue < 0
            lnValue = 0x100000000 + lnValue
        ENDIF
        LOCAL b0, b1, b2, b3
        b3 = Int(lnValue/m2)
        b2 = Int((lnValue - b3*m2)/m1)
        b1 = Int((lnValue - b3*m2 - b2*m1)/m0)
        b0 = Mod(lnValue, m0)
    RETURN Chr(b0)+Chr(b1)+Chr(b2)+Chr(b3)
     
     
     

    User rating: 10/10 (2 votes)
    Rate this code sample:
    • ~
    7678 bytes  
    Created: 2002-10-31 11:29:48  
    Modified: 2009-06-11 13:39:43  
    Visits in 7 days: 114  
    Listed functions:
    GetActiveWindow
    GetLastError
    GetMenu
    GetMenuItemCount
    GetMenuItemID
    GetMenuItemInfo
    GetMenuString
    GetObjectType
    GetWindowText
    GlobalAlloc
    GlobalFree
    IsMenu
    PathFileExists
    ShellExecute
    Printer friendly API declarations
    My comment:
    All VFP menu items are of OWNERDRAW type. That means the main VFP window receives at least two types of menu-related window messages: WM_MEASUREITEM and WM_DRAWITEM.

    The first one is received when a menu item is created. The second message -- every time an item has to be drawn or redrawn (for example, when selected or unselected).

    VFP9 allows intercepting window messages (BINDEVENT). So an application can receive and process the WM_DRAWITEM and other menu-related window messages.
    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 (54.224.79.93)
    3 sec.Function: 'SHRegEnumUSKey'
    5.5 hrs.Function: 'InternetCloseHandle'
    Function group: 'Internet Functions (WinInet)'
    9.08 hrs.Function: 'waveOutGetVolume'
    15.01 hrs.Function: 'AVIStreamGetFrameClose'
    15.97 hrs.Function: 'SQLConnect'
     Example: 'Retrieveing information about the active window (even if it is not owned by the calling process)'
    16.54 hrs.Example: 'Storing screen shot of a form to enhanced metafile (*.emf)'
     Example: 'How to display the port-configuration dialog box for a port on the specified server'
    21.42 hrs.Function: 'GetCurrentThreadId'
    Function group: 'Process and Thread'
    22.25 hrs.Example: 'Using Video Capture: displaying on FoxPro form frames and previewing video obtained from a digital camera'
    Language: 'C++'
    Google
    Advertise here!