Dec 222017
 
Using common dialogs from Visual Basic.
File CMDLG2.ZIP from The Programmer’s Corner in
Category BASIC Language
Using common dialogs from Visual Basic.
File Name File Size Zip Size Zip Type
CMDIALOG.BAS 11418 4469 deflated
DIALOG.TXT 2831 1149 deflated
DIALOGS.BAS 10218 2317 deflated
DIALOGS.FRM 5641 1897 deflated
DIALOGS.MAK 95 67 deflated
DOS_ERR.BAS 3938 922 deflated
TPCREAD.ME 199 165 deflated

Download File CMDLG2.ZIP Here

Contents of the DIALOG.TXT file


It has proved difficult to retrieve the printer Driver, DeviceName and Port
combination selected using the MhPrintDlg%() function (without using the 'Def'
version to change the default printer and using GetProgileStringto load the Windows,Device line)

However, with the help of the DIALOG.ZIP file uploaded by
Costas Kitsos (73667,1755) we have derived the following:

The DevNames data Structure contains the following items:

wDriverOffset : Offset to start of DriverName e.g. 'Canon LPB-8 III'
wDeviceOffset : Offset to start of DeviceName e.g. 'LBPIII'
wOutputOffset : Offset to start of PortName e.g. 'LPT1'
wDefault : 1 if default printer choosen, 0 otherwise.

All offsets are relative to the base of the DevNames structure.

Maximum Sizes:
32 for device + nul
8 for driver + nul
4 for port + nul
Total = 47 extra bytes

Type DevNames
wDriverOffset As Integer
wDeviceOffset As Integer
wOutputOffset As Integer
wDefault As Integer

cNames As String * 47
End Type

Dim N As DevNames
Dim P As MhPrintDlog

If P.hDevNames <> 0 Then
Address = GlobalLock(P.hDevNames) ' Lock handle and obtain address
Call hmemcpy(N, ByVal Address, Len(N)) ' Copy to VB variable.
Ok = GlobalUnlock(P.hDevNames) ' Unlock handle.
' The Memory used by the Devnames structure may be freed with
'
' Ok = GlobalFree(P.hDevNames)
'
' but if you call the the printer dialog again you need it
' to keep the settings currently in use

' Correct the offsets to to the base of the cNames string
N.wDeviceOffset = N.wDeviceOffset - 7
N.wDriverOffset = N.wDriverOffset - 7
N.wOutputOffset = N.wOutputOffset - 7

lenDevice = N.wDriverOffset - N.wDeviceOffset - 1
lenDriver = N.wOutputOffset - N.wDriverOffset - 1
' and retrieve the strings
PrnDevice$ = Mid$(N.cNames, N.wDeviceOffset, lenDevice)
PrnDriver$ = Mid$(N.cNames, N.wDriverOffset, lenDriver)
PrnPort$ = Mid$(N.cNames, N.wOutputOffset, 4)
End If

The PrnDevice$,PrnDriver$ and PrnPort$ may then be used
with CreateDC to generate a Printer device context whenever
printing is required. (Without presenting the common dialog):

In Global module:
Declare Function CreateDC Lib "GDI" (ByVal lpDriverName As String, ByVal
lpDeviceName As String, ByVal lpOutput As String,ByVal pInitData As String) As
Integer
Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer

Where printing required:

hDC = CreateDC(PrnDevice$, PrnDriver$, PrnPort$, hDevMode)
'
' StartDoc Escape
' For each page:
' API Printing calls...
' NewPage Escape
' EndDoc Escape
lOk = DeleteDC(hDC)



 December 22, 2017  Add comments

Leave a Reply