Fórum Mudar impressora padrão via comando em win98 #271764
10/03/2005
0
desde ja agradeço
delphi 7.0 e firebird
var
I: Integer;
Device : PChar;
Driver : Pchar;
Port : Pchar;
HdeviceMode: Thandle;
aPrinter : TPrinter;
begin
Printer.PrinterIndex := -1;
getmem(Device, 255);
getmem(Driver, 255);
getmem(Port, 255);
aPrinter := TPrinter.create;
for I := 0 to Printer.printers.Count-1 do
begin
if Printer.printers[i] = PrinterName then
begin
aprinter.printerindex := i;
aPrinter.getprinter
(device, driver, port, HdeviceMode);
StrCat(Device, ´,´);
StrCat(Device, Driver );
StrCat(Device, Port );
WriteProfileString(´windows´, ´device´, Device);
StrCopy( Device, ´windows´ );
SendMessage(HWND_BROADCAST, WM_DDE_INITIATE, 0, Longint(@Device));
end;
end;
Freemem(Device, 255);
Freemem(Driver, 255);
Freemem(Port, 255);
aPrinter.Free;
end;[/code]
Marcusbraga
Curtir tópico
+ 0Posts
10/03/2005
Faelcavalcanti
procedure SetDefaultPrinter(PrinterName: String); var I: Integer; Device : PChar; Driver : Pchar; Port : Pchar; HdeviceMode: Thandle; aPrinter : TPrinter; begin Printer.PrinterIndex := -1; getmem(Device, 255); getmem(Driver, 255); getmem(Port, 255); aPrinter := TPrinter.create; for I := 0 to Printer.printers.Count-1 do begin if Printer.printers[i] = PrinterName then begin aprinter.printerindex := i; aPrinter.getprinter (device, driver, port, HdeviceMode); StrCat(Device, ´,´); StrCat(Device, Driver ); StrCat(Device, Port ); WriteProfileString(´windows´, ´device´, Device); StrCopy( Device, ´windows´ ); SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@Device)); end; end; Freemem(Device, 255); Freemem(Driver, 255); Freemem(Port, 255); aPrinter.Free; end;
Espero ter ajudado!
Gostei + 0
10/03/2005
Marcusbraga
:?:
O código q vc me enviou é praticamente identico o q eu tenho, a não ser
por essa linha
no meu ta
SendMessage(HWND_BROADCAST, WM_DDE_INITIATE, 0, Longint(@Device));
e no seu
SendMessage(HWND_BROADCAST, WM_WININICHANGE,
0, Longint(@Device));
trocando apenas o ´WM_DDE_INITIATE´ POR ´WM_WININICHANGE´
não sei o significado destes comandos e infelismente não compilou pra eu testar, ele não reconhece o ´WM_WININICHANGE´ vc pode me mandar o nome do ´.dcu´ que ele usa?
obrigado
Gostei + 0
10/03/2005
Faelcavalcanti
Infelizmente não estou com a ´.dcu´, mas tenta a SysUtils, talvez seja uma constante de referência a ela.
Acredito que seja variável de referência ao windows. Sobre o valor não sei mas tu pode tentar traduzir neste tópico abaixo.
[url]http://www.guiadodelphi.com.br/ler.php?codigo=849[/url]
:wink:
Falow!!!
Gostei + 0
10/03/2005
Rômulo Barros
AssignFile(Arquivo,´C:\Sigma\Controle\Impressora.sgm´); Reset(Arquivo); Readln(Arquivo,Temp2); - Matricial Readln(Arquivo,Temp3); - Jato de tinta CloseFile(Arquivo); ChangeDefaultPrinter(Temp3); - Jato de tinta vira padrão ... Operações de impressão ChangeDefaultPrinter(Temp2); - Matricial vira padrão. Poderia usar simplesmente ChangeDefaultPrinter(1); - Jato de tinta vira padrão ... Operações de impressão ChangeDefaultPrinter(0); - Matricial vira padrão. Mas e se a ordem das impressoras forem diferentes? Configuro o txt e pronto. Só fechando, não usei o TPrintSetup, pois a operação tinha de ser transparente para o funcionário, para evitar erros. A procedure: procedure ChangeDefaultPrinter(APrinterIndex: Integer); var Device: array [0..255] of char; Driver: array [0..255] of char; Port: array [0..255] of char; hDeviceMode: THandle; begin Printer.PrinterIndex:=APrinterIndex; Printer.GetPrinter(Device,Driver,Port,hDeviceMode); StrCat(Device,´,´); StrCat(Device,Driver ); StrCat(Device,´,´); StrCat(Device,Port ); WriteProfileString(´windows´,´device´,Device ); StrCopy(Device,´windows´); SendMessage(HWND_BROADCAST,WM_WININICHANGE,0,longint(@Device)); end;
... Operações de impressão
ChangeDefaultPrinter(Temp2); - Matricial vira padrão.
Poderia usar simplesmente
ChangeDefaultPrinter(1); - Jato de tinta vira padrão
[u:2308580295][color=red:2308580295][b:2308580295]MAIS OUTRO EXEMPLO:[/b:2308580295][/color:2308580295][/u:2308580295]
procedure SetDefaultPrinter; var Device : array[0..cchDeviceName] of char; Driver : array[0..MAX_PATH] of char; DriverPort : string; Port : array[0..MAX_PATH] of char; hDMode : THandle; s : array[0..64] of char; WinIni : TIniFile; WinIniFileName : array[0..MAX_PATH] of char; begin if PrinterSetupDialog.Execute then begin Printer.GetPrinter(@Device, @Driver, @Port, hDMode); // For some reason "Driver" is never defined here by GetPrinter. // Let´s get "Driver,Port" from the Win.INI file "Devices" section GetWindowsDirectory(WinIniFileName, SizeOf(WinIniFileName)); StrCat(WinIniFileName, ´\win.ini´); WinIni := TIniFile.Create(WinIniFileName); try // Lookup Driver,Port in INI file "Devices" section DriverPort := WinIni.ReadString(´devices´, Device, ´´); // Update INI "Windows" section -- this is the Windows defaultprinter WinIni.WriteString(´windows´, ´device´, Device + ´,´ + DriverPort) finally WinIni.Free end; // Flush INI cache WritePrivateProfileString(NIL, NIL, NIL, WinIniFileName); // Broadcast system wide message about win.ini change s := ´windows´; SendMessage(HWND_BROADCAST, WM_WININICHANGE,0, Cardinal(@s)); end; end;
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)