この記事は2020年08月29日に投稿しました。
目次
リンク
1. はじめに
こんにちは、iOSのエディタアプリPWEditorの開発者の二俣です。
今回は業務で使用しているC++/CLIで印刷する文字列の長さを取得する方法についてです。
2. C++/CLIで印刷する文字列の長さを取得する
C++/CLIで印刷する文字列の長さを取得するには、TextRendererクラスのMeasureTextを使用します。
実装例
MainForm.h
#pragma once namespace CLIMeasureText { 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; // 印刷のため追加します。 /// <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, 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, 70); 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(); // イベントハンドラを登録します。 printDocument->PrintPage += gcnew PrintPageEventHandler(this, &MainForm::TextPrintPageEventHandler); // 印刷します(イベントハンドラが呼び出されます) printDocument->Print(); } private: // 印刷します。 void MainForm::TextPrintPageEventHandler(Object^ sender, PrintPageEventArgs^ e) { // 文字列の出力で使用するフォントと文字色を指定します。 System::Drawing::Font^ font = gcnew System::Drawing::Font("MS UI Gothic", 16.0f); Brush^ brush = gcnew SolidBrush(Color::Black); // 印刷する文字列の長さを取得します。 String^ text = "Hello world!"; System::Drawing::Size size = TextRenderer::MeasureText(text, font); // 今回のsize.Widthは75になります。 Console::WriteLine(size.Width); // 文字列を印刷します。 e->Graphics->DrawString(text, font, brush, PointF(0, 0)); // ブラシとフォントを削除します。 delete brush; delete font; } }; }
API Reference
TextRendererクラス
[MeasureText][MeasureText]メソッド
3. おわりに
印刷の場合、印刷位置は自分で計算する必要があります。
そのため出力する文字列の長さを取得する必要があります。
リンク
紹介している一部の記事のコードは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