実際に他の人に隠されているウィンドウ上の特定のピクセルを読み取ろうとしています。私はGDIライブラリからGetPixel関数を使用したいが、グローバルデバイスコンテキストでしか動作しないようだ。私は特定のウィンドウからピクセルを読み取ることができません。理由はわかりません。 this article PrintWindow関数を使用して、特定のウィンドウコンテンツを読み取り可能な一時的なデバイスコンテキストにコピーします。しかし私はそれを再現することはできません。ウィンドウのピクセルをzオーダーに関係なくキャプチャする
EDITは、すべて私の問題が解決されるありがとう:)
このスクリプトあなたに選びだしウィンドウ上のポインタのRGBの色を与え、ウィンドウが隠されているにもかかわらず。管理者権限で起動したプロセスのピクセルを取得するには、このプログラムを管理者権限で起動する必要があることに注意してください。
#define STRICT
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
// 0x0501 for PrintWindow function
// You must be at least running Windows XP
// See http://msdn.microsoft.com/en-us/library/6sehtctf.aspx
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define WINDOW_LIST_LIMIT 32
#define WINDOW_NAME_LIMIT 1024
void FatalError(char* error)
{
printf("%s", error);
exit(-1);
}
HWND window_list[WINDOW_LIST_LIMIT];
unsigned int window_list_index = 0;
BOOL EnumWindowsProc(HWND window_handle, LPARAM param)
{
char window_title[WINDOW_NAME_LIMIT];
if(!IsWindowVisible(window_handle)) return TRUE;
RECT rectangle = {0};
GetWindowRect(window_handle, &rectangle);
if (IsRectEmpty(&rectangle)) return TRUE;
GetWindowText(window_handle, window_title, sizeof(window_title));
if(strlen(window_title) == 0) return TRUE;
if(!strcmp(window_title, "Program Manager")) return TRUE;
window_list[window_list_index] = window_handle;
window_list_index++;
printf("%u - %s\n", window_list_index, window_title);
if(window_list_index == WINDOW_LIST_LIMIT) return FALSE;
return TRUE;
}
int main(int argc, char** argv)
{
unsigned int i, input;
EnumWindows((WNDENUMPROC) EnumWindowsProc, (LPARAM) NULL);
printf("\nChoose a window: ");
scanf("%u", &input);
printf("\n");
if(input > window_list_index) FatalError("Bad choice..\n");
HDC window_dc = GetWindowDC(window_list[input - 1]), global_dc = GetDC(0), temp_dc;
if(!window_dc && !global_dc) FatalError("Fatal Error - Cannot get device context.\n");
POINT cursor, previous_cursor;
while(1)
{
temp_dc = CreateCompatibleDC(window_dc);
if(!temp_dc) FatalError("Fatal Error - Cannot create compatible device context.\n");
RECT window_rectangle;
GetWindowRect(window_list[input - 1], &window_rectangle);
HBITMAP bitmap = CreateCompatibleBitmap(window_dc,
window_rectangle.right - window_rectangle.left,
window_rectangle.bottom - window_rectangle.top);
if (bitmap)
{
SelectObject(temp_dc, bitmap);
PrintWindow(window_list[input - 1], temp_dc, 0);
DeleteObject(bitmap);
}
GetCursorPos(&cursor);
if(cursor.x != previous_cursor.x && cursor.y != previous_cursor.y)
{
COLORREF color = GetPixel(temp_dc, cursor.x - window_rectangle.left, cursor.y - window_rectangle.top);
int red = GetRValue(color);
int green = GetGValue(color);
int blue = GetBValue(color);
printf("\rRGB %02X%02X%02X", red, green, blue);
cursor = previous_cursor;
}
DeleteDC(temp_dc);
Sleep(50); // for lags
}
ReleaseDC(window_list[input - 1], window_dc);
return 0;
}
私はいくつかのことを変更しましたが、User32は動的に読み込まれません。それは
gcc main.c -o main.exe -lGid32 -lUser32
でコンパイル
は素晴らしい一日を!
あなたはプロセス間でPrintWindow()仕事をすることはできません。 –
@Hansあなたは本当ですか? ['PrintWindow'](http://msdn.microsoft.com/en-us/library/windows/desktop/dd162869.aspx)ドキュメントには、別途記載されています。 –
はい。ペイントコードは他のプロセスにも存在します。 HDCは、プロセスの親和性を有する:[?XPで隠したりクリッピング窓からコンテンツをコピー]のhttp://stackoverflow.com/questions/2499487/sharing-hdc-between-different-processes –