I've a DGRect::draw(HWND hwnd)
which simply draws a Blank HBITMAP
on hwnd
window Handle.
I works fine If I call it from main()
. It even works correctly If called from DGRDPServer::DGRDPServer()
constructor which is QTcpServer
Derived. It also works well from DGRDPServer::listen(qint64 port)
. The hwnd is passed in DGRDPServer
constructor. The Problem appears when I call it from DGRDPServer::incomingConnection(int socketDescriptor)
I've qDebug()
ed value of hwnd
and its Okay. Whats Causing the draw for failing. ??
Here goes my code for 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 like hwnd
can be painted from main thread only. However UpdateWindow
Call works from a different thread. and looks like functions like DGRDPServer::incomingConnection(int socketDescriptor)
are called from a different thread. So What can be done to paint hwnd
from a different thread ?