C++ > CLI:オーナードロー

オーナードローとは、項目の描画をプログラムで変更する事を
言います。
今回はリストボックスの項目を偶数行と奇数行で背景色を
変えてみました。

MyForm.cpp

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include "MyForm.h"

using namespace Project1;

[STAThreadAttribute]
int main(){
 MyForm ^form1 = gcnew MyForm;
 form1->ShowDialog();
 return 0;
}

MyForm.h

#pragma once

namespace Project1 {

 using namespace System;
 using namespace System::ComponentModel;
 using namespace System::Collections;
 using namespace System::Windows::Forms;
 using namespace System::Data;
 using namespace System::Drawing;

 /// <summary>
 /// MyForm の概要
 /// </summary>
 public ref class MyForm : public System::Windows::Forms::Form
 {
 public:
  MyForm(void)
  {
   InitializeComponent();
   //
   //TODO: ここにコンストラクター コードを追加します
   //
   listBox1->DrawMode = DrawMode::OwnerDrawFixed;
   listBox1->DrawItem += gcnew DrawItemEventHandler( this, &MyForm::listBox_DrawItem);
  }

 protected:
  /// <summary>
  /// 使用中のリソースをすべてクリーンアップします。
  /// </summary>
  ~MyForm()
  {
   if (components)
   {
    delete components;
   }
  }
 private: System::Windows::Forms::ListBox^  listBox1;
 protected:


 protected:

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->listBox1 = (gcnew System::Windows::Forms::ListBox());
   this->SuspendLayout();
   //
   // listBox1
   //
   this->listBox1->FormattingEnabled = true;
   this->listBox1->ItemHeight = 12;
   this->listBox1->Items->AddRange(gcnew cli::array< System::Object^  >(10) {L"一番目", L"二番目", L"三番目", L"四番目", L"五番目", L"六番目",
    L"七番目", L"八番目", L"九番目", L"十番目"});
   this->listBox1->Location = System::Drawing::Point(12, 12);
   this->listBox1->Name = L"listBox1";
   this->listBox1->Size = System::Drawing::Size(260, 232);
   this->listBox1->TabIndex = 0;
   //
   // MyForm
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->Controls->Add(this->listBox1);
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   this->ResumeLayout(false);

  }
#pragma endregion
  // オーナードローの本体部分
  void listBox_DrawItem(System::Object^ sender, DrawItemEventArgs^ e)
    {
        // 既定の色で背景を塗り潰す
        e->DrawBackground();

  Brush^ brush = nullptr;
        // 偶数番目 (プログラム的には奇数番目) の項目であれば
        if (e->Index % 2 == 1)
        {
            //偶数なら色を変える
   brush = Brushes::AliceBlue;
  }else{
   brush = Brushes::White;
  }
  //選択中の項目はハイライトに
  if ((e->State & DrawItemState::Selected) == DrawItemState::Selected)brush = SystemBrushes::Highlight;
  // 用意したブラシで塗り潰す
  e->Graphics->FillRectangle(brush, e->Bounds);

        // 描画中の項目のテキストを取得
  String^ text = safe_cast<ListBox^>(sender)->Items[e->Index]->ToString();

        Brush^ foreBrush = gcnew SolidBrush(e->ForeColor);

        // テキストを描画
  e->Graphics->DrawString(text, e->Font, foreBrush, e->Bounds);


        // フォーカスを示す四角形を描画
        e->DrawFocusRectangle();
    }

 };
}

 

 

 

 

 

 

最終更新:2013年10月10日 21:29
添付ファイル