システムカーソルを取得する広告
システムに登録されているカーソル(マウスポインタ)は、Cursorsクラスの静的プロパティで取得できます。 以下に、Cursorsクラスで取得できるシステムカーソルをすべて描画する例を示します。このコードはフォームのクラスに記述されており、カーソルをそのフォームに描画しています。 [VB.NET] 'カーソルを描画するGraphics Dim g As System.Drawing.Graphics = Me.CreateGraphics() 'Cursorsクラスの静的プロパティを取得する Dim cursorsType As Type = GetType(System.Windows.Forms.Cursors) Dim pros As System.Reflection.PropertyInfo() = cursorsType.GetProperties( _ System.Reflection.BindingFlags.Public Or _ System.Reflection.BindingFlags.Static) '描画する位置 Dim drawPoint As New System.Drawing.Point(10, 10) For Each pro As System.Reflection.PropertyInfo In pros 'CursorsクラスのプロパティからCursorオブジェクトを取得する Dim cur As System.Windows.Forms.Cursor = _ DirectCast(pro.GetValue(Nothing, Nothing), System.Windows.Forms.Cursor) 'カーソルを描画する cur.Draw(g, New Rectangle(drawPoint, cur.Size)) 'プロパティの名前を描画する g.DrawString(pro.Name, Me.Font, System.Drawing.Brushes.Black, _ drawPoint.X, drawPoint.Y + cur.Size.Height) 'カーソルの描画位置を変更する drawPoint.X += cur.Size.Width + 40 If drawPoint.X > 500 Then drawPoint.X = 10 drawPoint.Y += cur.Size.Height + CInt(Me.Font.GetHeight()) + 10 End If Next g.Dispose() [C#] //カーソルを描画するGraphics System.Drawing.Graphics g = this.CreateGraphics(); //Cursorsクラスの静的プロパティを取得する Type cursorsType = typeof(System.Windows.Forms.Cursors); System.Reflection.PropertyInfo[] pros = cursorsType.GetProperties( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); //描画する位置 System.Drawing.Point drawPoint = new System.Drawing.Point(10, 10); foreach (System.Reflection.PropertyInfo pro in pros) { //CursorsクラスのプロパティからCursorオブジェクトを取得する System.Windows.Forms.Cursor cur = (System.Windows.Forms.Cursor)pro.GetValue(null, null); //カーソルを描画する cur.Draw(g, new Rectangle(drawPoint, cur.Size)); //プロパティの名前を描画する g.DrawString(pro.Name, this.Font, System.Drawing.Brushes.Black, drawPoint.X, drawPoint.Y + cur.Size.Height); //カーソルの描画位置を変更する drawPoint.X += cur.Size.Width + 40; if (drawPoint.X > 500) { drawPoint.X = 10; drawPoint.Y += cur.Size.Height + (int)this.Font.GetHeight() + 10; } } g.Dispose(); 結果は、次のように表示されます。 以下にCursorsクラスのプロパティを表で示します。なお説明は、MSDNの「Cursors クラス」からの抜粋です。
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。
|
Copyright(C) DOBON!. All rights reserved.
|