SetDefaultPrinter選擇設定印表機 |
缺席
|
t27
中階會員 發表:34 回覆:95 積分:90 註冊:2002-06-19 發送簡訊給我 |
這段程式已使用多年都沒問題
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr)); procedure SetDefaultPrinter1(NewDefPrinter: string); var ResStr: array[0..255] of Char; begin StrPCopy(ResStr, NewdefPrinter); WriteProfileString('windows', 'device', ResStr); StrCopy(ResStr, 'windows'); SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr)); end; 編輯記錄
t27 重新編輯於 2014-03-28 13:58:45, 註解 無‧
|
t27
中階會員 發表:34 回覆:95 積分:90 註冊:2002-06-19 發送簡訊給我 |
用以下程式測試結果,
可改變印表機設定, 但是 SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr)); 這行指令好像變成不歸路,無法執行下一行指令, 也就是說 SetDefaultPrinter(Combobox1.Text);指令執行後,程式就結束了. 以下指令無法執行 ShowMessage('Set Printer ok!'); 不知問題何在? unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IniFiles,UrlMon, StdCtrls, Gauges, ComCtrls, ExtCtrls; type TfrmMain = class(TForm) Panel1: TPanel; OpenDialog1: TOpenDialog; Label1: TLabel; ComboBox1: TComboBox; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmMain: TfrmMain; implementation {$R *.dfm} uses Printers; function GetDefaultPrinter: string; var ResStr: array[0..255] of Char; begin GetProfileString('Windows', 'device', '', ResStr, 255); Result := StrPas(ResStr); end; procedure SetDefaultPrinter(NewDefPrinter: string); var ResStr: array[0..255] of Char; begin StrPCopy(ResStr, NewdefPrinter); WriteProfileString('windows', 'device', ResStr); StrCopy(ResStr, 'windows'); SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr)); end; procedure TfrmMain.FormCreate(Sender: TObject); begin //Fill the combobox with all available printers Combobox1.Items.Clear; Combobox1.Items.AddStrings(Printer.Printers); // Get the Default Printer: label1.Caption := Format('Default Printer: %s',[GetDefaultPrinter]); end; procedure TfrmMain.Button2Click(Sender: TObject); begin //Set the selected printer in the combobox as default printer if Combobox1.Text <> '' then begin SetDefaultPrinter(Combobox1.Text); ShowMessage('Set Printer ok!'); end else ShowMessage('Choose a Printer first !'); end; |
t27
中階會員 發表:34 回覆:95 積分:90 註冊:2002-06-19 發送簡訊給我 |
|
alansunij
一般會員 發表:0 回覆:1 積分:0 註冊:2004-12-06 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |