Author Topic: Select printer  (Read 18330 times)

0 Members and 1 Guest are viewing this topic.

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Select printer
« on: January 04, 2019, 11:27:53 am »
Hi,

The program allows you to select the default printer from the list of printers that your computer has.
When it starts, the red line indicates the current printer and with the arrows up and down another one is indicated in the list. With the ENTER key the new printer is selected and with the ESC key the program is left without changing it.
I hope it works for you in your programs.

CLS
COLOR 15, 0
SHELL _HIDE "cmd/c wmic printer get name, default> impresora.txt"
DIM impresora(50) AS STRING
OPEN "I", 10, "impresora.txt"
n% = 0
LINE INPUT #10, dato$
DO
    LINE INPUT #10, dato$: LINE INPUT #10, dato$
    IF EOF(10) THEN EXIT DO
    n% = n% + 1
    DO
        a% = INSTR(dato$, CHR$(0))
        IF a% = 0 THEN EXIT DO
        IF a% = 1 THEN
            dato$ = MID$(dato$, a% + 1)
        ELSEIF a% = LEN(dato$) THEN
            dato$ = LEFT$(dato$, a% - 1)
        ELSE
            dato$ = LEFT$(dato$, a% - 1) + MID$(dato$, a% + 1)
        END IF
    LOOP
    a% = INSTR(dato$, "FALSE"): b% = INSTR(dato$, "TRUE")
    IF a% <> 0 THEN
        MID$(dato$, a%, 5) = SPACE$(5)
        dato$ = LTRIM$(RTRIM$(dato$))
    END IF
    IF b% <> 0 THEN
        MID$(dato$, b%, 5) = SPACE$(4)
        dato$ = LTRIM$(RTRIM$(dato$))
        loca% = n%
    END IF
    impresora(n%) = dato$
    impresora(0) = LTRIM$(STR$(n%))
LOOP
COLOR 7, 0
CLS
COLOR 1, 7
LOCATE 8, 20: PRINT "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"
LOCATE 9, 20: PRINT "º                                        º"
LOCATE 10, 20: PRINT "ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹"
LOCATE 11, 20: PRINT "º                                        º"
LOCATE 12, 20: PRINT "º                                        º"
LOCATE 13, 20: PRINT "º                                        º"
LOCATE 14, 20: PRINT "º                                        º"
LOCATE 15, 20: PRINT "º                                        º"
LOCATE 16, 20: PRINT "ÈÍÍÍÍÍÍÍ<Esc>ÍÍÍÍ<"; CHR$(24); ">Í<"; CHR$(25); ">ÍÍÍÍ<"; CHR$(17); CHR$(217); ">ÍÍÍÍÍÍÍÍͼ"
COLOR 15, 1
LOCATE 9, 21: PRINT "            SELECT PRINTER              ";
COLOR 1, 7
max% = VAL(impresora(0))
GOSUB impridatos
DO
    x$ = "": WHILE x$ = "": x$ = INKEY$: WEND
    x$ = UCASE$(x$)
    x% = ASC(RIGHT$(x$, 1))
    SELECT CASE x%
        CASE IS = 27
            a$ = "@"
            EXIT DO
        CASE IS = 13
            a$ = impresora(loca%)
            EXIT DO
        CASE IS = 72: '--Subir
            IF loca% > 1 THEN
                loca% = loca% - 1
                GOSUB impridatos
            END IF
        CASE IS = 80: '--Bajar
            IF loca% < max% THEN
                loca% = loca% + 1
                GOSUB impridatos
            END IF
    END SELECT
LOOP
COLOR 7, 0
CLS
IF a$ <> "@" THEN
    SHELL _HIDE "cmd/c wmic printer where name='" + a$ + "' call setdefaultprinter"
    PRINT "Selected printer: "; a$
ELSE
    PRINT "The printer has not been modified"
END IF
END

