multithreading - hwnd thread affinity painting from a different thread -
i've dgrect::draw(hwnd hwnd) draws blank hbitmap on hwnd window handle. works fine if call main() . works correctly if called dgrdpserver::dgrdpserver() constructor qtcpserver derived. works dgrdpserver::listen(qint64 port). hwnd passed in dgrdpserver constructor. problem appears when call dgrdpserver::incomingconnection(int socketdescriptor) i've qdebug()ed value of hwnd , okay. whats causing draw failing. ?? here goes code dgrect::draw(hwnd hwnd)
qbytearray ba; hdc hdc = getwindowdc(hwnd); hbitmap scrn = createcompatiblebitmap(hdc,/*width*/200,/*height*/200); setbitmapbits(scrn, /*size()*/200*200*4, ba.data()); bitmap bm; paintstruct ps; hdc whdc = beginpaint(hwnd, &ps); hdc hdcmem = createcompatibledc(whdc); hbitmap hbmold = (hbitmap)selectobject(hdcmem, scrn); getobject(scrn, sizeof(bm), &bm); bitblt(whdc, 0, 0, bm.bmwidth, bm.bmheight, hdcmem, 0, 0, srccopy); selectobject(hdcmem, hbmold); deletedc(hdcmem); endpaint(hwnd, &ps); showwindow(hwnd, sw_show); updatewindow(hwnd); update
seems hwnd can painted main thread only. updatewindow call works different thread. , looks functions dgrdpserver::incomingconnection(int socketdescriptor) called different thread. can done paint hwnd different thread ?
my guess problem here use of beginpaint: as per docs: application should not call beginpaint except in response wm_paint message
outside of that, may able use plain getdc/releasedc instead of beginpaint/endpaint pair.
the 'windows way' of doing sort of thing, however, use worker thread update internal data appropriately, , use invalidaterect or similar tell windows window needs updated, , windows send wm_paint later. updatewindow more immediate version of this: sends wm_paint window right there , (which causes actual painting take place on hwnd's thread).
Comments
Post a Comment