Windows Selection Dialogs
PowerBASIC provides convenient commands for displaying commonly used Windows selection dialogs, saving programmers from writing the detailed code otherwise needed to display the dialogs.

The supported dialogs are:

  • Open File
  • Save File
  • Browse Folders
  • Select Printer
  • Select Color
  • Select Font

Images, syntax and paramenters of the dialogs are shown below. Source code is provided showing how the tutorial images were created.

Browse Folders
The browse dialog allows a user to select a folder.

    DISPLAY BROWSE [hParent], [xpos], [ypos], title$, start$, _
                   flags TO folder$

    hParent - dialog handle
    xpos - upper left
    ypos - upper left
    title$ - caption display
    start$ - starting folder
    folder$ - selected folder ("" if none selected)
    flags -  (see bottom of page for flag list)

Color
The color dialog allows the user to select a color, or select from 16 pre-defined custom colors.

    DISPLAY COLOR [hParent], [xpos], [ypos], firstcolor, _
                  custcolors, flags TO color&

    hParent - dialog handle
    xpos - upper left
    ypos - upper left
    firstcolor - RGB of selected color
    custcolors - UDT of 16 colors (LONG)
    flags      - %CC_FULLOPEN - display entire dialog
               - %CC_PREVENTFULLOPEN - hide custom colors
               - %CC_SHOWHELP - display Help button
    color& - RGB value of selected color. (-1 if no selection)

Font
The font dialog allows the user to select the font - including name, size, and style.

    DISPLAY FONT [hParent], [xpos], [ypos], defname$, defpoints&, _
                 defstyle&, flags& TO _
                 TO fontname$, points&, style& [,color&, charset&]

    hParent - dialog handle
    xpos - upper left
    ypos - upper left
    defname$ - pre-selected font
    defpoint& - pre-selected point size
    defstyle& - pre-selected attribute (see style& below)
    flags& - (see bottom of page for full flag list)
    fontname$ - selected font name
    points& - selected point size
    style& - selected attribute:
             normal(0), bold(1), italic(2), 
             underline(4), strikeout(8)
    color& - RGB color value
    charset& - chosen character set (US=0)

OpenFile
The Open File dialog allows the user to select an existing file.

    DISPLAY OPENFILE [hParent], [xpos], [ypos], title$, folder$, _
                     filter$, start$, defext$, flags TO filename$

    hParent - dialog handle
    xpos - upper left
    ypos - upper left
    title$ - caption title
    folder$ - starting folder
    filter$ - pairs of filters (description/extension)
              CHR$("BASIC", 0, "*.BAS", 0)
    start$ - starting file name
    defext$ - default extension to add to file name
    flags -  (see bottom of page for flag list)
    filename$ - file to open

Select Printer
The Printer dialog allows the user to see all printers available to their PC and select the printer to which printing will be sent.

    XPRINT ATTACH Choose   

SaveFile
The Save File dialog allows the user to name a file and to select the folder where the files will be saved.

   DISPLAY SAVEFILE [hParent], [xpos], [ypos], title$, folder$, _
                    filter$, start$, defext$, flags TO filename$

    hParent - dialog handle
    xpos - upper left
    ypos - upper left
    title$ - caption title
    folder$ - starting folder
    filter$ - pairs of filters (description/extension)
              CHR$("BASIC", 0, "*.BAS", 0)
    start$ - starting file name
    defext$ - default extension to add to file name
    flags -  (see bottom of page for flag list)
    filename$ - filename to save

Dialog Flags
The flags for all but the color browser are provided here so that they wouldn't intrude on the information flow above. A single flag may be used, or they be combined with OR to combine their results.

