Problem is like this:
I have two winapi application’s. There is only one way communication. App1 sends message to App2. App2 should receive WM_COPYDATA with structure which has coordinates of two shapes and then it should paint those shapes.
Message is received. Data from structure is ok. Code in WM_COPYDATA case is executed which should paint window but nothing happens ( window has just solid white background)
Sending message looks like this:
HWND secondApp = FindWindowEx(NULL, NULL, _T("lab2app2"), NULL );
int error = GetLastError();
if(secondApp != 0) // window found, send message
{
// prepare data
FiguresData figuresData;
// Set Data About Location and dimensions of figures
// TODO
figuresData.square_origin_x = square_origin_x;
figuresData.square_origin_y = square_origin_y;
figuresData.circle_origin_x = circle_origin_x;
figuresData.circle_origin_y = circle_origin_y;
COPYDATASTRUCT data;
data.dwData = FIGURESDATA;
data.cbData = sizeof( figuresData );
data.lpData = &figuresData;
bool value = SendMessage(secondApp, WM_COPYDATA, (WPARAM)(HWND)mainWindow, (LPARAM)(LPVOID)&data);
}
FiguresData structure is straightforward and has only 4 ints in it.
The App2 receives messages and code for that is:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
RECT clientRectangle;
GetClientRect(hWnd, &clientRectangle);
hdc = BeginPaint(hWnd, &ps);
// nothing really here
EndPaint(hWnd, &ps);
break;
case WM_COPYDATA:
{
PCOPYDATASTRUCT pData;
pData = (PCOPYDATASTRUCT) lParam;
int square_origin_x = ((FiguresData *) ( pData->lpData ))->square_origin_x;
int square_origin_y = ((FiguresData *) ( pData->lpData ))->square_origin_y;
int circle_origin_x = ((FiguresData *) ( pData->lpData ))->circle_origin_x;
int circle_origin_y = ((FiguresData *) ( pData->lpData ))->circle_origin_y;
hdc = BeginPaint(hWnd, &ps);
HPEN oldPen = (HPEN)SelectObject(hdc, pen);
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, backgroundBrush);
COLORREF oldBackgroundColor = SetBkColor(hdc, backgroundColor);
Rectangle(hdc, square_origin_x, square_origin_y, square_origin_x+ SQUARE_SIDE, square_origin_y + SQUARE_SIDE);
Ellipse(hdc, circle_origin_x, circle_origin_y, circle_origin_x+ SQUARE_SIDE, circle_origin_y + SQUARE_SIDE);
// Put everything back the way we found it
SelectObject(hdc, oldPen);
SetBkColor(hdc, oldBackgroundColor);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
On debugger I see that window handle is ok. Message is sent as well as received by App2 and code in the WM_COPYDATA case is executed but it has no effect.
No shapes are drawn on the App2’s window and I have no idea why. Any clue?
You are calling
BeginPaintoutside of aWM_PAINThandler.BeginPaintreturns a device context that allows drawing only on the invalid region of the window. Since you have not calledInvalidateRect, the window is not yet invalid, soBeginPaintdoes not paint anything.As a general rule, you should restrict your painting to your
WM_PAINThandler. Have yourWM_COPYDATAhandler remember what needs to be painted, then invalidate the window and have theWM_PAINThandler actually paint it.