ウィンドウコントロールコンポーネントにスクロールバーを表示する - C#

ウィンドウコントロールのコンポーネントにスクロールバーを表示するコードを紹介します。

概要

ウィンドウコントロールにスクロールバーを表示する場合は、CreateParams プロパティをオーバーライドし、 CreateParams パラメータのStyle にWS_VSCROLL WS_HSCROLL を追加します。

プログラム例

ウィンドウコントロールのコンポーネントを作成します。 作成手順はこちらの記事を参照してください。

コード

コンポーネントのコードを以下のコードに変更します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace WindowControlWithScrollBar
{
  public partial class MyComponentWithScrollBar : Control
  {
    // window style constants for scrollbars
    public const int WS_VSCROLL = 0x00200000;
    public const int WS_HSCROLL = 0x00100000;

    public MyComponentWithScrollBar()
    {
      InitializeComponent();
    }

    public MyComponentWithScrollBar(IContainer container)
    {
      container.Add(this);

      InitializeComponent();
    }

    protected override CreateParams CreateParams {
      get
      {
        CreateParams cp = base.CreateParams;
        cp.Style |= WS_HSCROLL + WS_VSCROLL;
        return cp;
      }
    }

  }
}

フォームへの配置

プロジェクトをビルドします。ツールパレットに"MyComponentWithScrollBar" のコントロールが表示されますので、 フォームにドラッグ&ドロップして配置します。
スクロールバーが表示された状態で配置されます。

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。コントロールにスクロールバーが表示されていることが確認できます。

補足:縦、横どちらかのスクロールバーを表示する場合

縦スクロールバーのみを表示する場合は、StyleにWS_VSCROLLのみを設定します。
    protected override CreateParams CreateParams {
      get
      {
        CreateParams cp = base.CreateParams;
        cp.Style |= WS_VSCROLL;
        return cp;
      }
    }


横スクロールバーのみを表示する場合は、StyleにWS_HSCROLLのみを設定します。
    protected override CreateParams CreateParams {
      get
      {
        CreateParams cp = base.CreateParams;
        cp.Style |= WS_HSCROLL;
        return cp;
      }
    }


補足:SecurityPermission について

以前のバージョンでは、SecurityPermissionオブジェクトを作成し、Demand()メソッドを呼び出すコードがありましたが、最新バージョンでは旧形式のワーニングが発生します。
以下のコードでは、次のワーニングが発生します。
ワーニングメッセージ
警告:SYSLIB0003
'SecurityPermission' は旧形式です ('Code Access Security is not supported or honored by the runtime.')

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;

namespace WinformVisualComponent
{
  public partial class VisualComponent : Control
  {
    // window style constants for scrollbars
    public const int WS_VSCROLL = 0x00200000;
    public const int WS_HSCROLL = 0x00100000;

    public VisualComponent()
    {
      InitializeComponent();
    }

    public VisualComponent(IContainer container)
    {
      container.Add(this);

      InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
      get
      {
        new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

        CreateParams cp = base.CreateParams;
        cp.Style |= WS_HSCROLL + WS_VSCROLL;
        return cp;
      }
    }
  }
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2011-11-09
iPentec all rights reserverd.