Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
Enumerating the subkeys of a user-specific key
How to display a Task Dialog (Vista)
How to download this reference`s archive through WinInet functions using InternetOpenUrl
How to print picture stored in enhanced-format metafile (*.emf)
Obtaining OS memory performance information
Printing text with the Escape function
Creating a clipping region from the path selected into the device context of a form
MapiSendMail class for Visual FoxPro application
Storing registration key in the resources of an executable file
Terminating all running applications from a VFP program
Creating a device context for the specified printer
Displaying Windows shell folders in TreeView control with Visual FoxPro FLL
Using FtpCommand
Disabling drawing in the VFP form
Enumerating the subkeys for a given registry key
Retrieving IP statistics for the computer
Using the heap of the calling process to allocate memory blocks
Changing pitch and speed of a wave file
Comparing dimensions of the VFP main window with _SCREEN properties
How to create a desktop shortcut (shell link)
Pocket PC: base class
Pocket PC: enumerating mounted database volumes and databases in the Object Store
Reading metrics for the currently selected font
URL: splitting into its component parts
GDI+: Scrolling through large image using the mouse

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:
The code is based on custom GDI+ class. Download the class module first and save it in gdiplus.prg file.

* * *
The pictures taken with modern digital cameras are usually wider and higher than above average display resolution.

Cathedral Bluffs Yacht Club in Scarborough, Ontario
1920 by 1200 pixels, which requires fairly decent video card and monitor these days, is only 2.3 megapixel size. Uncropped 10.1 megapixel shot taken with Canon EOS 400D is 3888 by 2592 pixels -- roughly two times higher and wider.

To view so large picture in its original size you need a scrollable form. Normally it is programmed by setting the form`s ScrollBars to 3 and having the image control`s Stretch property set to 0 (default). The picture automatically stretches the image control. After that any part of it can be conveniently scrolled to with the form`s two scroll bars.



An example demonstrating this approach (the picture above) is provided in the article How To Programmatically Scroll a Visual FoxPro Form published on the Microsoft Help and Support web site.

In the Microsoft`s code I replaced the Shape control with the Image control and changed the ScrollBars property from 2 to 3. Also I added the HorisScrollPos property modelled by the VertScrollPos and tweaked the code a little bit.

* * *
Except with the scroll bars, I would also like scrolling the image by simply clicking on it and dragging (like in Picasa, for example). The example from Microsoft can certainly be modified to handle the MouseMove and MouseWheel events of the Image control.

Finding less fun in improving someone else`s code I decided on programming similar functionality using the GDI+ library. The results came not bad indeed.

