Author Topic: OpenFiledialog example  (Read 1942 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
OpenFiledialog example
« on: April 24, 2018, 09:17:45 AM »
Hello

i'm looking for an OpenFiledialog example code?  i searcehed and found that Aurel and Charles did one
back in 2012 ,  so is there any latest improvement on this sample code?

http://www.oxygenbasic.org/forum/index.php?topic=414.msg3075#msg3075

appreciate any help


Arnold

  • Guest
Re: OpenFiledialog example
« Reply #1 on: April 24, 2018, 09:39:18 AM »
In folder \inc there is FileDialog.inc with constants, structs and supporting functions.

In examples\wingui you will find:
DialogOpenFile1.o2bas
DialogOpenFile2.o2bas

If you have downloaded theForger_o2 examples, there are slightly modified File dialogs in app_two, app_three, app_four which can be adapted.

chrisc

  • Guest
Re: OpenFiledialog example
« Reply #2 on: April 24, 2018, 11:14:13 AM »
Thanxx a lot Roland

but i found that when i run the modified version of DialogOpenFile1 ( modified to have rtl64 only)
the filter isn't working

i preset the filter to display files that have exe and dll extension
but the dialog open display all the files  similar to *.*  ? 

why ?    what must be done so that the open dialog display *.exe and *.dll only

Code: [Select]
'  OpenFile1.o2bas
 $ filename "OpenFile1.exe"
    uses rtl64


#define OFN_READONLY                 0x00000001
#define OFN_OVERWRITEPROMPT          0x00000002
#define OFN_HIDEREADONLY             0x00000004
#define OFN_NOCHANGEDIR              0x00000008
#define OFN_SHOWHELP                 0x00000010
#define OFN_ENABLEHOOK               0x00000020
#define OFN_ENABLETEMPLATE           0x00000040
#define OFN_ENABLETEMPLATEHANDLE     0x00000080
#define OFN_NOVALIDATE               0x00000100
#define OFN_ALLOWMULTISELECT         0x00000200
#define OFN_EXTENSIONDIFFERENT       0x00000400
#define OFN_PATHMUSTEXIST            0x00000800
#define OFN_FILEMUSTEXIST            0x00001000
#define OFN_CREATEPROMPT             0x00002000
#define OFN_SHAREAWARE               0x00004000
#define OFN_NOREADONLYRETURN         0x00008000
#define OFN_NOTESTFILECREATE         0x00010000
#define OFN_NONETWORKBUTTON          0x00020000
#define OFN_NOLONGNAMES              0x00040000     // force no long names for 4.x modules
#define OFN_EXPLORER                 0x00080000     // new look commdlg
#define OFN_NODEREFERENCELINKS       0x00100000
#define OFN_LONGNAMES                0x00200000     // force long names for 3.x modules
#define OFN_ENABLEINCLUDENOTIFY      0x00400000     // send include message to callback
#define OFN_ENABLESIZING             0x00800000
#define OFN_DONTADDTORECENT          0x02000000
#define OFN_FORCESHOWHIDDEN          0x10000000    // Show All files including System and hidden files


/*
typedef struct tagOFNA {
   sys        lStructSize;
   HWND         hwndOwner;
   HINSTANCE    hInstance;
   LPCSTR       lpstrFilter;
   LPSTR        lpstrCustomFilter;
   DWORD        nMaxCustFilter;
   DWORD        nFilterIndex;
   LPSTR        lpstrFile;
   DWORD        nMaxFile;
   LPSTR        lpstrFileTitle;
   DWORD        nMaxFileTitle;
   LPCSTR       lpstrInitialDir;
   LPCSTR       lpstrTitle;
   DWORD        Flags;
   WORD         nFileOffset;
   WORD         nFileExtension;
   LPCSTR       lpstrDefExt;
   LPARAM       lCustData;
   LPOFNHOOKPROC lpfnHook;
   LPCSTR       lpTemplateName;
} OPENFILENAMEA, *LPOPENFILENAMEA;
*/

'  Apr 24 2018  -- modified to replace  handle and pointer
' from DWORD or LONG to sys otherwise
' when compile to 64bits it won't run

Type OPENFILENAME
    lStructSize As sys
    hwndOwner As sys
    hInstance As sys
    lpstrFilter as sys 'string
    lpstrCustomFilter As sys
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As sys 'string
    nMaxFile As Long
    lpstrFileTitle As sys 'string
    nMaxFileTitle As Long
    lpstrInitialDir As sys 'string
    lpstrTitle As sys 'string
    flags As Long
    nFileOffset As word
    nFileExtension As word
    lpstrDefExt As sys 'string
    lCustData As Long
    lpfnHook As sys
    lpTemplateName As sys 'string
End Type
'76 bytes


Declare GetModuleHandle  lib "kernel32.dll" alias "GetModuleHandleA" (sys n) as sys

Declare Function GetOpenFileName  Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As sys

Declare CommDlgExtendedError Lib "comdlg32.dll" () as dword



'==================
Function FileDialog(string iDir, filter, Title, sys Hwnd, Flags) As String


Dim ofn As OPENFILENAME
Dim filename[255] As zstring
int retval


ofn.lStructSize = sizeof(OPENFILENAME)
ofn.hwndOwner = hWnd
ofn.hInstance = GetModuleHandle(0)
ofn.lpstrFilter = ?filter
ofn.lpstrCustomFilter= NULL
ofn.nMaxCustFilter = 0
ofn.nFilterIndex = 2
ofn.lpstrFile = @filename       'zstring buffer
ofn.nMaxFile = 255
ofn.lpstrFileTitle = NULL
ofn.nMaxFileTitle = 0
ofn.lpstrInitialDir = ?idir
ofn.lpstrTitle = ?title
ofn.Flags = OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or OFN_HIDEREADONLY
ofn.nFileOffset = 0
ofn.nFileExtension = 0
ofn.lpstrDefExt = NULL
ofn.lCustData = 0
ofn.lpfnHook = 0
ofn.lpTemplateName = NULL

'
retval = GetOpenFileName(ofn)
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms646916(v=vs.85).aspx
'if retval=0 then print "Dialog Error " CommDlgExtendedError
return filename
'
End Function


'======================
' Main

' opening folder
dir="C:\Temp"

' Separator for the file filter
sep=chr(0)


filter= "*.exe;*.dll"

   '    "text"+sep+"*.txt"+sep+
   '    "basic"+sep+"*.bas;*.o2bas"+sep+
  '     "include"+sep+"*.inc"+sep+
   '    "header"+sep+"*.h"+sep+
 '      sep


title="File Browser"
hwnd=0
f=FileDialog(dir,filter,title,hwnd,OFN_EXPLORER or OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY)
if f then print f




José Roca

  • Guest
Re: OpenFiledialog example
« Reply #3 on: April 24, 2018, 11:23:05 AM »
Why do you use SYS in the lStructSize member of the structure if it must be a DWORD?

chrisc

  • Guest
Re: OpenFiledialog example
« Reply #4 on: April 24, 2018, 12:00:59 PM »
Thanxx Jose

i'm use to changing all the DWORD and LONG to sys when compiling to 64bits
so this is my bad.

but changing back, the filter still did not filter off to *.exe  and *.dll ?


José Roca

  • Guest
Re: OpenFiledialog example
« Reply #5 on: April 24, 2018, 12:12:31 PM »
I'm nor suprised, since you're using ";" as the separator instead of chr(0).

Arnold

  • Guest
Re: OpenFiledialog example
« Reply #6 on: April 24, 2018, 01:19:22 PM »
You can apply:

filter= "Exe, Dll" + sep +" *.exe;*.dll" + sep + sep

But I personally would save some work and use FileDialog.inc directly.

chrisc

  • Guest
Re: OpenFiledialog example
« Reply #7 on: April 24, 2018, 02:18:16 PM »
Thanxx a lot  Jose and Roland
that resolved the issue