Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to set Creation Date/Time for a folder (WinNT)
How to change the name and the size of the font in the MessageBox dialog
How to copy the image of a form to the Clipboard using Bitmap API functions
Drawing Windows frame controls using the DrawFrameControl function
Retrieving the name of the default printer for the current user on the local computer (Win NT/XP)
Creating the Open dialog box to specify the drive, directory, and name of a file to open
Winsock: connecting to a news server (NNTP, port 119)
How to make application automatically close all documents it opened
Reading list of folders and files on FTP server
How to block the PrintScreen key
How to extract frames from AVI files
Printing text on the main VFP window
Winsock: retrieving the standard host name and IP address for the local machine
Winsock: retrieving Web pages using sockets (HTTP, port 80)
Creating an Open dialog box to specify the drive, directory, and name of a file to open (Shell32 version)
Extended MessageBox Class
How to ping a remote site using IP Helper API calls
How to adjust monitor brightness (Vista, monitor with DDC support)
Pocket PC: custom RAPI class for operating with files and folders on mobile device
Confining Windows calculator inside the VFP main window
Converting a hexadecimal string to an integer
How to disable the Windows Clipboard (VFP9)
How to retrieve the number of print jobs queued for the printer
WAV file player
Placing a button on the VFP form as a new child window

User rating: 10/10 (3 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 demonstrates how to create a child window (the regular Windows button is always a child window for its container).

It is known that native FoxPro controls are not windows, as they are in VB or Delphi sets of controls, just name a few. Most of the times it creates a small inconvenience, unless you need more control on their properties.



For example, I do not know how to shape the native VFP CommandButton control to anything but a rectangle. Correct me if I am wrong. Transparent image does not solve the problem.
 
DO declare
 
LOCAL oForm As Tform
oForm=CREATEOBJECT("Tform")
oForm.Show()
READ EVENTS
* end of main
 
DEFINE CLASS Tform As Form
#DEFINE WM_PARENTNOTIFY 0x0210
#DEFINE WM_COMMAND 0x0111
#DEFINE WM_LBUTTONDOWN 0x201
#DEFINE WM_RBUTTONDOWN 0x204
#DEFINE GWL_WNDPROC -4
PROTECTED hWindow, hOrigProc
    hWindow=0
    hOrigProc=0
    Caption = "Adding the Windows Button to the form"
    Height = 250
    Width = 450
    Autocenter = .T.
    hButton = 0
 
    ADD OBJECT chAddButton As CheckBox WITH;
    Left=20, Top=20, Autosize=.T., Caption="Add Button"
 
PROCEDURE Destroy
    THIS.RemoveButton
    CLEAR EVENTS
 
PROCEDURE Activate
    IF THIS.hWindow = 0
        THIS.hWindow = GetFocus()
        THIS.hOrigProc = GetWindowLong(THIS.hWindow, GWL_WNDPROC)
 
        * link WM_PARENTNOTIFY window message to a method
        IF VERSION(5) >= 900
            = BINDEVENT(THIS.hWindow, WM_PARENTNOTIFY, THIS, "WindowProc")
            = BINDEVENT(THIS.hWindow, WM_COMMAND, THIS, "WindowProc")
        ENDIF
    ENDIF
 
PROCEDURE RemoveButton
    IF DestroyWindow(THIS.hButton) <> 0
        THIS.hButton = 0
    ENDIF
 
PROCEDURE AddButton
#DEFINE GWL_HINSTANCE -6
#DEFINE BS_DEFPUSHBUTTON 1
#DEFINE BS_PUSHBUTTON 0
#DEFINE BS_OWNERDRAW 0x000b
#DEFINE WS_VISIBLE 0x10000000
#DEFINE WS_CHILD 0x40000000
 
    LOCAL hApp, hParentHWnd, lnStyle, lnStyleX,;
        lnX, lnY, lnWidth, lnHeight
 
*    lnStyle = WS_VISIBLE+WS_CHILD+BS_PUSHBUTTON)
    lnStyle = BITOR(WS_VISIBLE, WS_CHILD, BS_PUSHBUTTON)
    lnStyleX = 0
 
    * handle to the window hosting the button
*    hParentHWnd=GetFocus()
    hParentHWnd=ThisForm.HWnd  && will not work for ShowWindow=2
 
    * handle to application instance
    hApp = GetWindowLong(hParentHWnd, GWL_HINSTANCE)
 
    * unique child-window identifier
    lnId = VAL(SYS(3))
 
    * size and position
    lnX = 140
    lnY = 50
    lnWidth = 100 
    lnHeight = 28
 
    * the predefined control class BUTTON is used 
    THIS.hButton = CreateWindow (lnStyleX, "BUTTON",;
        "True &Button", lnStyle,;
        lnX, lnY, lnWidth, lnHeight,;
        hParentHWnd, lnId, hApp, 0)
 
