导航:  Windows Procedures >

AfxOpenFileDialog

上一页返回章节概述下一页

描述

 

创建一个Open对话框,让用户指定的驱动器、目录和文件的名称或文件集被打开.对话框使用资源管理器风格的用户界面.

 

FreeBASIC 语法

 

FUNCTION AfxOpenFileDialog ( _

  BYVAL hwndOwner AS HWND, _

  BYREF wszTitle AS WSTRING, _

  BYREF wszFile AS WSTRING, _

  BYREF wszInitialDir AS WSTRING, _

  BYREF wszFilter AS WSTRING, _

  BYREF wszDefExt AS WSTRING, _

  BYVAL pdwFlags AS DWORD PTR = NULL, _

  BYVAL pdwBufLen AS DWORD PTR = NULL _

) AS CWSTR

 

参数

 

hwndOwner

 

[in]窗口的句柄拥有对话框.这个成员可以是任何有效的窗口句柄,或者它可以NULL如果对话框没有主人.

 

wszTitle

 

[in]字符串放在对话框的标题栏.如果这件NULL,系统将使用默认的标题(即Open).

 

wszFile

 

[in]文件名用于初始化File Name编辑控件.

 

wszInitialDir

 

[in]初始目录.如果没有指定初始目录,对话框将使用当前目录.

 

wszFilter

 

[in]缓冲含有对"|"分隔的字符串.每对中的第一个字符串是一个显示字符串描述滤波器(例如,'文本文件'),和第二字符串指定过滤模式(例如,"*.TXT").为单一显示字符串,指定多个过滤器模式使用分号分隔的模式(例如,"*.TXT;*.DOC;*.BAK").模式字符串可以是一种有效的文件名字符和通配符星号(*).不包括模式字符串中的空格.

系统不改变过滤器的顺序.它显示在指定的wszFilter秩序File Types组合框.

 

wszDefExt

 

[in]的默认扩展名.如果用户未能键入扩展名,则将此扩展名附加到文件名.此字符串可以是任意长度,但仅附加前三个字符.该字符串不应包含一段(.).如果这件NULL和用户不能类型的扩展,没有扩展附加.

 

dwFlags

 

[in, out]一位标志集合,您可以使用初始化对话框.当对话框返回时,它会设置这些标志来表示用户的输入.此成员可以是下列标志的组合.

OFN_ALLOWMULTISELECT (&H00000200)

The File Name list box allows multiple selections.

 

OFN_DONTADDTORECENT (&H02000000)

Prevents the system from adding a link to the selected file in the file system directory that contains the user's most recently used documents. To retrieve the location of this directory, call the SHGetSpecialFolderLocation function with the CSIDL_RECENT flag.

OFN_EXTENSIONDIFFERENT (&H00000400)

The user typed a file name extension that differs from the extension specified by wszDefExt. The function does not use this flag if wszDefExt is NULL.

 

OFN_FILEMUSTEXIST (&H00001000)

The user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used.

OFN_FORCESHOWHIDDEN (&H10000000)

Forces the showing of system and hidden files, thus overriding the user setting to show or not show hidden files. However, a file that is marked both system and hidden is not shown.

OFN_HIDEREADONLY (&H00000004)

Hides the Read Only check box.

OFN_NODEREFERENCELINKS (&H00100000)

Directs the dialog box to return the path and file name of the selected shortcut (.LNK) file. If this value is not specified, the dialog box returns the path and file name of the file referenced by the shortcut.

OFN_NONETWORKBUTTON (&H00020000)

Hides and disables the Network button.

OFN_NOREADONLYRETURN (&H00008000)

The returned file does not have the Read Only check box selected and is not in a write-protected directory.

OFN_PATHMUSTEXIST (&H00000800)

The user can type only valid paths and file names. If this flag is used and the user types an invalid path and file name in the File Name entry field, the dialog box function displays a warning in a message box.

OFN_READONLY (&H00000001)

Causes the Read Only check box to be selected initially when the dialog box is created. This flag indicates the state of the Read Only check box when the dialog box is closed.

OFN_SHOWHELP (&H00000010)

Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button.

 

dwBufLen

 

返回的字符串包含选定的文件或文件[in, opt]最大长度.

 

返回值

 

如果OFN_ALLOWMULTISELECT标志设置和用户选择多个文件,返回的字符串包含当前目录,其次是选定的文件的文件名.资源管理器风格对话框,目录和文件名的字符串是用分号分隔.如果用户只选择一个文件,返回的字符串在路径和文件名之间没有分隔符.

解析",".数量只有一个,那么用户只选择了一个文件和字符串包含完整路径.如果,第一个子字符串包含路径和其他文件.如果用户没有选择任何文件,则返回一个空字符串.失败,返回一个空字符串,如果不为空,这pdwBufLen参数将由所需的缓冲区大小填充字符.

 

引用文件

 

AfxWin.inc

 

示例 (single file selection)

 

' ########################################################################################

' Microsoft Windows

' Contents: Demonstrates the use of the AfxOpenFileDialog function (single selection).

' Compiler: FreeBasic 32 & 64 bit

' Copyright (c) 2016 José Roca. Freeware. Use at your own risk.

' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER

' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF

' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

' ########################################################################################

 

#define UNICODE

#INCLUDE ONCE "Afx/CWindow.inc"

USING Afx

 

CONST IDC_OFD = 1001

 

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                        BYVAL hPrevInstance AS HINSTANCE, _

                        BYVAL szCmdLine AS ZSTRING PTR, _

                        BYVAL nCmdShow AS LONG) AS LONG

 

 END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

 

' // 提前声明

DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

' ========================================================================================

' Main

' ========================================================================================

FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                BYVAL hPrevInstance AS HINSTANCE, _

                BYVAL szCmdLine AS ZSTRING PTR, _

                BYVAL nCmdShow AS LONG) AS LONG

 

 ' // 设置处理DPI感知

 AfxSetProcessDPIAware

 

 ' // Create the main window

 DIM pWindow AS CWindow

 pWindow.Create(NULL, "AfxOpenFileDialog example", @WndProc)

 pWindow.SetClientSize(500, 320)

 pWindow.Center

 

 ' // Add a button

 pWindow.AddControl("Button", , IDC_OFD, "&Open File Dialog", 350, 250, 110, 23)

 

 ' // Dispatch messages

 FUNCTION = pWindow.DoEvents(nCmdShow)

 

 ' // Uninitialize the COM library

 CoUninitialize

 

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Main window callback procedure

' ========================================================================================

FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

 SELECT CASE uMsg

 

    CASE WM_COMMAND

       SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)

          ' // If ESC key pressed, close the application sending an WM_CLOSE message

          CASE IDCANCEL

             IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN

                SendMessageW hwnd, WM_CLOSE, 0, 0

                EXIT FUNCTION

             END IF

          ' // Display the Open File Dialog

          CASE IDC_OFD

             IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN

                DIM wszFile AS WSTRING * 260 = "*.*"

                DIM wszInitialDir AS STRING * 260 = CURDIR

                DIM wszFilter AS WSTRING * 260 = "BAS files (*.BAS)|*.BAS|" & "All Files (*.*)|*.*|"

                DIM dwFlags AS DWORD = OFN_EXPLORER OR OFN_FILEMUSTEXIST OR OFN_HIDEREADONLY

                DIM cws AS CWSTR = AfxOpenFileDialog(hwnd, "", wszFile, wszInitialDir, wszFilter, "BAS", @dwFlags, NULL)

                MessageBoxW(hwnd, cws, "File", MB_OK)

                EXIT FUNCTION

             END IF

       END SELECT

 

         CASE WM_DESTROY

       ' // Quit the application

       PostQuitMessage(0)

       EXIT FUNCTION

 

 END SELECT

 

 ' // Default processing of Windows messages

 FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

 

END FUNCTION

' ========================================================================================

 

示例 (multiple file selection)

 

' ########################################################################################

' Microsoft Windows

' Contents: Demonstrates the use of the AfxOpenFileDialog function (multiple file selection).

' Compiler: FreeBasic 32 & 64 bit

' Copyright (c) 2016 José Roca. Freeware. Use at your own risk.

' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER

' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF

' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

' ########################################################################################

 

#define UNICODE

#INCLUDE ONCE "Afx/CWindow.inc"

USING Afx

 

CONST IDC_OFD = 1001

 

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                        BYVAL hPrevInstance AS HINSTANCE, _

                        BYVAL szCmdLine AS ZSTRING PTR, _

                        BYVAL nCmdShow AS LONG) AS LONG

 

 END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

 

' // 提前声明

DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

' ========================================================================================

' Main

' ========================================================================================

FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                BYVAL hPrevInstance AS HINSTANCE, _

                BYVAL szCmdLine AS ZSTRING PTR, _

                BYVAL nCmdShow AS LONG) AS LONG

 

 ' // 设置处理DPI感知

 AfxSetProcessDPIAware

 

 ' // Create the main window

 DIM pWindow AS CWindow

 pWindow.Create(NULL, "AfxOpenFileDialog example", @WndProc)

 pWindow.SetClientSize(500, 320)

 pWindow.Center

 

 ' // Add a button

 pWindow.AddControl("Button", , IDC_OFD, "&Open File Dialog", 350, 250, 110, 23)

 

 ' // Dispatch messages

 FUNCTION = pWindow.DoEvents(nCmdShow)

 

 ' // Uninitialize the COM library

 CoUninitialize

 

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Main window callback procedure

' ========================================================================================

FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

 SELECT CASE uMsg

 

    CASE WM_COMMAND

       SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)

          ' // If ESC key pressed, close the application sending an WM_CLOSE message

          CASE IDCANCEL

             IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN

                SendMessageW hwnd, WM_CLOSE, 0, 0

                EXIT FUNCTION

             END IF

          ' // Display the Open File Dialog

          CASE IDC_OFD

             IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN

                DIM wszFile AS WSTRING * 260 = "*.*"

                DIM wszInitialDir AS STRING * 260 = CURDIR

                DIM wszFilter AS WSTRING * 260 = "BAS files (*.BAS)|*.BAS|" & "All Files (*.*)|*.*|"

                DIM dwFlags AS DWORD = OFN_EXPLORER OR OFN_FILEMUSTEXIST OR OFN_HIDEREADONLY OR OFN_ALLOWMULTISELECT

                DIM cws AS CWSTR = AfxOpenFileDialog(hwnd, "", wszFile, wszInitialDir, wszFilter, "BAS", @dwFlags, NULL)

                IF LEN(cws) THEN

                   DIM nItems AS LONG = AfxStrParseCount(cws)

                   IF nItems THEN

                      DIM cwsPath AS CWSTR = AfxStrParse(cws, 1)

                      MessageBoxW(hwnd, cwsPath, "Path", MB_OK)

                      FOR i AS LONG = 2 TO nItems

                         MessageBoxW(hwnd, AfxStrParse(cws, i), "File", MB_OK)

                      NEXT

                   END IF

                END IF

                EXIT FUNCTION

             END IF

       END SELECT

 

         CASE WM_DESTROY

       ' // Quit the application

       PostQuitMessage(0)

       EXIT FUNCTION

 

 END SELECT

 

 ' // Default processing of Windows messages

 FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

 

END FUNCTION

' ========================================================================================