impridatos:
y% = 1
FOR n% = loca% - 2 TO loca% + 2
    IF n% < 1 OR n% > max% THEN
        COLOR 1, 7: LOCATE y% + 10, 21: PRINT "                                        "
    ELSE
        IF n% = loca% THEN COLOR 15, 4 ELSE COLOR 1, 7
        LOCATE y% + 10, 21: PRINT "                                        "
        LOCATE y% + 10, 21: PRINT LEFT$(impresora(n%), 40)
    END IF
    y% = y% + 1
NEXT
RETURN


Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Select printer
« Reply #1 on: January 05, 2019, 05:08:52 am »
Another way... (using mouse)


'Printer select by Euklides, 2018
GOSUB printerSelection
IF changeprint$ <> "" THEN PRINT "Active printer is now: " + changeprint$ ELSE PRINT "Printer: no change"
END



'=====================
printerSelection: 'use it as a sub-programme
GOSUB listimp: GOSUB selspecial
IF choixfait$ <> "" AND choixfait$ <> actuimp$ THEN bzz$ = choixfait$: GOSUB PRINTERMAN: changeprint$ = choixfait$
RETURN

'--- printer list---
listimp: FILETMP$ = _CWD$ + "\printy.txt": ord$ = "CMD /C" + "wmic printer get name,default > " + FILETMP$
SHELL _HIDE ord$: _DELAY .5: canal& = FREEFILE
CLOSE canal&: OPEN "i", #canal&, FILETMP$: lx = LOF(canal&): np = 0: actuprint$ = "": autreprint$ = ""
WHILE NOT EOF(canal&): LINE INPUT #canal&, apri1$
    apri$ = "": FOR ypr = 1 TO LEN(apri1$): IF MID$(apri1$, ypr, 1) <> CHR$(0) THEN apri$ = apri$ + MID$(apri1$, ypr, 1)
    NEXT ypr
    IF INSTR(apri$ + "x", "TRUE") > 0 THEN actuprint$ = RIGHT$(apri$, LEN(apri$) - 6): impy$ = actuprint$: actuprint$ = "> " + LTRIM$(RTRIM$(actuprint$))
    IF INSTR(apri$ + "x", "FALSE") > 0 THEN
        apri$ = RIGHT$(apri$, LEN(apri$) - 6): apri$ = LTRIM$(RTRIM$(apri$))
        autreprint$ = autreprint$ + "> " + apri$ + CHR$(10)
    END IF
WEND: CLOSE canal&: KILL FILETMP$
question$ = question$ + "Choose working printer" + CHR$(10)
question$ = question$ + "*" + CHR$(10)
question$ = question$ + " Actual active printer is: " + CHR$(10) + actuprint$ + CHR$(10)
question$ = question$ + " Other printer available: " + CHR$(10) + autreprint$ + CHR$(10)
RETURN

'---printer selection----
selspecial: 'SP  'entree: question+chr$(10)...: 'sortie ChoixFait$
IF RIGHT$(question$, 1) <> CHR$(10) THEN question$ = question$ + CHR$(10)
IF NOOK = 0 THEN question$ = question$ + "   CANCEL" + CHR$(10) + "   OK " + CHR$(10)
COLORAS = 6: COLORBS = 1: NUMREPo = -1: RANDOMIZE TIMER
NELOX = 0: LAX = 0: ima$ = "": QWEST$ = question$: choixfait$ = ""
MSL1: j = INSTR(QWEST$, CHR$(10)): IF j = 0 THEN QWEST$ = question$: question$ = "": GOTO MSL2
NELOX = NELOX + 1: QWEST$ = RIGHT$(QWEST$, LEN(QWEST$) - j):: IF j - 1 > LAX THEN LAX = j - 1
GOTO MSL1
MSL2: j = INSTR(QWEST$, CHR$(10)): IF j = 0 THEN GOTO MSL3
KQQ$ = LEFT$(QWEST$, j - 1) + SPACE$(LAX): question$ = question$ + LEFT$(KQQ$, LAX)
QWEST$ = RIGHT$(QWEST$, LEN(QWEST$) - j): GOTO MSL2
MSL3: MSQH = INT((80 - LAX) / 2): MSQV = INT((25 - NELOX) / 2): MSQH = MSQH + 8 - INT(RND * 8)
FOR x = 0 + MSQV TO NELOX + 1 + MSQV: FOR y = MSQH TO MSQH + LAX + 2:
        ' LOCATE 20, 1: PRINT x; Y; "  "; MSQH; "::lax=:"; LAX; "   " ';question$;"  ";

        w = SCREEN(x, y): ima$ = ima$ + CHR$(w)
        w = SCREEN(x, y, 1) AND 15: ima$ = ima$ + CHR$(w)
        w = SCREEN(x, y, 1) \ 16: ima$ = ima$ + CHR$(w)
