PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: AfxOpenFileDialogW  (Read 4244 times)

Josť Roca

  • Guru Member
  • *****
  • Posts: 3282
Re: AfxOpenFileDialogW
« Reply #15 on: June 15, 2016, 12:55:03 PM »

And then another for the IFileSaveDialog, with these additional methods:

Code: [Select]
ApplyProperties
Applies a set of properties to an item using the Shell's copy engine.

GetOptions
Gets the current flags that are set to control dialog behavior.

GetProperties
Retrieves the set of property values for a saved item or an item in the process of being saved.

SetCollectedProperties
Specifies which properties will be collected in the save dialog.

SetOptions
Sets flags to control the behavior of the dialog.

SetProperties
Provides a property store that defines the default values to be used for the item being saved.

SetSaveAsItem
Sets an item to be used as the initial entry in a Save As dialog.

I don't have plans to write FFC (FreeBASIC Foundation Classes).

Josť Roca

  • Guru Member
  • *****
  • Posts: 3282
Re: AfxOpenFileDialogW
« Reply #16 on: June 15, 2016, 01:12:03 PM »

> I'm afraid my Fb and COM are lacking (and no COM SaveAs example) to attempt this myself.

The save file dialog is very similar to the one for the single selection open file dialog.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: COM_IFileSaveDialog.fbtpl
' Contents: Demonstrates the use of the IFileSaveDialog interface.
' Compiler: FreeBasic 32 & 64 bit
' Copyright (c) 2016 Jose 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 _WIN32_WINNT &h0602
#INCLUDE ONCE "win/shobjidl.bi"
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx.CWindowClass

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)

' ========================================================================================
' Displays the FileSaveDialog
' The returned pointer must be freed with CoTaskMemFree
' ========================================================================================
FUNCTION AfxIFileSaveDialog (BYVAL hwndOwner AS HWND, BYVAL pwszFileName AS WSTRING PTR, _
BYVAL pwszDefExt AS WSTRING PTR, BYVAL sigdnName AS SIGDN = SIGDN_FILESYSPATH) AS WSTRING PTR

   ' // Create an instance of the IFileSaveDialog interface
   DIM hr AS LONG
   DIM psfd AS IFileSaveDialog PTR
   hr = CoCreateInstance(@CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, @IID_IFileSaveDialog, @psfd)
   IF psfd = NULL THEN RETURN NULL

   ' // Set the file types
   DIM rgFileTypes(1 TO 3) AS COMDLG_FILTERSPEC
   rgFileTypes(1).pszName = @WSTR("FB code files")
   rgFileTypes(2).pszName = @WSTR("Text files")
   rgFileTypes(3).pszName = @WSTR("All files")
   rgFileTypes(1).pszSpec = @WSTR("*.bas;*.inc;*.bi")
   rgFileTypes(2).pszSpec = @WSTR("*.txt")
   rgFileTypes(3).pszSpec = @WSTR("*.*")
   psfd->lpVtbl->SetFileTypes(psfd, 3, @rgFileTypes(1))

   ' // Set the title of the dialog
   hr = psfd->lpVtbl->SetTitle(psfd, "File Save Dialog")
   ' // Set the file name
   hr = psfd->lpVtbl->SetFileName(psfd, pwszFileName)
   ' // Set the extension
   hr = psfd->lpVtbl->SetDefaultExtension(psfd, pwszDefExt)
   ' // Display the dialog
   hr = psfd->lpVtbl->Show(psfd, hwndOwner)

   ' // Set the default folder
   DIM pFolder AS IShellItem PTR
   SHCreateItemFromParsingName (CURDIR, NULL, @IID_IShellItem, @pFolder)
   IF pFolder THEN
      psfd->lpVtbl->SetDefaultFolder(psfd, pFolder)
      pFolder->lpVtbl->Release(pFolder)
   END IF

   ' // Get the result
   DIM pItem AS IShellItem PTR
   DIM pwszName AS WSTRING PTR
   IF SUCCEEDED(hr) THEN
      hr = psfd->lpVtbl->GetResult(psfd, @pItem)
      IF SUCCEEDED(hr) THEN
         hr = pItem->lpVtbl->GetDisplayName(pItem, sigdnName, @pwszName)
         FUNCTION = pwszName
      END IF
   END IF

   ' // Cleanup
   IF pItem THEN pItem->lpVtbl->Release(pItem)
   IF psfd THEN psfd->lpVtbl->Release(psfd)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Window 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_CREATE
         EXIT FUNCTION

      CASE WM_COMMAND
         SELECT CASE LOWORD(wParam)
            ' // If ESC key pressed, close the application sending an WM_CLOSE message
            CASE IDCANCEL
               IF HIWORD(wParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
            CASE IDC_OFD
               IF HIWORD(wParam) = BN_CLICKED THEN
                  ' // Display the Save File Dialog
                  DIM pwszName AS WSTRING PTR = AfxIFileSaveDialog(hwnd, "test", "bas")
                  ' // Display the selected name
                  IF pwszName THEN
                     MessageBoxW(hwnd, *pwszName, "IFileSaveDialog", MB_OK)
                     CoTaskMemFree(pwszName)
                  END IF
                  EXIT FUNCTION
               END IF
         END SELECT

    CASE WM_DESTROY
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT

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

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Initialize the COM library
   CoInitialize(NULL)

   ' // Set process DPI aware
   AfxSetProcessDPIAware

   DIM pWindow AS CWindow
   pWindow.Create(NULL, "IFileOpenDialog example", @WndProc)
   pWindow.SetClientSize(500, 320)
   pWindow.Center

   ' // Add a button
   pWindow.AddControl("Button", , IDC_OFD, "File &Save Dialog", 350, 250, 110, 23)

   

   ' // Process event messages
   FUNCTION = pWindow.DoEvents(nCmdShow)

   ' // Uninitialize the COM library
   CoUninitialize

END FUNCTION
' ========================================================================================
« Last Edit: June 15, 2016, 01:27:59 PM by Jose Roca »
Logged

Josť Roca

  • Guru Member
  • *****
  • Posts: 3282
Re: AfxOpenFileDialogW
« Reply #17 on: June 15, 2016, 01:15:41 PM »

The major problem for low-level COM programming with FB is the lack of native support for BSTRs. Without it, any attempt to make it easier to use is futile.

Josť Roca

  • Guru Member
  • *****
  • Posts: 3282
Re: AfxOpenFileDialogW
« Reply #18 on: June 15, 2016, 01:34:25 PM »

And then the first question posted will be: "Is there an easy way to center the dialog?" :)
Pages: 1 [2]