I have a picture control which is called IDC_PICTURECONTROL, and a LPPICTURE which is called lpPicutre.
When my window receives WM_PAINT, I call my function drawPicture(HWND, LPPICTURE) like this:
drawPicture(GetDlgItem(hDlg, IDC_PICTURECONTROL), lpPicture);
The way it is written now, the black border around the control just disappears and the picture is not drawn at all.
If I edit the function so that it’s not drawing to the picture control, but rather the dialog itself (hDlg), then it is correctly drawn on the background of the window’s client area. (Not what I want).
Here is the code in the paint function:
void drawPicture(HWND hWnd, LPPICTURE picture)
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
//hdc = BeginPaint(hDlg, &ps); (works, but draws on window instead of control)
if (picture)
{
long hmWidth;
long hmHeight;
picture->get_Width(&hmWidth);
picture->get_Height(&hmHeight);
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
RECT rc;
GetClientRect(hWnd, &rc); // I have tried GetWindowRect() also
int w = 0, h = 0, x = 0, y = 0;
if (hmWidth == hmHeight)
{
// square
w = (rc.right - rc.left);
h = (rc.bottom - rc.top);
x = rc.left;
y = rc.top;
}
else if (hmWidth > hmHeight)
{
// wide
w = (rc.right - rc.left);
h = (w * hmHeight) / hmWidth;
x = rc.left;
y = (rc.bottom - rc.top - h) / 2;
}
else
{
//tall
h = (rc.bottom - rc.top);
w = (h * hmWidth) / hmHeight;
y = rc.top;
x = (rc.right - rc.left - w) / 2;
}
picture->Render(hdc, x, y, w, h, 0, hmHeight, hmWidth, -hmHeight, &rc);
}
EndPaint(hWnd, &ps);
//EndPaint(hDlg, &ps);
}
hWnd is the handler to the picture control, and hDlg is the handler to the dialog.
I thought maybe it’s being drawn off the window somewhere, so I set the x and y to 0, and the width and height to 1000, but this did not change anything.
What am I doing wrong?
If this code is working with hDlg, it is OK and the problem is probably in the static control itself. Ensure that is has SS_BITMAP style. In Visual Studio resource editor it is called Type and set to SS_BLACKFRAME (Frame) by default.