Announcement

Collapse
No announcement yet.

Choose a Printer Dialog

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Choose a Printer Dialog

    Tonight I put together is neat little routine (at least I think it's neat) to choose a printer.

    Just insert at the top of your code and call Z_Choose_Printer.

    Code:
    '********************************************************************
    '********************************************************************
    '*******     Choose Printer Dialog **********************************
    '********************************************************************
    '*******  by Gösta H. Lovgren March 2008 ****************************
    '********************************************************************
    '********************************************************************
    '
    '      'adapted from Help File  
    '
     
    CallBack Function z_CB_Printer As Long
      Global hdlg_printer As Dword  
      Global Printer_Btns%(), Printer_Btn_Labels$()
      Global g_Parameters_Prnter  As Asciiz * 250
     
        Select Case CbMsg     'This is TO determine the message TYPE 
          '       
          Case %WM_INITDIALOG'<- Initialiaton when the program loads 
          '
          Case %WM_COMMAND  'This processes command messages
            Select Case CbCtl 'TO determine which CONTROL 
               Case 1700 To 1799
                 g_Parameters_Prnter = Printer_Btn_Labels$(CbCtl - 1700)
                 Dialog Set Text CbHndl, g_Parameters_Prnter
            End Select      
        End Select
    End Function
    '*********************************
    '
    Sub z_Choose_Printer
    '  Common_Locals
      Local ctr&, wdth&, hght&, stile&   
      Local hdlg, hDlg_Printer As Dword   
      Local Btn_Width&, Btn_Height&, row&, col&
      Local caption$
     
     
      If Len(g_Parameters_Prnter) < 1 Then
         g_Parameters_Prnter = "No Printer Chosen Yet"
      End If
     
      wdth = Len(g_Parameters_Prnter) * 6
      For ctr = 1 To PrinterCount
         ReDim Preserve Printer_Btns%(1 To ctr), _
                        Printer_Btn_Labels$(1 To ctr)
         Printer_Btn_Labels$(ctr) = Printer$(Name, ctr)
         Printer_Btns%(ctr) = ctr + 1700
     
         'dialog dimensions
         Hght = ctr * 15 
         If Len(Printer_Btn_Labels$(ctr)) * 5 > Wdth Then
           wdth = Len(Printer_Btn_Labels$(ctr)) * 5 
         End If
      Next
     
     
    '     Main dialog window     
       Dialog Font "Comic Sans", 10
        Caption$ =  g_Parameters_Prnter 
     
     
     Local lb_Len&
     lb_Len = Wdth - 20 
     
        Stile = _
           %WS_CAPTION    Or _
           %WS_SYSMENU
     
       Dialog New hDlg, Caption$,,, Wdth, Hght,_
           Stile, _
           %WS_Ex_WindowEdge, _
           To hDlg_Printer
     
        Dialog Set Color hDlg_Printer, -1&,  252 + (241 * 256) + (167 * 256 * 256) 'cream
    '    Dialog Set Icon hDlg_Printer, "Zero"    
     
        Stile = %BS_Center
        Btn_Width = Wdth - 10
        Btn_Height = 12
         Col = 5
         Row = 3         
        For ctr = LBound(Printer_Btns%()) To UBound(Printer_Btns%())
           Control Add Button, hDlg_Printer, _
              Printer_Btns%(ctr), _
              Printer_Btn_Labels$(ctr), _
              Col, Row, _
              Btn_Width, Btn_Height,_
              Stile    
           Row = Row + Btn_Height + 2   
        Next ctr          
     
       Dialog Show Modal hDlg_Printer  Call z_CB_Printer
     
    End Sub 
    '********************************************************************
    '************ End of Printer Dialog *********************************
    '********************************************************************
    '********************************************************************
    Last edited by Gösta H. Lovgren-2; 4 Mar 2008, 09:31 PM. Reason: Removed some misleading lines
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    'Nice!
    'There is a syntax error, though.

    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    DECLARE SUB z_choosePrinter
    
    '********************************************************************
    '********************************************************************
    '*******     Choose Printer Dialog **********************************
    '********************************************************************
    '*******  by Gösta H. Lovgren March 2008 ****************************
    '********************************************************************
    '********************************************************************
    '
    '      'adapted from Help File
    '
    CALLBACK FUNCTION z_CB_Printer AS LONG
      GLOBAL hdlg_printer AS DWORD
      GLOBAL Printer_Btns%(), Printer_Btn_Labels$()
      GLOBAL g_Parameters_Prnter  AS ASCIIZ * 250
        SELECT CASE CBMSG     'This is TO determine the message TYPE
          '
          CASE %WM_INITDIALOG'<- Initialiaton when the program loads
          '
          CASE %WM_COMMAND  'This processes command messages
            SELECT CASE CBCTL 'TO determine which CONTROL
               CASE 1700 TO 1799
                 g_Parameters_Prnter = Printer_Btn_Labels$(CBCTL - 1700)
                 DIALOG SET TEXT CBHNDL, g_Parameters_Prnter
            END SELECT
        END SELECT
    END FUNCTION
    '*********************************
    '
    SUB z_Choose_Printer
    '  Common_Locals
      LOCAL ctr&, wdth&, hght&, stile&
      LOCAL hdlg, hDlg_Printer AS DWORD
      LOCAL Btn_Width&, Btn_Height&, row&, col&
      LOCAL caption$
    
      IF LEN(g_Parameters_Prnter) < 1 THEN
         g_Parameters_Prnter = "No Printer Chosen Yet"
      END IF
      'wdth = Len(g_Parameters.Prnter) * 6
      wdth = LEN(g_Parameters_Prnter) * 6  'changed to this
      
      FOR ctr = 1 TO PRINTERCOUNT
         REDIM PRESERVE Printer_Btns%(1 TO ctr), _
                        Printer_Btn_Labels$(1 TO ctr)
         Printer_Btn_Labels$(ctr) = PRINTER$(NAME, ctr)
         Printer_Btns%(ctr) = ctr + 1700
         'dialog dimensions
         Hght = ctr * 15
         IF LEN(Printer_Btn_Labels$(ctr)) * 5 > Wdth THEN
           wdth = LEN(Printer_Btn_Labels$(ctr)) * 5
         END IF
      NEXT
    
    '     Main dialog window
       DIALOG FONT "Comic Sans", 10
        Caption$ =  g_Parameters_Prnter
    
     LOCAL lb_Len&
     lb_Len = Wdth - 20
        Stile = _
           %WS_CAPTION    OR _
           %WS_SYSMENU
       DIALOG NEW hDlg, Caption$,,, Wdth, Hght,_
           Stile, _
           %WS_Ex_WindowEdge, _
           TO hDlg_Printer
    '    Dialog Set Color hDlg_Printer, -1&, %Cream
    '    Dialog Set Icon hDlg_Printer, "Zero"
        Stile = %BS_Center
        Btn_Width = Wdth - 10
        Btn_Height = 12
         Col = 5
         Row = 3
        FOR ctr = LBOUND(Printer_Btns%()) TO UBOUND(Printer_Btns%())
           CONTROL ADD BUTTON, hDlg_Printer, _
              Printer_Btns%(ctr), _
              Printer_Btn_Labels$(ctr), _
              Col, Row, _
              Btn_Width, Btn_Height,_
              Stile
           Row = Row + Btn_Height + 2
        NEXT ctr
       DIALOG SHOW MODAL hDlg_Printer  CALL z_CB_Printer
    END SUB
    '********************************************************************
    '************ End of Printer Dialog *********************************
    '********************************************************************
    '********************************************************************
    FUNCTION PBMAIN AS LONG
      z_Choose_Printer
         '  wdth = LEN(g_Parameters.Prnter) * 6   'syntax error
      
    END FUNCTION
    How long is an idea?

    Comment


    • #3
      Originally posted by Mike Doty View Post
      'Nice!
      Thanks Mike. I think it's pretty neat.

      'There is a syntax error, though.
      Thanks for spotting it. I corrected it in the code above. I use a TYPE to hold parameters and missed changing that one.

      Should have made it compilable like you did but never thought (past bedtime when I finished). If no one finds more errors, I'll copy it over to Source Code.
      It's a pretty day. I hope you enjoy it.

      Gösta

      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

      Comment


      • #4
        A friend had a good idea for this little gem. It would be neat to have on the desktop so (computerphobe) users could change the default printer at the Windows level (registry setting?). I have no idea if it can be done. Or even how to do it, if it can.

        Be even neater if someone could just take the code and insert the capability. Sort of a "Share the Code" deal.
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          so (computerphobe) users could change the default printer at the Windows level (registry setting?). I have no idea if it can be done. Or even how to do it, if it can.
          The deviously-named "SetDefaultPrinter" Windows API function looks promising......
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Semen posted this code in 2001 (works with XP SP2)

            www.powerbasic.com/support/pbforums/showthread.php?t=22951&highlight=setdefaultprinter

            Code:
               #COMPILE EXE
               #REGISTER NONE
               #DIM ALL
               #INCLUDE "WIN32API.INC"
               DECLARE FUNCTION SetDefaultPrinter (ASCIIZ) AS LONG
               FUNCTION PBMAIN()
                  DIM PrinterName AS ASCIIZ * 255
                  GetProfileString "WINDOWS", "DEVICE", ",,,", PrinterName, SIZEOF(PrinterName)
                  MSGBOX "Default is " + PrinterName
                  DIM Section AS ASCIIZ * 32767, nb AS LONG, kb AS LONG
                  nb = GetProfileSection ("PrinterPorts", Section, SIZEOF(Section)): kb = 0
                  DIM p AS ASCIIZ PTR, hSetDefaultPrinter AS DWORD, hLibWinSpool AS LONG
                  p = VARPTR(Section)
                  DO
                     IF LEN(@p) = 0 THEN EXIT DO
                     IF MSGBOX (@p, %MB_YESNO, "To set as default printer") = 6 THEN
                        hLibWinSpool= LoadLibrary("winspool.drv")
                        IF hLibWinSpool THEN hSetDefaultPrinter = GetProcAddress(hLibWinSpool, "SetDefaultPrinterA")
                        IF hSetDefaultPrinter THEN
                           CALL DWORD hSetDefaultPrinter USING SetDefaultPrinter(PARSE$(@p, "=", 1))
                        ELSE
                           WriteProfileString "WINDOWS", "DEVICE", @p
                           SendNotifyMessage %HWND_BROADCAST, %WM_WININICHANGE, 0, BYVAL p
                        END IF
                        IF hLibWinSpool THEN FreeLibrary hLibWinSpool
                        EXIT DO
                     END IF
                     p = p + LEN(@p) + 1
                  LOOP
               END FUNCTION
            Last edited by Mike Doty; 6 Mar 2008, 09:00 AM.
            How long is an idea?

            Comment


            • #7
              >(works with XP SP2)

              Code:
              SetDefaultPrinter "Printername"
              ...works on all current versions of the Windows operating system. (Win/2000+)

              Above code is only required on Win 9x/ME and NT4.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Thanks boys. I fooled around with both methods this morning. Turns out one method was just too devious for a simple mind so I have to go with Semen's solution (via Mike D).

                SetDefaultPrinter just added the chosen printer name to the beginning of the existing driver name. ie if "Canon 500" was installed and you chose "HP 6500" from the list, the driver name became "HP 6500Canon 500" ((To test, Set a new printer, close and run again.)

                'http://www.powerbasic.com/support/pbforums/showthread.php?t=36615
                '
                Code:
                'http://www.powerbasic.com/support/pbforums/showthread.php?t=36615
                #Compile Exe  
                #Include "WIN32API.INC"
                 
                 
                 
                '********************************************************************
                '********************************************************************
                '*******     Choose Printer Dialog **********************************
                '********************************************************************
                '*******  by Gösta H. Lovgren March 2008 ****************************
                '********************************************************************
                '********************************************************************
                '
                ''             adapted from the Help File  
                '
                 
                 
                 
                CallBack Function z_CB_Printer As Long
                  Global hdlg_printer As Dword  
                  Global Printer_Btns%(), Printer_Btn_Labels$()
                  Global g_Parameters_Prnter  As Asciiz * 255
                  
                    Select Case CbMsg     'This is TO determine the message TYPE 
                      '       
                      Case %WM_INITDIALOG'<- Initialiaton when the program loads 
                      '
                      Case %WM_COMMAND  'This processes command messages
                        Select Case CbCtl    'Determine which CONTROL sending message
                           Case 1700 To 1799
                              g_Parameters_Prnter = Printer_Btn_Labels$(CbCtl - 1700)
                             Dialog Set Text CbHndl, g_Parameters_Prnter 'Dialog header
                 
                          'ala the Master Deviate 
                'DECLARE FUNCTION SetDefaultPrinter LIB "WINSPOOL.DRV" ALIAS _ 
                '                "SetDefaultPrinterA" (pszPrinter As Asciiz) As Long             
                '  n& = SetDefaultPrinter(g_Parameters_Prnter) '<-- adds printer name to Driver name 
                 
                          'ala Semen via Mike D <-- Works - only puts in Printer Name
                ' n& = WriteProfileString "WINDOWS", _ 'lpszSection AS ASCIIZ
                '                          "DEVICE",  _'lpszKeyName AS ASCIIZ                          
                '                          PrinterName _'lpszString AS ASCIIZ
                   n& = WriteProfileString("WINDOWS", "DEVICE", g_Parameters_Prnter)
                        End Select      
                    End Select
                End Function
                '*********************************
                '
                Sub z_Choose_Printer                          
                'DECLARE FUNCTION   GetProfileString LIB "KERNEL32.DLL" ALIAS   
                '   "GetProfileStringA" (lpAppName AS ASCIIZ, _
                                        'lpKeyName AS ASCIIZ, _
                                        'lpDefault AS ASCIIZ, _
                                        'lpReturnedString AS ASCIIZ, _
                                        'BYVAL nSize AS DWORD) AS DWORD
                 
                'DECLARE FUNCTION WriteProfileString LIB "KERNEL32.DLL" ALIAS 
                '   "WriteProfileStringA" (lpszSection AS ASCIIZ, _
                                          'lpszKeyName AS ASCIIZ, _
                                          'lpszString AS ASCIIZ) AS LONG       
                 
                ' n& = WriteProfileString "WINDOWS", _ 'lpszSection AS ASCIIZ
                '                          "DEVICE",  _'lpszKeyName AS ASCIIZ                          
                '                          PrinterName _'lpszString AS ASCIIZ
                 
                '  Common_Locals
                  Local ctr&, wdth&, hght&, stile&   
                  Local hdlg, hDlg_Printer As Dword   
                  Local Btn_Width&, Btn_Height&, row&, col&
                  Local caption$                  
                  
                  If Len(g_Parameters_Prnter) < 1 Then
                       'semen's code via Mike Doty
                     Dim PrinterName As Asciiz * 255
                      GetProfileString "WINDOWS", _  'lpAppName AS ASCIIZ, _
                                       "DEVICE", _   'lpKeyName AS ASCIIZ, _
                                       ",,,", _      'lpDefault AS ASCIIZ, _
                                       PrinterName, _'lpReturnedString AS ASCIIZ, _
                                       SizeOf(PrinterName)'BYVAL nSize AS DWORD
                 
                     g_Parameters_Prnter = PrinterName'"No Printer Chosen Yet"
                  End If
                  
                  wdth = Len(g_Parameters_Prnter) * 6
                  For ctr = 1 To PrinterCount
                     ReDim Preserve Printer_Btns%(1 To ctr), _
                                    Printer_Btn_Labels$(1 To ctr)
                     Printer_Btn_Labels$(ctr) = Printer$(Name, ctr)
                     Printer_Btns%(ctr) = ctr + 1700
                     'dialog dimensions
                     Hght = ctr * 15 
                     If Len(Printer_Btn_Labels$(ctr)) * 5 > Wdth Then
                       wdth = Len(Printer_Btn_Labels$(ctr)) * 5 
                     End If
                  Next
                      
                  
                '     Main dialog window     
                   Dialog Font "Comic Sans", 10
                    Caption$ =  g_Parameters_Prnter '"Pick a Printer, Baby"
                     ' Wdth = 100'800
                      'Hght = 200'600          
                      
                      
                 Local lb_Len&
                 lb_Len = Wdth - 20 
                 
                    Stile = _
                       %WS_CAPTION    Or _
                       %WS_SYSMENU
                    
                   Dialog New hDlg, Caption$,,, Wdth, Hght,_
                       Stile, _
                       %WS_Ex_WindowEdge, _
                       To hDlg_Printer
                 
                    Dialog Set Color hDlg_Printer, -1&, 252 + (241 * 256) + (167 * 256 * 256) 'cream
                    'Dialog Set Icon hDlg_Printer, "Zero"    
                    
                    Stile = %BS_Center
                    Btn_Width = Wdth - 10
                    Btn_Height = 12
                     Col = 5
                     Row = 3         
                    For ctr = LBound(Printer_Btns%()) To UBound(Printer_Btns%())
                       Control Add Button, hDlg_Printer, _
                          Printer_Btns%(ctr), _
                          Printer_Btn_Labels$(ctr), _
                          Col, Row, _
                          Btn_Width, Btn_Height,_
                          Stile    
                       Row = Row + Btn_Height + 2   
                    Next ctr          
                              
                   Dialog Show Modal hDlg_Printer  Call z_CB_Printer
                 
                End Sub 
                '********************************************************************
                '************ End of Printer Dialog *********************************
                '********************************************************************
                '********************************************************************
                 
                Function PBMain
                  Call z_Choose_Printer
                End Function
                '
                It's a pretty day. I hope you enjoy it.

                Gösta

                JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                Comment


                • #9
                  Using XP SP2 it removes the default printer from being displayed in the Windows printers/faxes dialog.

                  Here is some code from Pierre Bellisle (lost link.)
                  Code:
                  #COMPILE EXE             '#Win 702#
                  #DIM ALL
                  #INCLUDE "Win32Api.Inc"  '2003-03-27
                  $AppName         = "PrinterSelect"
                  %FramePS         = 1001
                  %ButtonSetPS     = 1002
                  %ButtonInfoPS    = 1003
                  %ButtonExitPS    = 1004
                  %ListboxPS       = 1005
                  %LabelPS         = 1006
                  %MaxPrinterCount = 20
                  GLOBAL hDlg AS LONG
                  '______________________________________________________________________________
                  FUNCTION GetPrinterDefault(PList() AS STRING) AS LONG
                   LOCAL zPrinterDefault AS ASCIIZ * 255
                   LOCAL Counter         AS LONG
                   'GetProfileString will return: Printer Name,Driver,Port
                   'Like...                       Epson LQ-500,EPSON24,LPT1:
                   GetProfileString "WINDOWS", "DEVICE", ",,,", zPrinterDefault, SIZEOF(zPrinterDefault)
                   FOR Counter = 1 TO UBOUND(PList())
                     IF PARSE$(PList(Counter), "=", 1) = PARSE$(zPrinterDefault, ",", 1) THEN
                       FUNCTION = Counter
                       EXIT FUNCTION
                     END IF
                   NEXT
                  END FUNCTION
                  '______________________________________________________________________________
                  SUB GetPrinterAll(PList() AS STRING)
                   LOCAL  Section       AS ASCIIZ * 32767
                   LOCAL  SectionPtr    AS ASCIIZ PTR
                   LOCAL PrintersCount AS  LONG
                   LOCAL Retval        AS  LONG
                   Retval = GetProfileSection("PrinterPorts", Section,  SIZEOF(Section))
                   IF Retval THEN
                     SectionPtr =  VARPTR(Section)
                     PrintersCount = 0
                      DO
                       IF LEN(@SectionPtr) = 0 THEN EXIT  DO
                       INCR PrintersCount
                       IF  PrintersCount > %MaxPrinterCount THEN EXIT LOOP
                        REDIM PRESERVE PList(0 TO PrintersCount) AS STRING
                        PList(PrintersCount) = @SectionPtr 'Printer  Name=Driver,Port,NonSelect,Retry
                                                           'Like... Epson LQ-500=EPSON24,LPT1:,15,45
                       SectionPtr  = SectionPtr + LEN(@SectionPtr) + 1
                     LOOP
                   END  IF
                  END  SUB
                  '______________________________________________________________________________
                  SUB SetPrinterDefault(Printer AS STRING)
                   LOCAL OsInfo      AS OSVERSIONINFO
                   LOCAL PD          AS PRINTER_DEFAULTS
                   LOCAL PI5         AS PRINTER_INFO_5
                   LOCAL DeviceLine  AS STRING
                   LOCAL Win         AS STRING
                   LOCAL hPrinter    AS LONG
                   LOCAL Need        AS LONG
                   LOCAL LastError   AS LONG
                   LOCAL Retval      AS LONG
                   LOCAL Counter     AS LONG
                   DIM PList(0 TO 0) AS STRING
                   OsInfo.dwOsVersionInfoSize = SIZEOF(OsInfo)
                   GetVersionEx OsInfo
                   IF OsInfo.dwPlatformId = %VER_PLATFORM_WIN32_WINDOWS THEN 'Windows 95/98/Me
                     'Get the selected printer
                     IF Printer = "" THEN
                       MSGBOX "Error: No printer specified !", %MB_ICONERROR OR %MB_OK, $AppName
                       EXIT SUB
                     END IF
                     'Set the PRINTER_DEFAULTS members
                     PD.pDatatype = 0&
                     PD.DesiredAccess = %PRINTER_ALL_ACCESS OR PD.DesiredAccess
                     'Get an handle to the printer
                     Retval = OpenPrinter(BYCOPY Printer, hPrinter, PD)
                     IF Retval = 0 THEN
                       MSGBOX "Error: " & Printer & " not found !", %MB_ICONERROR OR %MB_OK, $AppName
                       EXIT SUB
                     END IF
                     'Check how many bytes we need
                     Retval = GetPrinter(hPrinter, 5, BYVAL 0, BYVAL 0, Need)
                     REDIM PArray((Need \ 4)) AS LONG
                     'Get info
                     Retval = GetPrinter(hPrinter, 5, PArray(0), Need, Need)
                     IF Retval = %False THEN
                       MSGBOX "Error: GetPrinter !", %MB_ICONERROR OR %MB_OK, $AppName
                       EXIT SUB
                     END IF
                     'Set default printer
                     PI5.pPrinterName             = PArray(0)
                     PI5.pPortName                = PArray(1)
                     PI5.Attributes               = PArray(2)
                     PI5.DeviceNotSelected        = PArray(3)
                     PI5.TransmissionRetryTimeout = PArray(4)
                     PI5.Attributes               = %PRINTER_ATTRIBUTE_DEFAULT          'This make it the default printer
                     Retval = SetPrinter(hPrinter, BYVAL 5, BYVAL VARPTR(PI5), BYVAL 0) 'Tell Windows to set default printer
                     IF Retval = 0 THEN
                       MSGBOX "SetPrinter failed !", %MB_ICONERROR OR %MB_OK, $AppName
                       EXIT SUB
                     END IF
                     ClosePrinter hPrinter
                   ELSE 'Windows NT/2000/XP
                     GetPrinterAll PList()  'String Array in the form of: Printer Name=Driver,Port,NonSelect,Retry
                     FOR Counter = 1 TO UBOUND(PList())
                       IF UCASE$(PARSE$(PList(Counter), "=", 1)) = UCASE$(Printer) THEN
                         DeviceLine = PARSE$(PList(Counter), ANY "=,", 1) & "," & _
                                      PARSE$(PList(Counter), ANY "=,", 2) & "," & _
                                      PARSE$(PList(Counter), ANY "=,", 3)
                         'Store WIN.INI, [WINDOWS], DEVICE =
                         Retval = WriteProfileString("Windows", "Device", BYCOPY DeviceLine)
                         'Cause all applications to reload the INI file
                         Win = "Windows"
                         Retval = SendMessage(%HWND_BROADCAST, %WM_WININICHANGE, 0, BYCOPY VARPTR(Win))
                         EXIT FOR
                       END IF
                     NEXT
                     IF LEN(Win) = 0 THEN
                       MSGBOX "Error: " & Printer & " not found !", %MB_ICONERROR OR %MB_OK, $AppName
                     END IF
                   END IF
                  END SUB
                  '______________________________________________________________________________
                  CALLBACK FUNCTION DlgProc() AS LONG
                   LOCAL Retval      AS LONG
                   LOCAL Counter     AS LONG
                   LOCAL Printer     AS STRING
                   DIM PList(0 TO 0) AS STATIC STRING
                   SELECT CASE CBMSG
                     CASE %WM_INITDIALOG
                       GetPrinterAll PList()  'StringArray in the form of: Printer Name=Driver,Port,NonSelect,Retry
                       FOR COUNTER = 1 TO UBOUND(PList())
                         LISTBOX ADD hDlg, %ListboxPS, PARSE$(PList(Counter), "=", 1)
                       NEXT
                       LISTBOX SELECT hDlg, %ListboxPS, GetPrinterDefault(PList())
                       CONTROL SET TEXT hDlg, %LabelPS, "Default is: " _
                               & PARSE$(PList(GetPrinterDefault(PList())), "=" , 1)
                     CASE %WM_COMMAND
                       SELECT CASE CBCTL
                         CASE %ButtonSetPS
                           IF CBCTLMSG = %BN_CLICKED THEN
                             LISTBOX GET TEXT hDlg, %ListboxPS TO Printer
                             SetPrinterDefault Printer
                           END IF
                         CASE %ListboxPS
                           IF CBCTLMSG = %LBN_DBLCLK THEN
                             LISTBOX GET TEXT hDlg, %ListboxPS TO Printer
                             SetPrinterDefault Printer
                           END IF
                         CASE %ButtonInfoPS
                           IF CBCTLMSG = %BN_CLICKED THEN
                             CONTROL SEND hDlg, %ListboxPS, %LB_GetCurSel, 0, 0 TO Retval
                             INCR Retval
                             MSGBOX "Name:"   & $TAB & PARSE$(PList(Retval), ANY "=,", 1) & $CRLF & _
                                    "Driver:" & $TAB & PARSE$(PList(Retval), ANY "=,", 2) & $CRLF & _
                                    "Port:"   & $TAB & PARSE$(PList(Retval), ANY "=,", 3) & $CRLF & _
                                    "Select:" & $TAB & PARSE$(PList(Retval), ANY "=,", 4) & $CRLF & _
                                    "Retry:"  & $TAB & PARSE$(PList(Retval), ANY "=,", 5), _
                                    %MB_ICONINFORMATION OR %MB_OK, $AppName
                           END IF
                         CASE  %ButtonExitPS, %IDCANCEL
                           IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
                       END SELECT
                     CASE %WM_WININICHANGE
                       'Here, we will be inform by Window if the default printer change
                       CONTROL SET TEXT hDlg, %LabelPS, "Default is: " _
                               & PARSE$(PList(GetPrinterDefault(PList())), "=" , 1)
                   END SELECT
                  END FUNCTION
                  '______________________________________________________________________________
                  FUNCTION PBMAIN AS LONG
                   LOCAL Counter       AS LONG
                   LOCAL Retval        AS LONG
                   LOCAL Kommand       AS STRING
                   DIM   PList(0 TO 0) AS STRING
                   Kommand = TRIM$(COMMAND$)
                   'Kommand = TRIM$("Epson LQ-500") 'Use your printer name for testing...
                   IF LEN(Kommand) THEN
                     SetPrinterDefault Kommand
                   ELSE
                     DIALOG NEW 0, $AppName, , , 280, 150, %WS_POPUP OR %WS_VISIBLE OR %WS_CLIPSIBLINGS _
                                   OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR %DS_3DLOOK _
                                   OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT TO hDlg
                     CONTROL ADD FRAME, hDlg, %FramePS, "Printer", 9, 9, 264, 93
                     CONTROL ADD LISTBOX, hDlg, %ListboxPS, , 21, 24, 240, 76, %WS_CHILD OR %WS_VISIBLE _
                                 OR %WS_BORDER OR %WS_VSCROLL OR %WS_TABSTOP OR %LBS_NOTIFY, 0
                     CONTROL ADD LABEL, hDlg, %LabelPS, "Default Printer is...", 21, 110, 240, 12
                     CONTROL ADD BUTTON, hDlg, %ButtonSetPS, "Set default printer", 9, 126, 130, 18, _
                               %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
                     CONTROL ADD BUTTON, hDlg, %ButtonInfoPS, "Info", 150, 126, 30, 18, _
                               %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
                     CONTROL ADD BUTTON, hDlg, %ButtonExitPS, "Exit", 192, 126, 78, 18, _
                               %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
                     DIALOG SHOW MODAL hDlg, CALL DlgProc
                   END IF
                  END FUNCTION
                  '______________________________________________________________________________
                  Last edited by Mike Doty; 6 Mar 2008, 11:27 AM.
                  How long is an idea?

                  Comment


                  • #10
                    Originally posted by Michael Mattias View Post
                    >(works with XP SP2)
                    Above code is only required on Win 9x/ME and NT4.
                    Wrong, this code won't work with Windows 9x...
                    --------------------

                    Gösta, if you want to support the antique Windows 9x series,
                    then you may have a look at this one
                    it will work with Windows 95, 98, Me, NT4, 2000, XP and Vista (Tested today)

                    Added: Mike was faster than me, we both refer to the same code...
                    Last edited by Pierre Bellisle; 6 Mar 2008, 11:38 AM.

                    Comment


                    • #11
                      Gosta, your code works, but ...
                      I missed it because I was checking the printers and faxes dialog which is not updated even after clicking on refresh. I did see the default printer using your code in the caption. It looks like something needs to be broadcast for the windows dialog to be updated.
                      Pierre's code does update the windows and faxes dialog with the check mark.
                      Last edited by Mike Doty; 6 Mar 2008, 11:30 AM.
                      How long is an idea?

                      Comment


                      • #12
                        Maybe it's <<SendMessage(%HWND_BROADCAST, %WM_WININICHANGE...>>
                        See in my code above...

                        Comment


                        • #13
                          Originally posted by Mike Doty View Post
                          Gosta, your code works, but ...
                          I missed it because I was checking the printers and faxes dialog which is not updated even after clicking on refresh. I did see the default printer using your code in the caption. It looks like something needs to be broadcast for the windows dialog to be updated.
                          It updates the Caption on every click now:

                          'http://www.powerbasic.com/support/pbforums/showthread.php?t=36615
                          '
                          Code:
                          'http://www.powerbasic.com/support/pbforums/showthread.php?t=36615
                          #Compile Exe  
                          #Include "WIN32API.INC"
                           
                          '********************************************************************
                          '********************************************************************
                          '*******     Choose Printer Dialog **********************************
                          '********************************************************************
                          '*******  by Gösta H. Lovgren March 2008 ****************************
                          '********************************************************************
                          '********************************************************************
                          '
                          ''             adapted from the Help File  
                          '
                           
                          Macro m_Set_Dialog_Caption
                            GetProfileString "WINDOWS", _  'lpAppName AS ASCIIZ, _
                                             "DEVICE", _   'lpKeyName AS ASCIIZ, _
                                             ",,,", _      'lpDefault AS ASCIIZ, _
                                             PrinterName, _'lpReturnedString AS ASCIIZ, _
                                             SizeOf(PrinterName)'BYVAL nSize AS DWORD
                             g_Parameters_Prnter = PrinterName'"No Printer Chosen Yet"
                             Dialog Set Text CbHndl, "Printer Driver = " & g_Parameters_Prnter 'Dialog header
                          End Macro
                          '
                          CallBack Function z_CB_Printer As Long
                            Global hdlg_printer As Dword  
                            Global Printer_Btns%(), Printer_Btn_Labels$()
                            Global g_Parameters_Prnter  As Asciiz * 255
                            Local PrinterName As Asciiz * 255
                           
                              Select Case CbMsg     'This is TO determine the message TYPE 
                                '       
                                Case %WM_INITDIALOG'<- Initialiaton when the program loads 
                                 'semen's code via Mike Doty
                                    m_Set_Dialog_Caption
                                '
                                Case %WM_COMMAND  'This processes command messages
                                  Select Case CbCtl    'Determine which CONTROL sending message
                                     Case 1700 To 1799
                                        g_Parameters_Prnter = Printer_Btn_Labels$(CbCtl - 1700)
                           
                                    'ala Semen via Mike D <-- Works - only puts in Printer Name
                          ' n& = WriteProfileString ("WINDOWS", _ 'lpszSection AS ASCIIZ
                          '                          "DEVICE",  _'lpszKeyName AS ASCIIZ                          
                          '                          PrinterName) _'lpszString AS ASCIIZ
                                    n& = WriteProfileString("WINDOWS", _ '<-- Sets the driver
                                                            "DEVICE", _
                                                            g_Parameters_Prnter)
                                    m_Set_Dialog_Caption  
                           
                                  End Select      
                              End Select
                          End Function
                          '*********************************
                          '
                          Sub z_Choose_Printer                          
                          '  Common_Locals
                            Local ctr&, wdth&, hght&, stile&   
                            Local hdlg, hDlg_Printer As Dword   
                            Local Btn_Width&, Btn_Height&, row&, col&
                            Local caption$, longest&                  
                           
                           
                            wdth = 0'Len(g_Parameters_Prnter) * 6
                            For ctr = 1 To PrinterCount
                               ReDim Preserve Printer_Btns%(1 To ctr), _
                                              Printer_Btn_Labels$(1 To ctr)
                               Printer_Btn_Labels$(ctr) = Printer$(Name, ctr)
                               Printer_Btns%(ctr) = ctr + 1700
                               'dialog dimensions
                               Hght = ctr * 15 
                               If Len(Printer_Btn_Labels$(ctr)) * 5 > Wdth Then
                                 wdth = Len(Printer_Btn_Labels$(ctr)) * 5 
                               End If
                            Next
                            Wdth = Wdth + (Len("Printer Driver = ") * 5) 'should be long enough to accomdate long driver names    
                           
                          '     Main dialog window     
                             Dialog Font "Comic Sans", 10
                              Caption$ = "Pick a Printer, Baby"
                           
                           
                           Local lb_Len&
                           lb_Len = Wdth - 20 
                              Stile = _
                                 %WS_CAPTION    Or _
                                 %WS_SYSMENU
                           
                             Dialog New hDlg, Caption$,,, Wdth, Hght,_
                                 Stile, _
                                 %WS_Ex_WindowEdge, _
                                 To hDlg_Printer
                              Dialog Set Color hDlg_Printer, -1&, 252 + (241 * 256) + (167 * 256 * 256) 'cream
                              'Dialog Set Icon hDlg_Printer, "Zero"    
                           
                              Stile = %BS_Center
                              Btn_Width = Wdth - 10
                              Btn_Height = 12
                               Col = 5
                               Row = 3         
                              For ctr = LBound(Printer_Btns%()) To UBound(Printer_Btns%())
                                 Control Add Button, hDlg_Printer, _
                                    Printer_Btns%(ctr), _
                                    Printer_Btn_Labels$(ctr), _
                                    Col, Row, _
                                    Btn_Width, Btn_Height,_
                                    Stile    
                                 Row = Row + Btn_Height + 2   
                              Next ctr          
                           
                             Dialog Show Modal hDlg_Printer  Call z_CB_Printer
                          End Sub 
                          '********************************************************************
                          '************ End of Printer Dialog *********************************
                          '********************************************************************
                          '********************************************************************
                          Function PBMain
                            Call z_Choose_Printer
                          End Function
                          '
                          Youse guys is great!!!
                          Last edited by Gösta H. Lovgren-2; 6 Mar 2008, 12:34 PM. Reason: Removed unnecessary "Dialog Set"
                          It's a pretty day. I hope you enjoy it.

                          Gösta

                          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                          Comment


                          • #14
                            It removes the check mark indicating the default printer (using Windows XP SP2) from the Printers and Faxes dialog box.
                            How long is an idea?

                            Comment


                            • #15
                              Originally posted by Mike Doty View Post
                              It removes the check mark indicating the default printer (using Windows XP SP2) from the Printers and Faxes dialog box.
                              Which "it"? Lotsa code here now. {grin}

                              Oops. Just checked my latest code and it doesn't change the default printer (checked via the printer dialog in Windows) It was working before I moved things around. {oh well, what's else is new?}.

                              Ah'll be bock!

                              {minute later} Found it. Be rght back as soon as fixed.

                              {more minutes later} Okay, I guess everything is okay after all. What I was doing is checking the printer setting using the Printer Dialog in Word. It didn't change there unless I exited/reloaded Word. I'm guessing that Word loads the Profile when it starts and doesn't get notified when changes are made to it.

                              Sorry for the (false) alarm. I just get so escited when I play in the deep end with the (you) Big Kids.

                              ============================================================
                              A man may learn wisdom even from a foe.
                              Aristophanes
                              ============================================================
                              Last edited by Gösta H. Lovgren-2; 6 Mar 2008, 01:13 PM. Reason: Found it
                              It's a pretty day. I hope you enjoy it.

                              Gösta

                              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                              Comment

                              Working...
                              X