PROTECTED PROCEDURE WindowProc(hWindow as Integer,;
    nMsgID as Integer, wParam as Integer, lParam as Integer)
* requires VFP9, otherwise ignored
 
    LOCAL nReturn
    nReturn=0
 
    DO CASE
    CASE nMsgID=WM_COMMAND
        ACTIVATE SCREEN
        ? "WM_COMMAND: wParam=" + LTRIM(STR(wParam)) +;
            ", lParam=" + LTRIM(STR(lParam))
 
    CASE nMsgID=WM_PARENTNOTIFY
        ACTIVATE SCREEN
        ? "WM_PARENTNOTIFY: wParam=" + LTRIM(STR(wParam)) +;
            ", lParam=" + LTRIM(STR(lParam))
 
        DO CASE
        CASE wParam=WM_LBUTTONDOWN
            ?? " -- Left Button pressed"
        CASE wParam=WM_RBUTTONDOWN
            ?? " -- Right Button pressed"
        ENDCASE
 
    OTHERWISE
    * pass control to the original window procedure
        nReturn = CallWindowProc(THIS.hOrigProc, THIS.hWindow,;
            m.nMsgID, m.wParam, m.lParam)
    ENDCASE
RETURN nReturn
 
PROCEDURE chAddButton.Click
    IF ThisForm.hButton=0
        ThisForm.AddButton
    ELSE
        ThisForm.RemoveButton
    ENDIF
    THIS.Caption = IIF(ThisForm.hButton=0,;
        "Add Button", "Remove Button")
 
ENDDEFINE
 
PROCEDURE declare
    DECLARE INTEGER GetFocus IN user32
 
    DECLARE INTEGER DestroyWindow IN user32;
        INTEGER hWindow
 
    DECLARE INTEGER GetWindowLong IN user32;
        INTEGER hWnd, INTEGER nIndex
 
    DECLARE INTEGER CreateWindowEx IN user32 AS CreateWindow;
        INTEGER dwExStyle, STRING lpClassName,;
        STRING lpWindowName, INTEGER dwStyle,;
        INTEGER x, INTEGER y, INTEGER nWidth, INTEGER nHeight,;
        INTEGER hWndParent, INTEGER hMenu, INTEGER hInstance,;
        INTEGER lpParam
 

User rating: 10/10 (3 votes)
Rate this code sample:
  • ~
3503 bytes  
Created: 2002-02-05 14:42:28  
Modified: 2009-10-29 14:02:51  
Visits in 7 days: 160  
Listed functions:
CreateWindow
CreateWindowEx
DestroyWindow
GetFocus
GetWindowLong
Printer friendly API declarations
My comment:
A click on the button sends a notification (Windows message WM_COMMAND) to the window that owns the button. The VFP form for sure receives this message through its WindowProc function.

With no access to this function there hardly is a way to intercept this message and link a procedure to the click.

There are other predefined control classes like the BUTTON: COMBOBOX, EDIT, LISTBOX, SCROLLBAR, STATIC etc. The BUTTON class allows creating command buttons as well as checkboxes, radiobuttons, buttons with images and icons etc.

* * *
Things changed when VFP9 introduced new BINDEVENT() functionality.

* * *
The WM_PARENTNOTIFY message is sent to the parent of a child window when the child window is created or destroyed, or when the user clicks a mouse button while the cursor is over the child window.

In VFP9 new BINDEVENT() allows to intercept this message. So tecnically, left and right mouse clicks on such windowed button control can be detected by its parent form and linked to a method.
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:
Denis Carrizo (dcarrizo@starsoft.com.br) | 2005-04-27 12:06:59
Using the new bindevent() feature (VFP 9.0), it is possible get the notification when the button is clicked.

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.20.196.179)
6.19 hrs.Example: 'Class library providing access to the System Registry'
9.07 hrs.Example: 'Using the DrawText function'
9.66 hrs.Example: 'How to download a file from HTTP server using URL Monikers functions'
 Function: 'GetPwrCapabilities'
Function group: 'Power Management'
10.05 hrs.Example: 'How to check whether the system is 32-bit or 64-bit'
10.19 hrs.Function: 'MAPIReadMail'
 Function: 'FindClose'
Function group: 'File Management'
16.4 hrs.Function: 'GetMenuItemInfo'
Function group: 'Menu'
 Example: 'Enumerating files opened on the network'
16.53 hrs.Example: 'Disabling drawing in the VFP form'
Google
Advertise here!