警告:SYSLIB0003
'SecurityPermission' は旧形式です ('Code Access Security is not supported or honored by the runtime.')
ウィンドウコントロールのコンポーネントにスクロールバーを表示するコードを紹介します。
ウィンドウコントロールにスクロールバーを表示する場合は、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オブジェクトを作成し、Demand()
メソッドを呼び出すコードがありましたが、最新バージョンでは旧形式のワーニングが発生します。
以下のコードでは、次のワーニングが発生します。
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;
}
}
}
}