Browse Folder FlagsDescription
%BIF_BROWSEINCLUDEFILES The dialog box will display both files and folders.
%BIF_BROWSEINCLUDEURLS Display URL's (must set %BIF_USENEWUI and %BIF_BROWSEINCLUDEFILES).
%BIF_DONTGOBELOWDOMAIN Exclude network folders in treeview.
%BIF_EDITBOX Display edit control for user to enter information.
%BIF_NEWDIALOGSTYLE Advanced interface (larger, resizable, drag-and-drop, reordering, shortcut menus, new folders, delete, and other shortcut menu commands.
%BIF_NONEWFOLDERBUTTON Exclude "New Folder" button.
%BIF_NOTRANSLATETARGETS Allow opening a shortcut (not the target file).
%BIF_RETURNFSANCESTORS File system ancestors only (OK button is grayed on other types).
%BIF_RETURNONLYFSDIRS File system directories only (OK button is grayed on other types).
%BIF_SHAREABLE Display remote, shareable resources (must set %BIF_NEWDIALOGSTYLE).
%BIF_UAHINT With %BIF_NEWDIALOGSTYLE, adds usage hint in lieu of edit box.
%BIF_USENEWUI Include edit box with advanced user interface.
%BIF_VALIDATE Require a valid result or CANCEL.

Browse Font FlagDescription
%CF_BOTH Screen and printer fonts displayed.
%CF_TTONLY TrueType fonts only.
%CF_EFFECTS Enable strikeout, underline, and color effects.
%CF_FIXEDPITCHONLY Display on fixed-pitch fonts.
%CF_FORCEFONTEXIST Only allows selection existing fonts.
%CF_NOSTYLESEL Dialog has no initial style selection.
%CF_NOSIZESEL Dialog has no initial size selection.
%CF_NOSIMULATIONS Does not display GDI font simulations.
%CF_NOVECTORFONTS Does not allow vector font selections.
%CF_PRINTERFONTS Display only fonts supported by the printer.
%CF_SCALABLEONLY Only allow selection of scalable fonts.
%CF_SCREENFONTS Lists only screen fonts supported by OS.
%CF_WYSIWYG Only allow selection fonts available on both the printer and the display (must set %CF_BOTH and %CF_SCALABLEONLY flags).

Open File FlagsDescription
%OFN_ALLOWMULTISELECT Allow multiple selections. Return values are null-terminated names.
%OFN_CREATEPROMPT Allow file which does not exist.
%OFN_ENABLESIZING Dialog is resizable.
%OFN_EXPLORER Explorer style interface (default state).
%OFN_FILEMUSTEXIST File name must exist.
%OFN_NOCHANGEDIR Maintains current directory regardless of user selection.
%OFN_NODEREFERENCELINKS Returns name of shortcut file.
%OFN_NONETWORKBUTTON Hides and disables the network button.
%OFN_NOTESTFILECREATE File not created before the dialog is closed.
%OFN_NOVALIDATE File name not validated for invalid characters.
%OFN_PATHMUSTEXIST Allows only valid paths and filenames.
%OFN_SHAREWARE Returns network filename even if sharing violation occurs.

Save File FlagsDescription
%OFN_ALLOWMULTISELECT Allow multiple selections. Return values are null-terminated names.
%OFN_CREATEPROMPT Allow file which does not exist.
%OFN_ENABLESIZING Dialog is resizable.
%OFN_EXPLORER Explorer style interface (default state).
%OFN_FILEMUSTEXIST File name must exist.
%OFN_NOCHANGEDIR Maintains current directory regardless of user selection.
%OFN_NODEREFERENCELINKS Returns of name shortcut file.
%OFN_NONETWORKBUTTON Hides and disables the network button.
%OFN_NOTESTFILECREATE File not created before the dialog is closed.
%OFN_NOVALIDATE File name not validated for invalid characters.
%OFN_PATHMUSTEXIST Allows only valid paths and filenames.
%OFN_SHAREWARE Returns network filename even if sharing violation occurs.

Source Code
Here is the souce code that I used to generate the dialog images above.

#COMPILE EXE
#DIM ALL

TYPE CustomColors
    c(16) AS LONG
END TYPE

FUNCTION PBMAIN () AS LONG

    LOCAL hParent AS DWORD
    LOCAL hDlg AS DWORD
    LOCAL result&, Style&, exStyle&
    DIALOG NEW hParent, "My Form",,, 160, 50, Style&, exStyle& TO hDlg
    DIALOG SHOW MODELESS hdlg TO result&

'  Folder Browers =====================================

    DIM gStartPath  AS STRING  ' path to start search from
    DISPLAY BROWSE  hParent, 100,100,"Browse", "", _
                    %BIF_RETURNONLYFSDIRS OR _
                    %BIF_DONTGOBELOWDOMAIN OR _
                    %BIF_NONEWFOLDERBUTTON TO gStartPath

'  Font Browser =======================================

    LOCAL  newFontName  AS STRING
    LOCAL  newFontSize  AS LONG
    LOCAL  newFontStyle AS LONG
    DISPLAY FONT hDlg, , , "", 8&, 0&, %CF_BOTH OR _
                     %CF_FORCEFONTEXIST OR _
                     %CF_INITTOLOGFONTSTRUCT OR _
                     %CF_NOSCRIPTSEL _
                     TO newFontName, newFontSize, newFontStyle

' Color Browser ======================================

    LOCAL colorresult&
    LOCAL custom AS customcolors

    DISPLAY COLOR hParent, , , &HFFFFFF, Custom , _
                  %CC_PREVENTFULLOPEN TO colorresult&


' Open File ==========================================

  LOCAL  dwStyle AS DWORD
  LOCAL  Buf     AS STRING
  LOCAL  sFile   AS STRING
  STATIC sPath   AS STRING  ' Use a Static path variable
  IF LEN(sPath) = 0 THEN sPath = CURDIR$
  sFile = "*.BAS;*.INC;*.RC"
  Buf = CHR$("All files (*.*)", 0, "*.*", 0)
  dwStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY
  DISPLAY OPENFILE hDlg, , , "", sPath, buf, "", "BAS", dwStyle TO sFile

' Select Printer  =========================================

  XPrint Attach Choose

' Save File  =========================================

    LOCAL rf                    AS LONG     ' File and file result variables
    LOCAL reptname              AS STRING   ' Name for the file to be created
    LOCAL i                     AS LONG     ' Housekeeping counter

    DISPLAY SAVEFILE hDlg, , , "Save", CURDIR$, buf, "", "", _
                    %OFN_PATHMUSTEXIST OR %OFN_EXPLORER OR _
                    %OFN_OVERWRITEPROMPT TO reptname    

END FUNCTION 

If you have any suggestions or corrections, please let me know.