Solved

Change button caption/lable in common dialog

Posted on 2000-03-16
7
490 Views
Last Modified: 2008-02-20
Is it possible to rename i.e. the Open button to "Insert" in a common dialog control ?
0
Comment
Question by:molletor
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:hkshijun
ID: 2626782
Maybe you can create a new allication with similiar form :-)
0
 

Expert Comment

by:hkshijun
ID: 2626784
Maybe you can create a new allication with similiar form :-)
0
 
LVL 27

Accepted Solution

by:
Ark earned 300 total points
ID: 2626898
Hi
Here it is
Module Code
'-------------
Option Explicit
Private Const MAX_PATH = 260
Private Const MAX_FILE = 260

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As Long
End Type

Private Declare Function GetOpenFileName Lib "COMDLG32" Alias "GetOpenFileNameA" (file As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "COMDLG32" Alias "GetSaveFileNameA" (file As OPENFILENAME) As Long

Public Enum EOpenFile
    OFN_READONLY = &H1
    OFN_OVERWRITEPROMPT = &H2
    OFN_HIDEREADONLY = &H4
    OFN_NOCHANGEDIR = &H8
    OFN_SHOWHELP = &H10
    OFN_ENABLEHOOK = &H20
    OFN_ENABLETEMPLATE = &H40
    OFN_ENABLETEMPLATEHANDLE = &H80
    OFN_NOVALIDATE = &H100
    OFN_ALLOWMULTISELECT = &H200
    OFN_EXTENSIONDIFFERENT = &H400
    OFN_PATHMUSTEXIST = &H800
    OFN_FILEMUSTEXIST = &H1000
    OFN_CREATEPROMPT = &H2000
    OFN_SHAREAWARE = &H4000
    OFN_NOREADONLYRETURN = &H8000
    OFN_NOTESTFILECREATE = &H10000
    OFN_NONETWORKBUTTON = &H20000
    OFN_NOLONGNAMES = &H40000
    OFN_EXPLORER = &H80000
    OFN_NODEREFERENCELINKS = &H100000
    OFN_LONGNAMES = &H200000
End Enum
Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type


' Messages:
Private Const WM_DESTROY = &H2
Private Const WM_NOTIFY = &H4E
Private Const WM_NCDESTROY = &H82
Private Const WM_GETDLGCODE = &H87
Private Const WM_INITDIALOG = &H110
Private Const WM_COMMAND = &H111
Private Const WM_USER = &H400


' Messages to dialog controls
Private Const CDM_FIRST = (WM_USER + 100)
Private Const CDM_LAST = (WM_USER + 200)

Private Const CDM_GETSPEC = (CDM_FIRST + &H0)
Private Const CDM_GETFILEPATH = (CDM_FIRST + &H1)
Private Const CDM_GETFOLDERPATH = (CDM_FIRST + &H2)
Private Const CDM_GETFOLDERIDLIST = (CDM_FIRST + &H3)
Private Const CDM_SETCONTROLTEXT = (CDM_FIRST + &H4)
Private Const CDM_HIDECONTROL = (CDM_FIRST + &H5)
Private Const CDM_SETDEFEXT = (CDM_FIRST + &H6)

' Dialog controls IDs
Private Const ID_OPEN = &H1  'Open or Save button
Private Const ID_CANCEL = &H2 'Cancel Button
Private Const ID_HELP = &H40E 'Help Button
Private Const ID_READONLY = &H410 'Read-only check box
Private Const ID_FILETYPELABEL = &H441 'FileType label
Private Const ID_FILELABEL = &H442 'FileName label
Private Const ID_FOLDERLABEL = &H443 'Folder label
Private Const ID_FORMAT = &H470 'FileType combo box
Private Const ID_FOLDER = &H471 'Folder combo box
Private Const ID_FILETEXT = &H480 'FileName text box

Private Const SW_NORMAL = 1

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Dim OFN As OPENFILENAME
Dim rct As RECT
Public hDlg As Long
Public DlgFlags As EOpenFile
Public DlgLeft As Long, DlgTop As Long
Public DlgOpenVisible As Boolean, DlgCancelVisible As Boolean
Public DlgOpenText As String, DlgCancelText As String

Private Function lHookAddress(lPtr As Long) As Long
  lHookAddress = lPtr
End Function

Public Function DialogHookFunction(ByVal hDlg As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Select Case wMsg
          Case WM_INITDIALOG
               MoveDlg hDlg
               ModifyDlg hDlg
          Case Else
   End Select
End Function
Public Sub InitOFN(Optional ByVal hOwner As Long)
   With OFN
       .hwndOwner = hOwner
       .lpfnHook = lHookAddress(AddressOf DialogHookFunction)
       .flags = DlgFlags
       .lStructSize = LenB(OFN)
   End With
End Sub

Public Sub ShowSave()
   hDlg = GetOpenFileName(OFN)
End Sub

Public Sub ShowOpen()
   hDlg = GetOpenFileName(OFN)
End Sub

Private Sub MoveDlg(hDlg)
  Dim DlgHwnd As Long
  DlgHwnd = GetParent(hDlg)
  'DlgHwnd = hdlg 'When using with other then Open/Save Dialogs
  GetWindowRect DlgHwnd, rct
  MoveWindow DlgHwnd, DlgLeft, DlgTop, rct.Right - rct.Left, rct.Bottom - rct.Top, 1
End Sub

Private Sub ModifyDlg(hDlg)
  Dim DlgHwnd As Long
  DlgHwnd = GetParent(hDlg)
  SendMessage DlgHwnd, CDM_SETCONTROLTEXT, ID_OPEN, ByVal DlgOpenText
  SendMessage DlgHwnd, CDM_SETCONTROLTEXT, ID_CANCEL, ByVal DlgCancelText
  SendMessage DlgHwnd, CDM_HIDECONTROL, ID_FILETEXT, 0&
End Sub
'using - Form Code
Private Sub Form_Load()
   DlgLeft = 100
   DlgTop = 100
   DlgOpenText = "Where is file?"
   DlgCancelText = "Exit :-(?"
   DlgFlags = OFN_EXPLORER Or OFN_ENABLEHOOK
   InitOFN ' Me.hwnd
End Sub

Private Sub Command1_Click()
   ShowOpen
End Sub

Some more stuff - positioning of common dialog
Cheers

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Ark
ID: 2627342
Hi
Above code show the way of subclassing dlg and change its interface. But it doesn't have all CommonDialog properties and not return FileName. Ok, here is full code. I make a class so you can call it the same way as CommonDialog:

'Bas Module (only for AddressOf operator)
Option Explicit
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Const WM_INITDIALOG = &H110
Private Const WM_USER = &H400

Private Const CDM_FIRST = (WM_USER + 100)
Private Const CDM_SETCONTROLTEXT = (CDM_FIRST + &H4)
Private Const CDM_HIDECONTROL = (CDM_FIRST + &H5)
Private Const ID_OPEN = &H1  'Open or Save button
Private Const ID_CANCEL = &H2 'Cancel Button
Private Const ID_HELP = &H40E 'Help Button
Private Const SW_NORMAL = 1

Public DlgLeft As Long
Public DlgTop As Long
Public OpenText As String
Public CancelText As String

Public Function lHookAddress(lPtr As Long) As Long
  lHookAddress = lPtr
End Function

Public Function DialogHookFunction(ByVal hDlg As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Select Case wMsg
          Case WM_INITDIALOG
               MoveDlg hDlg
               ModifyDlg hDlg
          Case Else
   End Select
End Function

Private Sub MoveDlg(hDlg)
  Dim DlgHwnd As Long
  Dim rct As RECT
  DlgHwnd = GetParent(hDlg)
  GetWindowRect DlgHwnd, rct
  MoveWindow DlgHwnd, DlgLeft, DlgTop, rct.Right - rct.Left, rct.Bottom - rct.Top, 1
End Sub

Private Sub ModifyDlg(hDlg)
  Dim DlgHwnd As Long
  DlgHwnd = GetParent(hDlg)
  SendMessage DlgHwnd, CDM_SETCONTROLTEXT, ID_OPEN, ByVal OpenText
  SendMessage DlgHwnd, CDM_SETCONTROLTEXT, ID_CANCEL, ByVal CancelText
End Sub

'Class module - incapsulate all CommonDialog properties and methods for Open and save Dialogs. If you need all common dialog methods (font, color etc.) and some more - take a look at http://www.freevbcode.com/ShowCode.Asp?ID=631
Option Explicit

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

' ------------- Dialog calling functions
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

' --------------- Enum Flags
Public Enum CdlgExt_Flags
  OFNAllowMultiselect = &H200
  OFNCreatePrompt = &H2000
  OFNexplorer = &H80000
  OFNEnableHook = &H20
  OFNExtensionDifferent = &H400
  OFNFileMustExist = &H1000
  OFNHelpButton = &H10
  OFNHideReadOnly = &H4
  OFNLongNames = &H200000
  OFNNoChangeDir = &H8
  OFNNoDereferenceLinks = &H100000
  OFNNoLongNames = &H40000
  OFNNoReadOnlyReturn = &H8000
  OFNNoValidate = &H100
  OFNOverwritePrompt = &H2
  OFNPathMustExist = &H800
  OFNReadOnly = &H1
  OFNShareAware = &H4000
End Enum

Private RetValue As Long
Private OFN As OPENFILENAME

Private Const MAX_PATH = 260
Private Const MAX_FILE = 260

'Inner variables for properties
Private mFileName As String
Private mFileTitle As String
Private mhOwner As Long
Private mDialogTitle As String
Private mFilter As String
Private mInitDir As String
Private mSelDir As String
Private mDefaultExt As String
Private mFilterIndex As Long
Private mFlags As CdlgExt_Flags
Private mCancelError As Boolean

' Let/Get Properties
Public Property Let CancelError(ByVal vData As Boolean)
   mCancelError = vData
End Property

Public Property Get CancelError() As Boolean
  CancelError = mCancelError
End Property

Public Property Get hOwner() As Long
    hOwner = mhOwner
End Property

Public Property Let hOwner(ByVal New_hOwner As Long)
    mhOwner = New_hOwner
End Property

Public Property Get flags() As CdlgExt_Flags
    flags = mFlags
End Property

Public Property Let flags(ByVal New_Flags As CdlgExt_Flags)
    mFlags = New_Flags
End Property

Public Property Get DialogTitle() As String
   DialogTitle = mDialogTitle
End Property

Public Property Let DialogTitle(sTitle As String)
   mDialogTitle = sTitle
End Property

Public Property Get DefaultExt() As String
   DefaultExt = mDefaultExt
End Property

Public Property Let DefaultExt(sDefExt As String)
   mDefaultExt = DefaultExt
End Property

Public Property Get FileName() As String
   FileName = mFileName
End Property

Public Property Let FileName(sFileName As String)
   mFileName = sFileName
End Property

Public Property Get FileTitle() As String
   FileTitle = mFileTitle
End Property

Public Property Let FileTitle(sTitle As String)
   mFileTitle = sTitle
End Property

Public Property Get Filter() As String
   Filter = mFilter
End Property

Public Property Let Filter(sFilter As String)
   mFilter = sFilter
End Property

Public Property Get FilterIndex() As Long
   FilterIndex = mFilterIndex
End Property

Public Property Let FilterIndex(lIndex As Long)
    mFilterIndex = lIndex
End Property

Public Property Get InitDir() As String
   InitDir = mInitDir
End Property

Public Property Let InitDir(sDir As String)
    mInitDir = sDir
End Property

' Standard  Dialogs
Public Sub ShowOpen()
  Dim iDelim As Integer
  InitOFN
  RetValue = GetOpenFileName(OFN)
  If RetValue > 0 Then
     iDelim = InStr(OFN.lpstrFileTitle, vbNullChar)
     If iDelim Then mFileTitle = Left$(OFN.lpstrFileTitle, iDelim - 1)
     iDelim = InStr(OFN.lpstrFile, vbNullChar)
     If iDelim Then mFileName = Left$(OFN.lpstrFile, iDelim - 1)
  Else
     If mCancelError Then Err.Raise 0
  End If
End Sub

Public Sub ShowSave()
  Dim iDelim As Integer
  InitOFN
  RetValue = GetSaveFileName(OFN)
  If RetValue > 0 Then
     iDelim = InStr(OFN.lpstrFileTitle, vbNullChar)
     If iDelim Then mFileTitle = Left$(OFN.lpstrFileTitle, iDelim - 1)
     iDelim = InStr(OFN.lpstrFile, vbNullChar)
     If iDelim Then mFileName = Left$(OFN.lpstrFile, iDelim - 1)
  Else
     If mCancelError Then Err.Raise 0
  End If
End Sub

Private Sub InitOFN()
  Dim sTemp As String, i As Integer
  Dim uFlag As Long
  uFlag = mFlags Or OFNEnableHook Or OFNexplorer
  With OFN
       .lStructSize = Len(OFN)
       .hwndOwner = mhOwner
       .lpfnHook = lHookAddress(AddressOf DialogHookFunction)
       .flags = uFlag
       .lpstrDefExt = mDefaultExt
       sTemp = mInitDir
       If sTemp = "" Then sTemp = App.Path
       .lpstrInitialDir = sTemp
       sTemp = mFileName
       .lpstrFile = sTemp & String$(MAX_PATH - Len(sTemp), 0)
       .nMaxFile = MAX_FILE
       .lpstrFileTitle = String$(MAX_PATH, 0)
       .nMaxFileTitle = MAX_FILE
        sTemp = mFilter
        For i = 1 To Len(sTemp)
            If Mid(sTemp, i, 1) = "|" Then
               Mid(sTemp, i, 1) = vbNullChar
            End If
        Next
        sTemp = sTemp & String$(2, 0)
        .lpstrFilter = sTemp
        .nFilterIndex = mFilterIndex
        .lpstrTitle = mDialogTitle
        .hInstance = App.hInstance
 End With
End Sub
' An finally, how to use - Form Code

Dim CDlg As New CdlgEx

Private Sub Command1_Click()
  DlgLeft = 100
  DlgTop = 100
  OpenText = "Insert"
  CancelText = "No Insert?"
  CDlg.CancelError = True
  CDlg.Filter = "Text files|*.txt|All files|*.*"
  CDlg.ShowOpen
  Text1 = CDlg.FileName
End Sub
Cheers
0
 

Author Comment

by:molletor
ID: 2652218
Adjusted points from 200 to 300
0
 

Author Comment

by:molletor
ID: 2652222
Excellent code Ark ! - it just works

Because of my late reply I have added yet another 100 points for your answer

Thanks
0
 
LVL 27

Expert Comment

by:Ark
ID: 2652328
Hi
Glad to help you!
BTW, using bas module, you can change/hide any control at common dialog, not only Open button. For example, you can hide Cancel button to force user choose smth.
Cheers
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now