Results 1 to 11 of 11

Thread: Help me get rid of the Common Dialog OCX!

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43

    Help me get rid of the Common Dialog OCX!

    I'm writing a program that up until now used the comdlg32.ocx control. All the program uses is the open, save and print dialogs. I want to get rid of the control so that my setup files won't be as big. I found the code to show the open and save dialogs using the common dialog dll that comes with windows, but what I need to know is how I can show the print dialog using a dll that comes with windows. I can't find the code anywhere (maybe it doesn't exist? ), but if someone could help me out, I could get rid of that ocx file. I can't just use the print commands in vb because I want to show the user the same print dialog that most programs use. If anyone knows how to do this, any help you could give me would be greatly appreciated. Thanks!

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Can you make your own form that looks exactly the same as the print dialog, then use the built in print functions?
    <removed by admin>

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    File open:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type OPENFILENAME
    4.     lStructSize As Long
    5.     hwndOwner As Long
    6.     hInstance As Long
    7.     lpstrFilter As String
    8.     lpstrCustomFilter As Long
    9.     nMaxCustrFilter As Long
    10.     nFilterIndex As Long
    11.     lpstrFile As String
    12.     nMaxFile As Long
    13.     lpstrFileTitle As String
    14.     nMaxFileTitle As Long
    15.     lpstrInitialDir As String
    16.     lpstrTitle As String
    17.     Flags As Long
    18.     nFileOffset As Integer
    19.     nFileExtension As Integer
    20.     lpstrDefExt As String
    21.     lCustrData As Long
    22.     lpfnHook As Long
    23.     lpTemplateName As Long
    24. End Type
    25.  
    26. Private Const OFN_HIDEREADONLY = &H4 'hide open as read only check box
    27. Private Const OFN_ALLOWMULTISELECT = &H200
    28. Private Const OFN_CREATEPROMPT = &H2000
    29. Private Const OFN_EXPLORER = &H80000
    30. Private Const OFN_FILEMUSTEXIST = &H1000
    31. Private Const OFN_NOCHANGEDIR = &H8
    32. Private Const OFN_NODEREFERENCELINKS = &H100000
    33. Private Const OFN_NONETWORKBUTTON = &H20000
    34. Private Const OFN_NOREADONLYRETURN = &H8000
    35. Private Const OFN_NOVALIDATE = &H100
    36. Private Const OFN_OVERWRITEPROMPT = &H2
    37. Private Const OFN_PATHMUSTEXIST = &H800
    38. Private Const OFN_READONLY = &H1
    39. Private Const OFN_SHOWHELP = &H10
    40.  
    41. Private Const ALLFILES = "All Files"
    42.  
    43. Private Declare Function GetActiveWindow Lib "user32" () As Long
    44. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    45.     "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
    46.    
    47. 'Purpose: Shows the open file common dialog
    48. 'Inputs: strTitle, the title of the dialog, pass "" for default
    49. 'Inputs: strDefaultPath, the default path
    50. 'Inputs: Filter, the filter, an even number of arguments e.g.("text files, *.txt, mdb files, *.mdb")
    51. 'Outputs: Returns the specified file name and path or "" if cancelled
    52. 'Author: Nucleus
    53.    
    54. Public Function ShowOpen(Optional strTitle As String, _
    55.     Optional strDefaultPath As String, Optional Filter As String) As String
    56.  
    57.     Dim OFName As OPENFILENAME
    58.     OFName.lStructSize = Len(OFName)
    59.     'Set the parent window
    60.     OFName.hwndOwner = GetActiveWindow
    61.     'Set the application's instance
    62.     OFName.hInstance = 0
    63.     'create a buffer for the file
    64.     OFName.lpstrFile = Space$(254)
    65.     'set the maximum length of a returned file
    66.     OFName.nMaxFile = 255
    67.     'Create a buffer for the file title
    68.     OFName.lpstrFileTitle = Space$(254)
    69.     'Set the maximum length of a returned file title
    70.     OFName.nMaxFileTitle = 255
    71.     'Set the title
    72.     OFName.lpstrTitle = strTitle
    73.     'No flags
    74.     OFName.Flags = OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST _
    75.  
    76.     If strDefaultPath = "" Or Len(Dir$(strDefaultPath)) Then
    77.         OFName.lpstrInitialDir = strDefaultPath
    78.     Else
    79.         OFName.lpstrInitialDir = CurDir$
    80.     End If
    81.    
    82.     OFName.lpstrFilter = CreateFilterString(Filter)
    83.    
    84.     'Call the Open dialog routine.
    85.     If GetOpenFileName(OFName) Then _
    86.     ShowOpen = Trim$(OFName.lpstrFile) Else ShowOpen = ""
    87.    
    88. End Function
    89.  
    90. Private Function CreateFilterString(Filter As String) As String
    91. ' Creates a filter string from the passed in arguments.
    92. ' Expects an even number of arguments
    93.     Dim sa() As String, i%
    94.     sa = Split(Filter, ",")
    95.    
    96.     For i = 0 To UBound(sa)
    97.         CreateFilterString = CreateFilterString & sa(i) & vbNullChar
    98.     Next i
    99.     CreateFilterString = CreateFilterString & "All Files (*.*)" & vbNullChar & "*.*"
    100. End Function

    usage:

    Private Sub Command1_Click()
    MsgBox ShowOpen()
    End Sub

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    File save:
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
    3. Private Declare Function GetActiveWindow Lib "user32" () As Long
    4. Private Type OPENFILENAME
    5.     lStructSize As Long
    6.     hwndOwner As Long
    7.     hInstance As Long
    8.     lpstrFilter As String
    9.     lpstrCustomFilter As String
    10.     nMaxCustFilter As Long
    11.     nFilterIndex As Long
    12.     lpstrFile As String
    13.     nMaxFile As Long
    14.     lpstrFileTitle As String
    15.     nMaxFileTitle As Long
    16.     lpstrInitialDir As String
    17.     lpstrTitle As String
    18.     flags As Long
    19.     nFileOffset As Integer
    20.     nFileExtension As Integer
    21.     lpstrDefExt As String
    22.     lCustData As Long
    23.     lpfnHook As Long
    24.     lpTemplateName As String
    25. End Type
    26.  
    27. Private Const OFN_HIDEREADONLY = &H4 'hide open as read only check box
    28. Private Const OFN_OVERWRITEPROMPT = &H2 'show message before overwriting existing files
    29. Private Const OFN_PATHMUSTEXIST = &H800 'refuse filenames with invalid paths
    30.  
    31.  
    32. 'Purpose     :  Shows the save file common dialog
    33. 'Inputs      :  strTitle                The title of the dialog, pass "" for default
    34. '               strDefaultPath          The default path
    35. '               Filter                  The filter, an even number of arguments
    36. '                                        e.g.("text files, *.txt, mdb files, *.mdb")
    37. 'Outputs     :  Returns the specified file name and path or "" if cancelled
    38. 'Author      :  Nucleus
    39.  
    40. Function ShowSave(Optional strTitle As String, Optional strDefaultPath As String, _
    41.         Optional Filter As String) As String
    42.     Const clBufferLen As Long = 255
    43.     Dim OFName As OPENFILENAME, sBuffer As String * 255
    44.     Dim strFilter As String: strFilter = ""
    45.     Dim intcount As Integer
    46.    
    47.     'Set title
    48.     OFName.lpstrTitle = strTitle
    49.    
    50.     'Set filter
    51.     OFName.lpstrFilter = CreateFilterString(Filter)
    52.    
    53.     'Set the initial directory
    54.     If strDefaultPath = "" Or Len(Dir$(strDefaultPath)) Then
    55.         OFName.lpstrInitialDir = strDefaultPath
    56.     Else
    57.         OFName.lpstrInitialDir = CurDir$
    58.     End If
    59.    
    60.     OFName.lStructSize = Len(OFName)
    61.     OFName.hwndOwner = GetActiveWindow  'or Me.hwnd in VB
    62.     OFName.hInstance = 0                'or App.hInstance in VB
    63.     OFName.lpstrFile = sBuffer
    64.     OFName.nMaxFile = clBufferLen       'Set max number of characters
    65.     OFName.lpstrFileTitle = sBuffer
    66.     OFName.nMaxFileTitle = clBufferLen  'Set max number of characters
    67.     OFName.flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
    68.  
    69.     'Show dialog
    70.     If GetSaveFileNameA(OFName) Then _
    71.     ShowSave = Trim$(OFName.lpstrFile) Else ShowSave = ""
    72.    
    73. End Function
    74.  
    75. Private Function CreateFilterString(Filter As String) As String
    76. ' Creates a filter string from the passed in arguments.
    77. ' Expects an even number of arguments
    78.     Dim sa() As String, i%
    79.     sa = Split(Filter, ",")
    80.    
    81.     For i = 0 To UBound(sa)
    82.         CreateFilterString = CreateFilterString & sa(i) & vbNullChar
    83.     Next i
    84.     CreateFilterString = CreateFilterString & "All Files (*.*)" & vbNullChar & "*.*"
    85. End Function

    Usage:

    'Usage
    Private Sub Command1_Click()
    MsgBox ShowSave(, "d:", "Text files , *.txt")
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43
    To MidgetsBro: I thought of that and I guess I could do it, but I won't for two reasons:

    A: It would be a lot of work that I'd rather not do if the code already exists in a dll file.

    B: After writing the new form and trying to make sure there were no bugs in it, I'd probably find that I'd made my setup files bigger than they were before because my program had gotten so much larger.

    Thanks for trying to help though.

    To Nucleus: I already found that code. What I need to know is if there's a way to do that for the print dialog box. Thanks anyway.

    If anyone else has any ideas or knows how to do this, please post a response!

  6. #6
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Well he beat me to the punch there. I was going to say that was the part you already knew . Anyhow, I'm not going to repost a long list of junk, so I'll give a link with an example. There's one in the API Guide, which means there is a version on the net. This is it.
    http://www.allapi.net/api/PrintDialog.php
    It goes to the main page of the function and there's a link for the example, and there are other links you may find useful (ones to all of the other common dialog parts).
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43
    Thanks Kaverin! You da man!

  8. #8
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Also check out here for a class that replaces the common dialog control completely:

    http://www.vbforums.com/showthread.p...=common+dialog

  9. #9
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    I tried... I failed... what's new? Sorry I couldn't help, you're not the first person or the last that I won't be able to help when I run my mouth. Oh well. I'm used to failing, it's part of my daily routine now.
    <removed by admin>

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43
    Thanks for the link to the great class module Nucleus! I was having trouble with Kaverin's code...it kept crashing for some reason ...

  11. #11
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    No problem, but thank Joacim Andersson not me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width