簡單回答你的問題是你可以用不同的座標多次調用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);
然後就這樣做。用新值再次調用'GetPixel()'。這不行嗎? – andlabs
我不需要聲明另一個int紅色和int綠色......?像int reda只是爲了傳遞新的值?這不會造成不必要的內存使用情況嗎? –
在線程函數中調用「GetPixel」而不是在外面,我的意思是在函數「verde」和「amarelo」中。與你的不同的x,y值。 – Naidu