See also:
  • Creating thumbnails to preview images in a directory
  •  
    cPath = GETENV("windir")+"\Web\Wallpaper"
    nCount = ADIR(arr, m.cPath+"\*.jpg")
    cFilename = cPath + "\"+arr[m.nCount,1]
     
    LOCAL oForm As TImageViewer
    oForm = CREATEOBJECT("TImageViewer", m.cFilename)
    IF VARTYPE(oForm)="O"
        oForm.Show(1)
    ENDIF
    * end of main
     
    DEFINE CLASS TImageViewer As Form
    #DEFINE SRCCOPY 0x00CC0020
        Width=_screen.Width * 0.7
        Height=_screen.Height * 0.4
        MinButton=.F.
        MaxButton=.F.
        Autocenter=.T.
        gdiplus=0
        oImg=0
        hDC=0
        formgraphics=NULL
        OffsetX=0
        OffsetY=0
        SavedOffsetX=0
        SavedOffsetY=0
        ScrollStepX=10
        ScrollStepY=10
     
        ADD OBJECT sbar As Tbar
     
    PROCEDURE Init(cImageFile As String)
        SET PROCEDURE TO gdiplus ADDITIVE
     
        WITH THIS
            .declare
            .gdiplus = CREATEOBJECT("gdiplusinit")
            .oImg = CREATEOBJECT("gdiimage", m.cImageFile)
            IF .oImg.imgwidth=0
                = MESSAGEBOX("The image file " + CHR(13) +;
                    m.cImageFile + CHR(13) +;
                    "is either invalid " +;
                    "or missing.     ", 48, "Error!")
                RETURN .F.
            ENDIF
            .MaxWidth = MAX(.oImg.imgwidth,300)
            .MaxHeight = MAX(.oImg.imgheight, 140)
        ENDWITH
     
        * redraw the image upon changes in the main vfp window
        = BINDEVENT(_screen, "Activate", THIS, "Activate")
        = BINDEVENT(_screen, "Resize", THIS, "Resize")
     
        THIS.sbar.Panels(1).Text = m.cImageFile
     
    PROCEDURE Destroy
        WITH THIS  && the order of release is important
            .formgraphics=NULL
            .oImg=NULL
            .gdiplus=NULL
            = ReleaseDC(.HWnd, .hDC)
        ENDWITH
     
    PROCEDURE Activate
        THIS.AdjustClippingRegion
        THIS.DrawImage
     
    PROCEDURE Resize
        THIS.AdjustClippingRegion
        THIS.DrawImage
     
    PROCEDURE OffsetX_ASSIGN(nValue As Number)
    * the X coordinate of the upper-left corner of the image
    * the value is either zero (initial) or negative
        IF VARTYPE(THIS.oImg) <> "O";
            OR THIS.oImg.imgwidth <= THIS.Width;
            OR m.nValue > 0
            THIS.OffsetX=0
        ELSE
            THIS.OffsetX = MAX(m.nValue,;
                THIS.Width - THIS.oImg.imgwidth)
        ENDIF
     
    PROCEDURE OffsetY_ASSIGN(nValue As Number)
    * the Y coordinate of the upper-left corner of the image
    * the value is either zero (initial) or negative
        IF VARTYPE(THIS.oImg) <> "O";
            OR THIS.oImg.imgheight <= THIS.Height;
            OR m.nValue > 0
            THIS.OffsetY=0
        ELSE
            THIS.OffsetY = MAX(m.nValue,;
                THIS.Height - THIS.oImg.imgheight)
        ENDIF
     
    PROCEDURE AdjustClippingRegion
        WITH THIS
            .LockScreen=.F.
     
            IF .hDC <> 0
                .formgraphics=NULL
                = ReleaseDC(.HWnd, .hDC)
            ENDIF
     
            * create Graphics object from the device context
            * of the form; note that the GetDC is used (not GetWindowDC),
            * the output affects only the form`s client area
            .hDC = GetDC(.HWnd)
            .formgraphics = CREATEOBJECT("graphics", .hDC)
     
            * reset the upper-left corner to (0,0)
            STORE 0 TO .OffsetX, .OffsetY
     
            * the clipping is required only if the image
            * display area is smaller than the form`s client area
    *!*            = GdipSetClipRectI(.formgraphics.graphics,;
    *!*                0, 0, .Width, .Height, 0)
     
            * this prevents VFP from painting on the form
            .LockScreen=.T.
        ENDWITH
     
    PROCEDURE DrawImage
        IF VARTYPE(THIS.oImg) <> "O"
            RETURN
        ENDIF
     
        THIS.Caption = "("+TRANSFORM(THIS.oImg.imgwidth) +;
            ", " + TRANSFORM(THIS.oImg.imgheight) + ")" +;
            " offset (" + TRANSFORM(-THIS.OffsetX) +;
            ", " + TRANSFORM(-THIS.OffsetY) + ")"
     
        * the offset is the point where the upper-left
        * corner of the image is positioned;
        * this is either zero (initial) or negative value
        THIS.formgraphics.DrawImage(THIS.oImg,;
            THIS.OffsetX, THIS.OffsetY)
     
    PROCEDURE MouseDown
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
        IF nButton=1  && store the coordinates
            WITH THIS
                .SavedOffsetX=.OffsetX - m.nXCoord
                .SavedOffsetY=.OffsetY - m.nYCoord
            ENDWITH
        ENDIF
     
    PROCEDURE MouseUp
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
        IF nButton=1  && store the coordinates
            WITH THIS
                .SavedOffsetX=.OffsetX - m.nXCoord
                .SavedOffsetY=.OffsetY - m.nYCoord
            ENDWITH
        ENDIF
     
    PROCEDURE MouseMove
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
    * the image is scrolled when the mouse is moved
    * and the left button is pressed
        IF nButton = 1
            WITH THIS  && redraw the image at new offset
                IF m.nShift <> 2  && vertical only, when CTRL pressed
                    .OffsetX = .SavedOffsetX + m.nXCoord
                ENDIF
                IF m.nShift <> 1  && horizontal only, when Shift pressed
                    .OffsetY = .SavedOffsetY + m.nYCoord
                ENDIF
                .DrawImage
            ENDWITH
        ENDIF
     
    PROCEDURE MouseWheel
    LPARAMETERS nDirection, nShift, nXCoord, nYCoord
    * The mouse wheel scrolls the image vartically or horizontally.
        IF nShift = 0  && vertical scroll
            THIS.OffsetY = THIS.OffsetY +;
                IIF(nDirection > 0, 1, -1) * THIS.ScrollStepX
        ELSE  && horizontal scroll if Shift|Ctrl|Alt pressed
            THIS.OffsetX = THIS.OffsetX +;
                IIF(nDirection > 0, 1, -1) * THIS.ScrollStepY
        ENDIF
        THIS.DrawImage
     
    PROCEDURE KeyPress
    LPARAMETERS nKeyCode, nShiftAltCtrl
        DO CASE
        CASE nKeyCode=19  && left
            THIS.OffsetX = THIS.OffsetX + THIS.ScrollStepX
        CASE nKeyCode=5  && up
            THIS.OffsetY = THIS.OffsetY + THIS.ScrollStepY
        CASE nKeyCode=4  && right
            THIS.OffsetX = THIS.OffsetX - THIS.ScrollStepX
        CASE nKeyCode=24  && down
            THIS.OffsetY = THIS.OffsetY - THIS.ScrollStepY
        ENDCASE
        THIS.DrawImage
     
    PROCEDURE Moved
    * the form can be dragged outside of the main window
    * this event handler provides the image not to disappear
        THIS.DrawImage
     
    PROCEDURE declare
        DECLARE INTEGER GetWindowDC IN user32 INTEGER hwnd
        DECLARE INTEGER GetDC IN user32 INTEGER hwnd
        DECLARE INTEGER ReleaseDC IN user32 INTEGER hwnd, INTEGER dc
     
        DECLARE INTEGER GdipSetClipRectI IN gdiplus;
            INTEGER graphics, INTEGER x, INTEGER y,;
            INTEGER nWidth, INTEGER nHeight,;
            INTEGER combineMode
     
    ENDDEFINE
     
    DEFINE CLASS Tbar As OleControl
        OleClass="MSComctlLib.SBarCtrl.2"
    PROCEDURE Init
        THIS.Height=21
        THIS.Panels(1).Width = 2000
    ENDDEFINE
     

    User rating: 10/10 (2 votes)
    Rate this code sample:
    • ~
    5886 bytes  
    Created: 2009-01-08 13:15:34  
    Modified: 2009-04-24 12:06:19  
    Visits in 7 days: 143  
    Listed functions:
    GdipSetClipRectI
    GetDC
    GetWindowDC
    ReleaseDC
    Printer friendly API declarations
    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 (23.22.76.170)
    41.38 min.Example: 'Obtaining provider name for a specific type of network'
    41.5 min.Example: 'Enumerating network resources'
    41.55 min.Example: 'Configuring DEVMODE structure for a printer'
    4.95 hrs.Function: 'CeRegOpenKeyEx'
    4.96 hrs.Example: 'Verifying a file using the Authenticode policy provider'
    5.19 hrs.Example: 'Confining Windows calculator inside the VFP main window'
     Function: 'NetGroupEnum'
    8.02 hrs.Example: 'GetProcessVersion points at target OS'
    18.72 hrs.Example: 'Displaying Windows shell folders in TreeView control with Visual FoxPro FLL'
     Example: 'Uploading file to the FTP server using InternetWriteFile'
    Google
    Advertise here!