2014年6月15日 星期日

vb.net form focus

參考引用來源:How to return keyboard focus to topmost form?
--
真是找了好久!! 這方法只有:讚!!

 C#.NET:

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
static extern IntPtr GetForegroundWindow();


static void restoreKbFocus(IntPtr hWnd)
{
      if (GetForegroundWindow() != hWnd)
      {
           SetForegroundWindow(hWnd);
      }
}

void timer_Tick(object sender, EventArgs eArgs)
{
      if (sender == timer)
      {
            restoreKbFocus(this.Handle);
      }
}

VB.NET:

Imports System.Runtime.InteropServices
_
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function
_
Private Shared Function GetForegroundWindow() As IntPtr
End Function

Private Shared Sub restoreKbFocus(hWnd As IntPtr)
If GetForegroundWindow() <> hWnd Then
SetForegroundWindow(hWnd)
End If
End Sub

Private Sub timer_Tick(sender As Object, eArgs As EventArgs)
If sender = timer Then
restoreKbFocus(Me.Handle)
End If
End Sub

沒有留言:

張貼留言