NEXT y: NEXT x
MSLV: pxq = 1: COLOR COLORAS, COLORBS: FOR x = 0 TO NELOX + 1
    LOCATE MSQV + x, MSQH
    IF x = 0 THEN PRINT "Ú"; STRING$(LAX, "Ä"); "¿"
    COLOR COLORAS, COLORBS
    IF x > 0 AND x < NELOX + 1 THEN
        UQ$ = MID$(question$, pxq, LAX): PRINT "³";:
        IF NUMREPo = x THEN COLOR COLORBS, COLORAS
        IF LEFT$(UQ$, 1) <> ">" THEN COLOR 7, 0
        PRINT UQ$;: COLOR COLORAS, COLORBS: PRINT "³";: pxq = pxq + LAX
    END IF
    IF x = NELOX + 1 THEN PRINT "À"; STRING$(LAX, "Ä"); "Ù";
NBEW: NEXT x
MSL4: GOSUB souriskey: IF human = 1 AND z$ = CHR$(27) THEN GOTO MSL5
IF human <> 3 THEN GOTO MSL4
IF ysouris < MSQV + 1 THEN GOTO MSL4
IF ysouris > MSQV + NELOX THEN GOTO MSL4
IF xsouris < MSQH + 1 THEN GOTO MSL4
IF xsouris > MSQH + 1 + LAX THEN GOTO MSL4
numz = ysouris - MSQV: 'LOCATE 21, 1: PRINT NUM
numz = (numz - 1) * LAX + 1: repW$ = RTRIM$(LTRIM$(MID$(question$, numz, LAX)))
IF repW$ = "OK" THEN GOTO MSL5
IF repW$ = "CANCEL" THEN choixfait$ = "": GOTO MSL5
IF LEFT$(repW$, 1) <> ">" THEN GOTO MSL4
NUMREPo = ysouris - MSQV: choixfait$ = repW$: IF NOOK = 0 THEN GOTO MSLV ELSE GOTO MSL5
MSL5: w = 1: FOR x = 0 + MSQV TO NELOX + 1 + MSQV: FOR y = MSQH TO MSQH + LAX + 2:
        COLOR ASC(MID$(ima$, w + 1, 1)), ASC(MID$(ima$, w + 2, 1))
        LOCATE x, y: PRINT CHR$(ASC(MID$(ima$, w, 1)));:: w = w + 3
NEXT y: NEXT x:
IF LEN(choixfait$) > 0 THEN choixfait$ = RIGHT$(choixfait$, LEN(choixfait$) - 1): choixfait$ = RTRIM$(LTRIM$(choixfait$))
RETURN

RETURN
'---printer activation
PRINTERMAN: 'entrée avec bzz$="?" pour générer la liste des imprimantes dans sortie avec question$
'avec bzz$="?", on a aussi le nom actuel dans Impy$
' bzz$ avec "nom imprimante" pour la paramétrer
IF bzz$ = "?" THEN GOTO listimp
ord$ = "CMD /C" + "wmic printer where name='" + bzz$ + "' call setdefaultprinter"
SHELL _HIDE ord$
RETURN
'---mouse management
souriskey:
human = 0: z$ = INKEY$: IF z$ <> "" THEN human = 1: RETURN
clicko = 0
IF _MOUSEINPUT THEN
    human = 2: xsouris = INT(_MOUSEX + .5): ysouris = INT(_MOUSEY + .5)
    MOUVSOURI$ = RIGHT$(STR$(ysouris + 100), 2) + "s" + RIGHT$(STR$(xsouris + 100), 2)
    IF _MOUSEBUTTON(1) THEN clicko = 1: human = 3
