Then how would you write this ShowBmp.cpp routine, please.
Maybe I will learn something so I can do it right.
It still needs to load the bitmap from a file.
On 1/9/2011 6:06 PM, Joseph M. Newcomer wrote:
See below..
On Sun, 09 Jan 2011 16:26:31 -0500, Ed<me@right.her> wrote:
JOE, SEE BELOW>
==========================================================
On 1/9/2011 4:10 PM, Joseph M. Newcomer wrote:
See below...
On Sun, 09 Jan 2011 11:52:35 -0500, Ed<me@right.her> wrote:
In my main dialog I have two buttons:
These two buttons load a bitmap from a file to a picture box ShowBmp
I have two test bitmap files 01.bmp& 02.bmp for testing.
I can view both bitmap files fine with windows.
For some reason within my program one shows fine and the other does not
show at all.
The OnPaint function has been removed from the main dialog.
I already checked that bitmap selection does work within OnPaint.
Any ideas?
void CMyDemoDlg::OnView1() // button 1
{
m_ShowBmp.SetBitmap(1);
m_ShowBmp.Invalidate();
}
void CMyDemoDlg::OnView2() // button 2
{
m_ShowBmp.SetBitmap(2);
m_ShowBmp.Invalidate();
}
=======================================================
// ShowBmp.cpp : implementation file
#include "stdafx.h"
#include "ShowBmp.h"
#include "resource.h"
// CShowBmp
IMPLEMENT_DYNAMIC(CShowBmp, CStatic)
CShowBmp::CShowBmp()
: m_IDBitmap(0)
{
}
CShowBmp::~CShowBmp()
{
}
BEGIN_MESSAGE_MAP(CShowBmp, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CShowBmp::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect cr;
GetClientRect(&cr);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
int SavedDC1 = MemDC.SaveDC();
CBitmap MemBitmap;
MemBitmap.CreateCompatibleBitmap(&dc,cr.Width(),cr.Height());
MemDC.SelectObject(&MemBitmap);
//start with a grey background
MemDC.FillSolidRect(&cr,RGB(220,220,220));
****
What is the purpose of the above and where did you find the random integers 220,220,220?
Why do you think these integers make sense?
****
======================================================
Like the comment above it ststes, the purpose of the 220,220,220 is just
to fill the background with a grey color I chose.
****
So why not use ::GetSysColor(COLOR_3DFACE), which would fill it in with the dialog
background color? And would track the user's color scheme.
*****
======================================================
****
The code below is just incredibly bad in many ways. For example, it hardwires the path,
and doesn't generate the filename from the integer.
======================================================
Why are you picking apart sections that have nothing to do with the
actual problem? I want a fixed path because I control it and that is
where the actual bitmaps are going to be.........
*****
Because the code is bad, even for a test harness.
****
======================================================
Why are you using LR_CREATEDIBSECTION?
======================================================
I found this from an internet search on how to use a bitmap from a file.
****
I've never used this, except in one case where I needed to get a partial bitmap.
****
======================================================
Why are you not putting spaces around the |? The line is nearly unreadable.
****
if (m_IDBitmap == 1) //loads bitmap if set
{
// load the .bmp from a file
CString szFilename("C:\\TestFolder\\01.bmp");
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap Bitmap;
Bitmap.Attach(hBmp);
BITMAP BmpInfo;
Bitmap.GetBitmap(&BmpInfo);
CDC MemDC2;
MemDC2.CreateCompatibleDC(&dc);
****
This is some of the most convoluted code I've seen in a while. Why are you creating all
these memory DCs? What's that MemDC above for?
****
int SavedDC2 = MemDC2.SaveDC();
MemDC2.SelectObject(&Bitmap); //copy bitmap to memory dc.
MemDC.StretchBlt(0,0,cr.Width(),cr.Height(),&MemDC2,0,0,BmpInfo.bmWidth,BmpInfo.bmHeight,SRCCOPY);
MemDC2.RestoreDC(SavedDC2);
****
This gets rid of everything the memDC2, making it unusable for anything else. And at no
point did you copy the MemDC2 into the main DC.
****
}
if (m_IDBitmap == 2) //loads bitmap if set
{
CString szFilename("C:\\TestFolder\\02.bmp"); //
loads the .bmp from a file
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap Bitmap;
Bitmap.Attach(hBmp);
BITMAP BmpInfo;
Bitmap.GetBitmap(&BmpInfo);
CDC MemDC2;
MemDC2.CreateCompatibleDC(&dc);
int SavedDC2 = MemDC2.SaveDC();
MemDC2.SelectObject(&Bitmap); //copy bitmap to memory dc.
MemDC.StretchBlt(0,0,cr.Width(),cr.Height(),&MemDC2,0,0,BmpInfo.bmWidth,BmpInfo.bmHeight,SRCCOPY);
****
OK, so you have the bitmap in the memory DC, but where did you copy the memory DC to the
DC?
MemDC2.RestoreDC(SavedDC2);
}
dc.BitBlt(0,0,cr.Width(),cr.Height(),&MemDC,0,0,SRCCOPY); //dump
****
But this gets back to the convoluted code. You never put anything into MemDC, and
everything you put into those local variables has been erased!
It doesn't work because you didn't actually DO anything to show the bitmap!
======================================================
Them explain why it does work with 01.bmp as it shows in the picture box
just fine??
****
I have no idea. The code is needlessly complex, and stores the bitmap in the wrong place.
Also, I notice you do error checking, so you don't even know if it successfully loaded
02.bmp.
======================================================
****
everything from memory dc to dialog
MemDC.RestoreDC(SavedDC1);
****
Give you did absolutely nothing to MemDC, restoring it doesn't make sense. Of course, if
you had actually put something in it, this would make sense.
joe
****
}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
======================================================
As usual with you JOE, you allways criticize what you see as TOTALLY
WRONG in your eyes, but NEVER actually help with YOUR corrections...
****
OK, maybe because the code is so needlessly complex I can't see the problem. If I can't
see the problem, I can't offer advice. Simplifiy the code to something that is minimal
for doing the problem, add error checking, and maybe you can find out why it doesn't work.
My corrections are intended to create code that either works or is analyzable.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm