OS:Win XP
IDE:VB2005 Express
タイマーを使って現在のマウスポインタの位置にある色を取得する。というものを作っています
普通のウィンドウ、普通の状態(?)ならうまい具合に色が取得できるのですが
どうもDirectXを使われていたり、フルスクリーンだったりするとうまく色が取得できません。
以下コードです。
Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Integer
Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Integer, ByVal yPoint As Integer) As IntPtr
Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer
Declare Function ScreenToClient Lib "user32" (ByVal hwnd As IntPtr, ByRef lpPoint As POINTAPI) As Integer
Private Sub timColorPick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timColorPick.Tick
Call GetCursorPos(rctPoint) 'マウス位置取得
ptrHwnd = WindowFromPoint(rctPoint.X, rctPoint.Y) 'マウス直下のウィンドウハンドル取得
Call ScreenToClient(ptrHwnd, rctPoint) '座標をウィンドウハンドルに対応したものに
ptrDC = GetDC(ptrHwnd) '対象ウィンドウのデバイスコンテキスト取得
lngColor = GetPixel(ptrDC, rctPoint.X, rctPoint.Y) '色取得
Call ReleaseDC(ptrHwnd, ptrDC)
End Sub
ツイート | ![]() |