Mudar impressora padrão via comando em win98
Amigos, uso o código abaixo pra mudar impressora padrao para que na hora da impressao o usuario nao tenha que ficar selecionando impressora e nao corra o risco de imprimir com a impressora errada apos terminada a impressao eu volto a impressora padrao normal, mas isso só funciona no winxp gostaria que funcionasse tambem no win98 alguem pode me ajudar com isso
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]
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
Curtidas 0
Respostas
Faelcavalcanti
10/03/2005
Cara não sei se vai resolver o teu problema mas talvez sirva alternando entre os nome das impressoras. Você pode listar todas as disponíveis e colocar este código para especificar qual é a padrão a partir do nome
Espero ter ajudado!
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
Marcusbraga
10/03/2005
faelcavalcanti
:?:
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
:?:
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
Faelcavalcanti
10/03/2005
Cara, vi que era bem parecido, mas antes pensava que se referia a outra coisa por conta no argumento, no seu caso o parâmetro diferente do meu.
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!!!
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
Rômulo Barros
10/03/2005
galera, com o código abaixo fiz o seguinte:
Salvo em um arquivo txt o numero da impressora matricial e jato de tinta.
Exemplo: Um terminal de caixa meu imprime em uma matricial, mas o mesmo terminal emite boleto bancário quando a venda é em duplicata. Então nas transações normais devo usar a matricial e no boleto usar a jato de tinta.
Coloquei em um arquivo texto o numero (index) da impressora matricial (exemplo: 0) e da jato de tinta (exemplo: 1).
Quando for imprimir o boleto ele coloca a impressora 1 como padrão e imprime, depois voltando para a impressora 0.
Segue:
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