2016-05-05 11 views
1

別のウィンドウからピクセルカラーを取得したい。私が持っているコードは次のとおりです。C#別のウィンドウからピクセルカラーを取得

using System; 
    using System.Drawing; 
    using System.Runtime.InteropServices; 


sealed class Win32 
    { 
     [DllImport("user32.dll")] 
     static extern IntPtr GetDC(IntPtr hwnd); 

     [DllImport("user32.dll")] 
     static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc); 

     [DllImport("gdi32.dll")] 
     static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); 

     static public System.Drawing.Color GetPixelColor(int x, int y) 
     { 
     IntPtr hdc = GetDC(IntPtr.Zero); 
     uint pixel = GetPixel(hdc, x, y); 
     ReleaseDC(IntPtr.Zero, hdc); 
     Color color = Color.FromArgb((int)(pixel & 0x000000FF), 
        (int)(pixel & 0x0000FF00) >> 8, 
        (int)(pixel & 0x00FF0000) >> 16); 
     return color; 
     } 
    } 

問題は、このコードは、私が欲しいものではありません画面全体をスキャンしていることです。アイデアは、別のアプリケーション画面の境界に基づいてピクセルカラーをスキャンするようにコードを変更することです。たぶんFindWindowまたはGetWindowで何か?前もって感謝します。

答えて

0

キャプションでウィンドウを見つけるために言ったようにあなたがFindWindowをインポートすることができます。

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

static IntPtr FindWindowByCaption(string caption) 
{ 
    return FindWindowByCaption(IntPtr.Zero, caption); 
} 

ハンドラを使用してGetPixelColorに渡すパラメータを追加し、次に:

static public System.Drawing.Color GetPixelColor(IntPtr hwnd, int x, int y) 
{ 
    IntPtr hdc = GetDC(hwnd); 
    uint pixel = GetPixel(hdc, x, y); 
    ReleaseDC(hwnd, hdc); 
    Color color = Color.FromArgb((int)(pixel & 0x000000FF), 
        (int)(pixel & 0x0000FF00) >> 8, 
        (int)(pixel & 0x00FF0000) >> 16); 
    return color; 
} 

使用法:

var title = "windows caption"; 

var hwnd = FindWindowByCaption(title); 

var pixel = Win32.GetPixelColor(hwnd, x, y); 
関連する問題