如何选择打印机?? 请问我现在有两台打印机安装在机器上。我改如何在程序中实现两台打印机之间的切换。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 procedure TForm1.Button1Click(Sender: TObject);var Device: array[0..255] of Char; Driver: array[0..255] of char; Port: array[0..255] of char; s : array[0..255] of Char; hDeviceMode: THandle;begin //messagebeep(2); beep(); Printer.PrinterIndex := ComboBox1.ItemIndex; Printer.GetPrinter (Device, Driver, Port, hDeviceMode); StrCopy (s, Device); StrCat (s, ','); StrCat (s, Driver); StrCat (s, ','); StrCat (s, Port); WriteProfileString ('windows', 'device', s); StrCopy (s, 'windows'); SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s)); ComboBox1.Items := Printer.Printers; {populates ComboBox} ComboBox1.ItemIndex := Printer.PrinterIndex; end;procedure TForm1.ComboBox1Change(Sender: TObject);var Device: array[0..255] of Char; Driver: array[0..255] of char; Port: array[0..255] of char; s : array[0..255] of Char; hDeviceMode: THandle;begin Printer.PrinterIndex := ComboBox1.ItemIndex; Printer.GetPrinter (Device, Driver, Port, hDeviceMode); StrCopy (s, Device); StrCat (s, ','); StrCat (s, Driver); StrCat (s, ','); StrCat (s, Port); WriteProfileString ('windows', 'device', s); StrCopy (s, 'windows'); SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));end;上述代码为改变默认打印机 在表单上添加一个下拉控件,然后在表单的create事件中添加代码:全部代码如下: procedure Tyjkcpddb_form.FormCreate(Sender: TObject);begin { 取得打印机名称列表,并显示缺省的打印机名称 } combobox1.Items.Assign(Printer.Printers); combobox1.Text := Printer.Printers[Printer.PrinterIndex];end;procedure Tyjkcpddb_form.ComboBox1Change(Sender: TObject);var IniFile: TIniFile; TempStr1, TempStr2: String; WinIniFileName : array[0..MAX_PATH] of char; S: array[0..64] of char;begin with Printer do begin //获得所选打印机的索引号 PrinterIndex := combobox1.ItemIndex; //把其名称存在临时字符串中 TempStr1 := Printers[PrinterIndex]; //删除打印机名中不必要的部分 System.Delete(TempStr1, Pos(' on ', TempStr1), Length(TempStr1)); GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName)); //获取系统路径 StrCat(WinIniFileName, '\win.ini'); IniFile := TIniFile.Create(WinIniFileName); //创建INI文件 try TempStr2 := IniFile.ReadString('Devices', TempStr1, ''); // 获得原先缺省的打印机名称 IniFile.WriteString('windows', 'device', TempStr1 + ',' + TempStr2); //写入新的默认打印机名称 StrCopy(S, 'windows'); SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@S)); //通知Windows默认打印机已经改变 finally IniFile.Free; end; end;最后别忘了在uses中添加: IniFiles, Printers; 关于调用无模式窗体的问题,请指教? 消息处理问题 强烈要求你们Delphi版将这种人渣封杀!!!!!!!!! 谁还遇到过这个问题:函数放在dll中就运行出错?? 1 100分征解:与TOleContainer有关 像ADOConnection的ConnectionString的属性编辑器那样的窗体怎么做? delphi中怎样不连接数据库直接处理表格? 在Delphi+Paradox7.0中如何用select查部分记录存到另一个文件中(*.mb) 怎样从4字节的integer变量中获取高字和低字? 我想让Panel可改变大小,该怎么做? 单机版数据库软件转换为网络版数据库软件有简单的办法吗?
var
Device: array[0..255] of Char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
s : array[0..255] of Char;
hDeviceMode: THandle;begin
//messagebeep(2);
beep();
Printer.PrinterIndex := ComboBox1.ItemIndex;
Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
StrCopy (s, Device);
StrCat (s, ',');
StrCat (s, Driver);
StrCat (s, ',');
StrCat (s, Port);
WriteProfileString ('windows', 'device', s);
StrCopy (s, 'windows');
SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
ComboBox1.Items := Printer.Printers; {populates ComboBox}
ComboBox1.ItemIndex := Printer.PrinterIndex;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
var
Device: array[0..255] of Char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
s : array[0..255] of Char;
hDeviceMode: THandle;begin Printer.PrinterIndex := ComboBox1.ItemIndex;
Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
StrCopy (s, Device);
StrCat (s, ',');
StrCat (s, Driver);
StrCat (s, ',');
StrCat (s, Port);
WriteProfileString ('windows', 'device', s);
StrCopy (s, 'windows');
SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
end;
上述代码为改变默认打印机
全部代码如下:
procedure Tyjkcpddb_form.FormCreate(Sender: TObject);
begin
{ 取得打印机名称列表,并显示缺省的打印机名称 }
combobox1.Items.Assign(Printer.Printers);
combobox1.Text := Printer.Printers[Printer.PrinterIndex];
end;procedure Tyjkcpddb_form.ComboBox1Change(Sender: TObject);
var
IniFile: TIniFile;
TempStr1, TempStr2: String;
WinIniFileName : array[0..MAX_PATH] of char;
S: array[0..64] of char;
begin
with Printer do
begin
//获得所选打印机的索引号
PrinterIndex := combobox1.ItemIndex;
//把其名称存在临时字符串中
TempStr1 := Printers[PrinterIndex];
//删除打印机名中不必要的部分
System.Delete(TempStr1, Pos(' on ', TempStr1), Length(TempStr1));
GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName));
//获取系统路径
StrCat(WinIniFileName, '\win.ini');
IniFile := TIniFile.Create(WinIniFileName);
//创建INI文件
try
TempStr2 := IniFile.ReadString('Devices', TempStr1, '');
// 获得原先缺省的打印机名称
IniFile.WriteString('windows', 'device', TempStr1 + ',' + TempStr2);
//写入新的默认打印机名称
StrCopy(S, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@S));
//通知Windows默认打印机已经改变
finally
IniFile.Free;
end;
end;最后别忘了在uses中添加:
IniFiles, Printers;