Imports System.Runtime.InteropServices Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows フォーム デザイナで生成されたコード " Public Sub New() MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Friend WithEvents Button1 As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(80, 32) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(120, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = "時間がかかる処理" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) Me.ClientSize = New System.Drawing.Size(292, 86) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' フォームを無効にする Me.Enabled = False ' 時間のかかる処理 Dim i As Integer For i = 0 To 199 ' 何からの処理 System.Threading.Thread.Sleep(100) ' WIN32APIを使ってWM_PAINTメッセージのみを処理する Dim msg As New MSG If PeekMessage(msg, 0, WM_PAINT, WM_PAINT, PeekMsgOption.PM_REMOVE) Then DispatchMessage(msg) End If Next i ' フォームを有効に戻す Me.Enabled = True End Sub ' WIN32APIを使ってWindowsメッセージをすべて処理する場合のサンプル・コード ' ////////////////////////////////////////////////// ' Windowsメッセージを処理するための定義 ' なお、.NET Framework 1.1 以降には、似た構造体に ' Message構造体(System.Windows.Forms名前空間)があるが、 ' ここではWIN32APIに準拠した構造体を作成して使用する。 ' WIN32APIのインポート _ Private Shared Function _ PeekMessage( _ ByRef lpMsg As MSG, _ ByVal hwnd As Int32, _ ByVal wMsgFilterMin As Int32, _ ByVal wMsgFilterMax As Int32, _ ByVal wRemoveMsg As PeekMsgOption _ ) As Boolean End Function _ Private Shared Function _ TranslateMessage(ByRef lpMsg As MSG) As Boolean End Function _ Private Shared Function _ DispatchMessage(ByRef lpMsg As MSG) As Int32 End Function ' メッセージの処理方法 Private Enum PeekMsgOption PM_NOREMOVE = 0 ' 処理後、メッセージをキューから削除しない PM_REMOVE ' 処理後、メッセージをキューから削除する  End Enum 'PeekMsgOption ' Windowsメッセージの定義 Private Shared WM_PAINT As Int32 = &HF ' メッセージ構造体 _ Structure MSG Public HWnd As Int32 ' ウィンドウ・ハンドル Public Msg As Int32 ' メッセージID Public WParam As Int32 ' WParamフィールド(メッセージIDごとに異なる) Public LParam As Int32 ' LParamフィールド(メッセージIDごとに異なる) Public Time As Int32 ' 時間 Public Pt As POINTAPI ' カーソル位置(スクリーン座標) End Structure 'MSG _ Structure POINTAPI Public x As Int32 ' x座標 Public y As Int32 ' y座標 End Structure 'POINTAPI End Class