From: Joe on
Also:

How do I get an HDC from a HBITMAP?

I need an HDC of the source (HBITMAP) is one of the parameters to StretchBlt.
From: " ctacke/>" on
Look through Help for the APIs I mentioned. There are some simple samples
using each. Googling for examples will also work.

Here's a quick sample that loads an image from file and blits it to a
window. You'd simply modify it to stretch instead.

// load the bitmap
HBITMAP bmp = SHLoadDIBitmap(_T("\\myimage.bmp"));

// get the DC for the window on which to draw
// you have to have created a windows somewhere to pass in the handle here
HDC windowDC = GetDC(hWindowHandle);

// create a compatible DC with it
HDC drawingDC = CreateCompatibleDC(windowDC);

// select the bitmap into it
HBITMAP oldBMP = SelectObject(drawingDC, bmp);

// how big is the bitmap we loaded?
BITMAP b;
GetObject(bmp, sizeof(BITMAP), &b);

// blit the image to the window
BitBlt(windowDC, 0, 0, b.bmWidth, b.bmHeight, drawingDC, 0, 0,
SRCCCOPY);

// now clean up to prevent leaks
SelectObject(drawingDC, oldBMP);
DeleteDC(drawingDC);
DeleteObject(bmp);


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--




"Joe" <Joe(a)discussions.microsoft.com> wrote in message
news:9B2495D3-6CDB-4A42-AE73-20B09C0B0203(a)microsoft.com...
> At this point, I don't know how to tell what is MFC and what is Win32 API.
> Currently, I have books on MFC and a book on Win32 API being shipped here.
>
> For GetObject, I used like so:
> BITMAP bitmap;
> HBITMAP hbitmap = SHLoadDIBitmap(strBmp);
> if (hbitmap)
> {
> CRect r1, r2;
> ::GetObject(hbitmap, sizeof(BITMAP), &bitmap);
> r2.top = r2.left = 0;
> r2.bottom = bitmap.bmHeight;
> r2.right = bitmap.bmWidth;
> m_Picture.GetWindowRect(&r1); // m_Picture is CStatic Pic Box control
> ScreenToClient(&r1);
> // now what?
> }
>
> CreateCompatibleDC(HDC) requires an HDC structure. How do I get that from
> the CStatic m_Picture?
>
> "<ctacke/>" wrote:
>
>> I think you need to become one with the Win32 API before working with
>> wrappers like MFC so you can actually understand what's going on under
>> the
>> hood.
>>
>> GetObject on your HBITMAP will return a BITMAP, which will tell you its
>> size.
>> CreateCompatibleDC would generate a DC compatible with your CStatic
>> SelectObject can put a bitmap into a DC
>> CreateCompatibleBitmap would generate a buffer into which you could
>> StretchBlt
>>
>> There are probably MFC wrappers that handle these as well (like CBitmap
>> and
>> CDC) but again, if you understand how to do it in Win32, you shouldn't be
>> doing it with a wrapper class.
>>
>> --
>> Chris Tacke
>> OpenNETCF Consulting
>> Managed Code in the Embedded World
>> www.opennetcf.com
>> --
>


From: Joe on
Why is it that even though I tell MSDN to keep me logged in and to save my
password, the website still asks me for it every time?

Anyway...

Fantastic! I have finally gotten the new images to stretch to the actual
CStatic Picture Box control size!

However, there does seem to be another problem that might be related, so
I'll ask it here as well.

Before I call my ChangePic routine, I display a custom OpenFile Dialog to
allow my Clients to specify a picture from their media (SD flash cards). If
the dialog result is IDOK, I call ChangePic.

What I am noticing is that only the information around the custom dialog is
being painted. The region behind the custom dialog is *not* being painted,
even though my ChangePic routine can only be called after the custom dialog
has returned.

Any idea what could be causing this behavior? I do NOT override the OnPaint
event, so I don't think it is me.

Should I post my working code here for others to see, or is that frowned on?
From: " ctacke/>" on
Start a new thread, since it's a new issue, and post code, yes. My guess is
you need to invalidate the background.

-Chris


"Joe" <Joe(a)discussions.microsoft.com> wrote in message
news:B3D92A46-F326-4E94-8410-A4DB86DB78E6(a)microsoft.com...
> Why is it that even though I tell MSDN to keep me logged in and to save my
> password, the website still asks me for it every time?
>
> Anyway...
>
> Fantastic! I have finally gotten the new images to stretch to the actual
> CStatic Picture Box control size!
>
> However, there does seem to be another problem that might be related, so
> I'll ask it here as well.
>
> Before I call my ChangePic routine, I display a custom OpenFile Dialog to
> allow my Clients to specify a picture from their media (SD flash cards).
> If
> the dialog result is IDOK, I call ChangePic.
>
> What I am noticing is that only the information around the custom dialog
> is
> being painted. The region behind the custom dialog is *not* being
> painted,
> even though my ChangePic routine can only be called after the custom
> dialog
> has returned.
>
> Any idea what could be causing this behavior? I do NOT override the
> OnPaint
> event, so I don't think it is me.
>
> Should I post my working code here for others to see, or is that frowned
> on?


From: Joe on
The Working Code:
//---------------------------------------------------------------------------
void CMyDlg::ShowLastError(LPTSTR lpszMsgTitle) {
// my custom Show Last Error for GetLastError()
CString str;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
str.Format(L"Load Image failed:\n%s", lpMsgBuf);
MessageBox(str, lpszMsgTitle, MB_OK | MB_ICONERROR);
LocalFree(lpMsgBuf);
}
//---------------------------------------------------------------------------
int CMyDlg::ChangePic(LPTSTR lpszPicPath) {
// many thanks to Chris, aka <ctacke/>
int retVal(-1); // -1=some error; Did pic change? 1=yes, 0=no
CRect r_pic, r_bmp;
m_Picture.GetWindowRect(&r_pic);
ScreenToClient(&r_pic);
// load the bitmap
HBITMAP bmp = SHLoadDIBitmap(lpszPicPath);
if (bmp != NULL) {
// get DC for the window on which to draw
HDC picDC = ::GetDC(m_Picture.m_hWnd);
if (picDC == NULL) {
ShowLastError(L"Image Error - picDC");
return retVal;
}
// create a compatible DC with it
HDC bmpDC = CreateCompatibleDC(picDC);
if (bmpDC == NULL) {
ShowLastError(L"Image Error - bmpDC");
return retVal;
}
// select the bitmap into it
HBITMAP oldBMP = (HBITMAP)SelectObject(bmpDC, bmp);
if (oldBMP == NULL) {
ShowLastError(L"Image Error - oldBMP GDI_ERROR");
return retVal;
}
// get bitmap size info
BITMAP b;
if (GetObject(bmp, sizeof(BITMAP), &b) == 0) {
ShowLastError(L"Image Error - GetObject");
return retVal;
}
r_bmp.top = 0;
r_bmp.left = 0;
r_bmp.bottom = b.bmHeight;
r_bmp.right = b.bmWidth;
// StretchBlt the image to the window
retVal = ::StretchBlt(picDC, r_pic.left, r_pic.top, r_pic.Width(),
r_pic.Height(),
bmpDC, r_bmp.left, r_bmp.top, r_bmp.Width(), r_bmp.Height(), SRCCOPY);
if (retVal == 0) {
ShowLastError(L"Image Error - StretchBlt");
return retVal;
}
// clean up memory leaks
SelectObject(bmpDC, oldBMP);
DeleteDC(bmpDC);
DeleteObject(bmp);
}
return retVal;
}
//---------------------------------------------------------------------------