END IF
RETURN
'=====================
Why not yes ?

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Select printer
« Reply #2 on: May 01, 2020, 08:52:32 am »
Try this sometime as a means of getting printer and sending files to it for printing
Code: QB64: [Select]
  1.  IF _FILEEXISTS("printdialog.ps1") = 0 THEN
  2.         OPEN "printdialog.ps1" FOR OUTPUT AS #12
  3.         PRINT #12, "$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'"
  4.         PRINT #12, "Add-Type -AssemblyName System.Windows.Forms"
  5.         PRINT #12, "$prnDlg=New-Object System.Windows.Forms.PrintDialog"
  6.         PRINT #12, "$prnDlg.ShowDialog() > cancel.txt"
  7.         PRINT #12, "$prnDlg.PrinterSettings.Copies > copies.txt"
  8.         PRINT #12, "$prnDlg.PrinterSettings.PrinterName > printername.txt"
  9.         CLOSE #12
  10.         SHELL _HIDE _DONTWAIT "attrib +h printdialog.ps1"
  11.     END IF
  12.                 SHELL _HIDE "powershell -ExecutionPolicy Bypass -Command " + CHR$(34) + "&" + CHR$(34) + CHR$(34) + _STARTDIR$ + "\printdialog.ps1" + CHR$(34) + CHR$(34) + CHR$(34) 'This dialog returns whatever is selected, regardless of exiting without pressing PRINT
  13.                 _DELAY 0.2 'Makes program wait 0.2 seconds in order to ensure file has been deployed and ready for viewing
  14.                 OPEN "copies.txt" FOR BINARY AS #4 'Opens file to read in data from copies.txt
  15.                 OPEN "printername.txt" FOR BINARY AS #5 'Opens file to read in data from printername.txt
  16.                 OPEN "cancel.txt" FOR BINARY AS #8 'Opens file to read in data from cancel.txt
  17.                 LINE INPUT #4, copies$ 'Reads in data from copies.txt and stores it in the string copies$
  18.                 LINE INPUT #5, printername$ 'Reads in data from printername.txt and stores it in the string printername$
  19.                 LINE INPUT #8, cancel$ 'Reads in data from cancel.txt and stores it in the string cancel$
  20.                 LENGTHCOP = LEN(copies$) 'Stores the length of the copies$ string into an integer called LENGTHCOP
  21.                 LENGTHPN = LEN(printername$) 'Stores the length of the printername$ string into an integer called LENGTHPN
  22.                 LENGTHCAN = LEN(cancel$) 'Stores the length of the cancel$ string into an integer called LENGTHCAN
  23.                 copies$ = RIGHT$(copies$, LENGTHCOP - 3) 'Trims off the first three characters in the copies$ string
  24.                 printername$ = RIGHT$(printername$, LENGTHPN - 3) 'Trims off the first three characters in the printername$ string
  25.                 cancel$ = RIGHT$(cancel$, LENGTHCAN - 3) 'Trims off the first 3 characters in the cancel$ string
  26.                 CLOSE #4
  27.                 CLOSE #5
  28.                 CLOSE #8
  29.                 CLOSE #1
  30.                 IF cancel$ = "Cancel" THEN
  31.                     copies = 0
  32.                 ELSE
  33.                     copies = VAL(copies$) 'Converts string value of copies$ to an integer value for counting
  34.                     DO UNTIL copies = 0 'Performs command below until copies = 0
  35.                         SHELL _HIDE _DONTWAIT "NOTEPAD  /pt " + CHR$(34) + "pathtofile" + CHR$(34) + " " + CHR$(34) + printername$ + CHR$(34) 'Opens NOTEPAD and uses the /pt switch to send file to printer
  36.                         copies = copies - 1 'Subtracts 1 from the copies integer
  37.                     LOOP
  38.                 END IF
  39.             END IF
« Last Edit: May 01, 2020, 08:55:40 am by SpriggsySpriggs »
Shuwatch!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Select printer
« Reply #3 on: May 08, 2020, 11:28:18 am »
All 3 of these programs are great - thanks you guys for same.