この記事は2020年08月30日に投稿しました。
目次
リンク
1. はじめに
こんにちは、iOSのエディタアプリPWEditorの開発者の二俣です。
今回は業務で使用しているC++/CLIでインストールされているプリンタ名を取得する方法についてです。
2. C++/CLIでインストールされているプリンタ名を取得する
C++/CLIでインストールされているプリンタ名を取得するには、PrinterSettingsクラスのInstalledPrintersプロパティを使用します。
実装例
MainForm.h
#pragma once namespace CLIInstalledPrinters { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Text; using namespace System::Drawing::Printing; // 印刷のため追加します。 /// <summary> /// MainForm の概要 /// </summary> public ref class MainForm : public System::Windows::Forms::Form { public: MainForm(void) { InitializeComponent(); // //TODO: ここにコンストラクター コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~MainForm() { if (components) { delete components; } } private: System::Windows::Forms::Button^ PrintButton; protected: protected: private: /// <summary> /// 必要なデザイナー変数です。 /// </summary> System::ComponentModel::Container^ components; #pragma region Windows Form Designer generated code /// <summary> /// デザイナー サポートに必要なメソッドです。このメソッドの内容を /// コード エディターで変更しないでください。 /// </summary> void InitializeComponent(void) { this->PrintButton = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // PrintButton // this->PrintButton->Location = System::Drawing::Point(112, 24); this->PrintButton->Name = L"PrintButton"; this->PrintButton->Size = System::Drawing::Size(75, 23); this->PrintButton->TabIndex = 0; this->PrintButton->Text = L"印刷"; this->PrintButton->UseVisualStyleBackColor = true; this->PrintButton->Click += gcnew System::EventHandler(this, &MainForm::PrintButton_Click); // // MainForm // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 66); this->Controls->Add(this->PrintButton); this->Name = L"MainForm"; this->Text = L"MainForm"; this->ResumeLayout(false); } #pragma endregion // 印刷ボタンが押下された時に呼び出されます。 private: System::Void PrintButton_Click(System::Object^ sender, System::EventArgs^ e) { // PrintDocumentオブジェクトを生成します。 PrintDocument^ printDocument = gcnew PrintDocument(); // PrinterSettingsオブジェクトを取得します。 PrinterSettings^ printerSettings = printDocument->PrinterSettings; // PrinterSettingsオブジェクトからPCにインストールされたプリンタの名前を取得し、表示します。 StringBuilder^ sb = gcnew StringBuilder(); for each (String ^ name in printerSettings->InstalledPrinters) { sb->Append(name); sb->Append(Environment::NewLine); } MessageBox::Show(sb->ToString()); } }; }
API Reference
InstalledPrintersプロパティ
3. おわりに
印刷に使用するプリンタを選択するために、プリンタ名を取得する必要があります。
InstalledPrintersプロパティでインストールされているプリンタ名が取得できます。
リンク
紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。
私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッドOS [Bi-OS][Bi-OS]は高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOSモバイルアプリやウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。
また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。
EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688