トレーニング
モジュール
Windows 開発プロジェクトに最適なアプリケーション フレームワークを選ぶ - Training
Windows 開発者には、Windows で実行されるアプリケーションを作成するためのさまざまなオプションがあります。 このモジュールでは、Windows 開発に使用できる Windows UI フレームワークを紹介します。 また、アプリケーションに最適なフレームワークを選ぶ方法に関するガイダンスも提供します。
このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
通常、1 回の クリック ユーザー インターフェイス アクションが開始され、ダブルクリック アクションが拡張されます。 たとえば、通常、1 回のクリックで項目を選択し、ダブルクリックすると選択した項目が編集されます。 ただし、Windows フォームのクリック イベントは、DoubleClick または MouseDoubleClick イベントに関連付けられたアクションの前に、Click または MouseClick イベントに関連付けられたアクションが実行されるため、クリックとダブルクリックが互換性のないアクションを実行するシナリオに簡単には対応できません。 このトピックでは、この問題に対する 2 つの解決策を示します。
1 つの解決策は、ダブルクリック イベントを処理し、クリック イベントの処理でアクションをロールバックすることです。 まれに、MouseDown イベントを処理し、SystemInformation クラスの DoubleClickTime プロパティと DoubleClickSize プロパティを使用して、クリックとダブルクリックの動作をシミュレートする必要がある場合があります。 クリックの間隔を測定し、DoubleClickTime の値に達する前に 2 回目のクリックが発生し、クリックが DoubleClickSizeで定義された四角形内にある場合は、ダブルクリック アクションを実行します。それ以外の場合は、クリック アクションを実行します。
操作しているコントロールに標準のダブルクリック動作があることを確認します。 有効でない場合は、SetStyle メソッドを使用してコントロールを有効にします。 ダブルクリック イベントを処理し、クリック アクションとダブルクリック アクションをロールバックします。 次のコード例では、ダブルクリックを有効にしてカスタム ボタンを作成する方法と、ダブルクリック イベント処理コードでクリック アクションをロールバックする方法を示します。
このコード例では、ダブルクリックを有効にする新しいボタン コントロールを使用します。
public partial class DoubleClickButton : Button
{
public DoubleClickButton()
{
// Set the style so a double click event occurs.
SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
}
}
Public Class DoubleClickButton : Inherits Button
Public Sub New()
SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, True)
End Sub
End Class
次のコードは、フォームが新しいボタン コントロールのクリックまたはダブルクリックに基づいて罫線のスタイルを変更する方法を示しています。
public partial class Form1 : Form
{
private FormBorderStyle _initialStyle;
private bool _isDoubleClicking;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_initialStyle = this.FormBorderStyle;
var button1 = new DoubleClickButton();
button1.Location = new Point(50, 50);
button1.Size = new Size(200, 23);
button1.Text = "Click or Double Click";
button1.Click += Button1_Click;
button1.DoubleClick += Button1_DoubleClick;
Controls.Add(button1);
}
private void Button1_DoubleClick(object sender, EventArgs e)
{
// This flag prevents the click handler logic from running
// A double click raises the click event twice.
_isDoubleClicking = true;
FormBorderStyle = _initialStyle;
}
private void Button1_Click(object sender, EventArgs e)
{
if (_isDoubleClicking)
_isDoubleClicking = false;
else
FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
}
Partial Public Class Form1
Private _initialStyle As FormBorderStyle
Private _isDoubleClicking As Boolean
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim button1 As New DoubleClickButton
_initialStyle = FormBorderStyle
button1.Location = New Point(50, 50)
button1.Size = New Size(200, 23)
button1.Text = "Click or Double Click"
AddHandler button1.Click, AddressOf Button1_Click
AddHandler button1.DoubleClick, AddressOf Button1_DoubleClick
Controls.Add(button1)
End Sub
Private Sub Button1_DoubleClick(sender As Object, e As EventArgs)
' This flag prevents the click handler logic from running
' A double click raises the click event twice.
_isDoubleClicking = True
FormBorderStyle = _initialStyle
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
If _isDoubleClicking Then
_isDoubleClicking = False
Else
FormBorderStyle = FormBorderStyle.FixedToolWindow
End If
End Sub
End Class
MouseDown イベントを処理し、SystemInformation プロパティと Timer コンポーネントを使用してクリック間の場所と期間を決定します。 クリックまたはダブルクリックのどちらが行われるかに応じて、適切なアクションを実行します。 次のコード例は、これを行う方法を示しています。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace project
{
public partial class Form2 : Form
{
private DateTime _lastClick;
private bool _inDoubleClick;
private Rectangle _doubleClickArea;
private TimeSpan _doubleClickMaxTime;
private Action _doubleClickAction;
private Action _singleClickAction;
private Timer _clickTimer;
public Form2()
{
InitializeComponent();
_doubleClickMaxTime = TimeSpan.FromMilliseconds(SystemInformation.DoubleClickTime);
_clickTimer = new Timer();
_clickTimer.Interval = SystemInformation.DoubleClickTime;
_clickTimer.Tick += ClickTimer_Tick;
_singleClickAction = () => MessageBox.Show("Single clicked");
_doubleClickAction = () => MessageBox.Show("Double clicked");
}
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (_inDoubleClick)
{
_inDoubleClick = false;
TimeSpan length = DateTime.Now - _lastClick;
// If double click is valid, respond
if (_doubleClickArea.Contains(e.Location) && length < _doubleClickMaxTime)
{
_clickTimer.Stop();
_doubleClickAction();
}
return;
}
// Double click was invalid, restart
_clickTimer.Stop();
_clickTimer.Start();
_lastClick = DateTime.Now;
_inDoubleClick = true;
_doubleClickArea = new Rectangle(e.Location - (SystemInformation.DoubleClickSize / 2),
SystemInformation.DoubleClickSize);
}
private void ClickTimer_Tick(object sender, EventArgs e)
{
// Clear double click watcher and timer
_inDoubleClick = false;
_clickTimer.Stop();
_singleClickAction();
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form2
Private _lastClick As Date
Private _inDoubleClick As Boolean
Private _doubleClickArea As Rectangle
Private _doubleClickMaxTime As TimeSpan
Private _singleClickAction As Action
Private _doubleClickAction As Action
Private WithEvents _clickTimer As Timer
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_doubleClickMaxTime = TimeSpan.FromMilliseconds(SystemInformation.DoubleClickTime)
_clickTimer = New Timer()
_clickTimer.Interval = SystemInformation.DoubleClickTime
_singleClickAction = Sub()
MessageBox.Show("Single click")
End Sub
_doubleClickAction = Sub()
MessageBox.Show("Double click")
End Sub
End Sub
Private Sub Form2_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
If _inDoubleClick Then
_inDoubleClick = False
Dim length As TimeSpan = Date.Now - _lastClick
' If double click is valid, respond
If _doubleClickArea.Contains(e.Location) And length < _doubleClickMaxTime Then
_clickTimer.Stop()
Call _doubleClickAction()
End If
Return
End If
' Double click was invalid, restart
_clickTimer.Stop()
_clickTimer.Start()
_lastClick = Date.Now
_inDoubleClick = True
_doubleClickArea = New Rectangle(e.Location - (SystemInformation.DoubleClickSize / 2),
SystemInformation.DoubleClickSize)
End Sub
Private Sub SingleClickTimer_Tick(sender As Object, e As EventArgs) Handles _clickTimer.Tick
' Clear double click watcher and timer
_inDoubleClick = False
_clickTimer.Stop()
Call _singleClickAction()
End Sub
End Class
.NET Desktop feedback に関するフィードバック
.NET Desktop feedback はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。
トレーニング
モジュール
Windows 開発プロジェクトに最適なアプリケーション フレームワークを選ぶ - Training
Windows 開発者には、Windows で実行されるアプリケーションを作成するためのさまざまなオプションがあります。 このモジュールでは、Windows 開発に使用できる Windows UI フレームワークを紹介します。 また、アプリケーションに最適なフレームワークを選ぶ方法に関するガイダンスも提供します。