プログラムを書こう!

実務や自作アプリ開発で習得した役に立つソフトウェア技術情報を発信するブログ

C++/CLIでプリンタでサポートされている用紙サイズを取得する

この記事は2020年09月01日に投稿しました。

f:id:paveway:20190914064630j:plain

目次

  1. はじめに
  2. C++/CLIでプリンタでサポートされている用紙サイズを取得する
  3. おわりに

1. はじめに

こんにちは、iOSのエディタアプリPWEditorの開発者の二俣です。
今回は業務で使用しているC++/CLIでプリンタでサポートされている用紙サイズを取得する方法についてです。

目次へ

2. C++/CLIでプリンタでサポートされている用紙サイズを取得する

C++/CLIでプリンタでサポートされている用紙サイズを取得するには、PrinterSettingsクラスのPagerSizesプロパティを参照します。

実装例

MainForm.h
#pragma once

namespace CLIPrintPagerSize {

    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::Drawing::Printing;
    using namespace System::Text;

    /// <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(104, 16);
            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, 59);
            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 = gcnew PrintDocument();
        PrinterSettings^ printerSettings = printDocument->PrinterSettings;
        StringBuilder^ sb = gcnew StringBuilder();
        for each(PaperSize^ pagerSize in printerSettings->PaperSizes)
        {
            sb->Append(pagerSize->PaperName);
            sb->Append(Environment::NewLine);
        }
        MessageBox::Show(sb->ToString(), "用紙サイズ");
    }
    };
}

API Reference

[PageSettings][PageSettings]クラス

[PagerSizes][PagerSizes]プロパティ

目次へ

3. おわりに

業務でプリンタを使用する場合、用紙サイズは大体A4かB5を使用すると思います。
ただたまに違うサイズにする場合もあると思います。
その場合に備えてプリンタがサポートしている用紙サイズを取得しておくと安全です。

紹介している一部の記事のコードは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

目次へ