2016-11-01 33 views
-1

我有一個程序,它有使用GetPixel,但在不同的位置,讀取一個值並評估它後,它必須更改x,y位置並重新評估另一個函數的值。 我有當前的代碼:如何使用GetPixel()檢查不同的位置

while (true) { 
     HDC hDC; 
     hDC = GetDC(NULL); 
     int cx = 793; 
     int cy = 866; 
COLORREF color = GetPixel(hDC, cx, cy); //Use x=793 y=866 
     ReleaseDC(NULL, hDC); 
     RGBTRIPLE rgb; 
     int red = GetRValue(color); 
     int green = GetGValue(color); 
     int blue = GetBValue(color); 
     std::thread t1 (verde, red, green, blue); 
     t1.join(); 
     std::thread t2(amarelo, red, green, blue);//But then here I would like it to read 
     //from let's say x=803 y=796 
     t2.join(); 
} 

問題是GetPixel應該使用X = 793和Y = 866的verde函數,然後用x = 803 Y = 796爲amarelo功能

+2

然後就這樣做。用新值再次調用'GetPixel()'。這不行嗎? – andlabs

+0

我不需要聲明另一個int紅色和int綠色......?像int reda只是爲了傳遞新的值?這不會造成不必要的內存使用情況嗎? –

+1

在線程函數中調用「GetPixel」而不是在外面,我的意思是在函數「verde」和「amarelo」中。與你的不同的x,y值。 – Naidu

回答

1

簡單回答你的問題是你可以用不同的座標多次調用GetPixel()

另外,在每次循環迭代中不需要調用GetDC(0)。進入循環之前,您應該調用它一次。

試試這個:

COLORREF color; 
int red, green, blue; 

HDC hDC = GetDC(NULL); 
while (some_condition_is_true) { // don't write infinite loops! 
    color = GetPixel(hDC, 793, 866); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    std::thread t1(verde, red, green, blue); 
    t1.join(); 

    color = GetPixel(hDC, 803, 796); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    std::thread t2(amarelo, red, green, blue); 
    t2.join(); 
} 
ReleaseDC(NULL, hDC); 

不過,話雖如此,你的線程被浪費的開銷。在啓動第二個線程之前,您的循環被阻塞,等待第一個線程完成,並且在進入下一個循環迭代之前阻塞等待第二個線程完成。您運行的代碼序列化的方式,擊敗使用線程的目的,所以你可能也只是刪除線程乾脆直接調用該函數:

COLORREF color; 
int red, green, blue; 

HDC hDC = GetDC(NULL); 
while (some_condition_is_true) { 
    color = GetPixel(hDC, 793, 866); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    verde(red, green, blue); 

    color = GetPixel(hDC, 803, 796); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    amarelo(red, green, blue); 
} 
ReleaseDC(NULL, hDC); 

如果你想使用線程來處理多個像素同時,它應該看起來更像是這樣的:

COLORREF color; 
int red, green, blue; 

HDC hDC = GetDC(NULL); 
while (some_condition_is_true) { 
    color = GetPixel(hDC, 793, 866); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    std::thread t1(verde, red, green, blue); 

    color = GetPixel(hDC, 803, 796); 
    red = GetRValue(color); 
    green = GetGValue(color); 
    blue = GetBValue(color); 
    std::thread t2(amarelo, red, green, blue); 

    t1.join(); 
    t2.join(); 
} 
ReleaseDC(NULL, hDC); 

甚至是這樣的:

void verde(HDC hDC, int x, int y) 
{ 
    COLORREF color; 
    int red, green, blue; 

    while (some_condition_is_true) { 
     color = GetPixel(hDC, x, y); 
     red = GetRValue(color); 
     green = GetGValue(color); 
     blue = GetBValue(color); 
     //... 
    } 
} 

void amarelo(HDC hDC, int x, int, y) 
{ 
    COLORREF color; 
    int red, green, blue; 

    while (some_condition_is_true) { 
     color = GetPixel(hDC, x, y); 
     red = GetRValue(color); 
     green = GetGValue(color); 
     blue = GetBValue(color); 
     //... 
    } 
} 

HDC hDC = GetDC(NULL); 

std::thread t1(verde, hDC, 793, 866); 
std::thread t2(amarelo, hDC, 803, 796); 

t1.join(); 
t2.join(); 

ReleaseDC(NULL, hDC); 
+0

謝謝!它確實解決了我的問題,非常感謝! –

相關問題