Re: Custom CRect Question
"Joseph M. Newcomer" wrote:
It isn't very hard to do this.
For a seasoned pro like you, I'm sure it is easy. But I don't have many
years of development under my belt, and I hardly ever have to draw anything
to the screen. It has taken me too long to find examples that can be molded
to what I want to do. I am also the only VC developer, and (by default) the
only MFC developer here. The other two developers here are Linux, Borland,
and Assembly developers. When I went to school, I learned on Visual Studio.
Now I'm using eMbedded VC.
...it still doesn't explain why you need some special
operator to extract the rectangle part of the subclass.
Originally, I wanted the rectangle dimensions so I would know what size
object to paint on the screen. Since reading responses to this thread and
the "Drawing Layers" thread, my approach has changed - hopefully for the
better!
I've read more than one request for my code, so here it is (without the
headers). The object "m_img" in CMainDlg::DisplayImage (called from
CMainDlg::OnPaint) is not defined below, because it works. I'm sure my
coding style will disturb some (white space disturbs me), but everyone is
different.
My hope is that someone like Mr. joe Flounder can see what I'm trying to do
and point out any wrong practices.
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// ZRectBar class
ZRectBar::ZRectBar(RECT &r, COLORREF rgb) {
m_cr = rgb;
m_pt[eBL] = CPoint(r.bottom, r.left);
m_pt[eBR] = CPoint(r.bottom, r.left); // innitialize to left size
m_pt[eBM] = CPoint(r.bottom, r.right);
m_pt[eTL] = CPoint(r.top, r.left);
m_pt[eTR] = CPoint(r.top, r.left); // innitialize to left size
m_pt[eTM] = CPoint(r.top, r.right);
}
//---------------------------------------------------------------------------
ZRectBar::~ZRectBar() {
}
//---------------------------------------------------------------------------
void ZRectBar::Update(double ratio) { // only updates right side
m_pt[eTR].x = int(m_pt[eTM].x * ratio + 0.5);
m_pt[eTR].y = int(m_pt[eTM].y * ratio + 0.5);
m_pt[eBR].x = int(m_pt[eBM].x * ratio + 0.5);
m_pt[eBR].y = int(m_pt[eBM].y * ratio + 0.5);
}
//---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// ZProgressBar class
ZProgressBar::ZProgressBar(RECT &r) : m_active(FALSE) {
if (m_rb[eBASE] != NULL) delete m_rb[eBASE];
if (m_rb[eSESS] != NULL) delete m_rb[eSESS];
if (m_rb[eFRAM] != NULL) delete m_rb[eFRAM];
SetPosition(r);
}
//---------------------------------------------------------------------------
ZProgressBar::~ZProgressBar() {
delete[] &m_rb;
}
//---------------------------------------------------------------------------
DWORD ZProgressBar::PaintDC(CDC* dc) {
CPen penBase(PS_SOLID, 1, m_rb[eBASE]->m_cr);
CPen* oldpen = dc->SelectObject(&penBase);
CBrush brushBase(m_rb[eBASE]->m_cr);
CBrush* oldbrush = dc->SelectObject(&brushBase);
dc->Polygon(m_rb[eBASE]->m_pt, 4);
// Frame
CPen penFram(PS_SOLID, 1, m_rb[eFRAM]->m_cr);
dc->SelectObject(&penFram);
CBrush brushFram(m_rb[eFRAM]->m_cr);
dc->SelectObject(&brushFram);
dc->Polygon(m_rb[eFRAM]->m_pt, 4);
// Session
CPen penSess(PS_SOLID, 1, m_rb[eSESS]->m_cr);
dc->SelectObject(&penSess);
CBrush brushSess(m_rb[eSESS]->m_cr);
dc->SelectObject(&brushSess);
dc->Polygon(m_rb[eSESS]->m_pt, 4);
dc->SelectObject(oldpen);
dc->SelectObject(oldbrush);
return 0;
}
//---------------------------------------------------------------------------
void ZProgressBar::SetPosition(RECT& r) {
CPoint center(CRect(r).CenterPoint());
CRect rect;
COLORREF cr;
int x(3 * center.x / 4); // = 3/4 * center.x
int y(1 * center.y / 10); // = 1/10 * center.y
int top1(center.y - y), top2(center.y - (y - 5));
int left(center.x - x), right(center.x + x);
int bottom1(center.y + y), bottom2(center.y + (y - 5));
if (m_rb[eBASE] == NULL) {
rect = CRect(CPoint(top1, left), CPoint(bottom1, right));
cr = RGB(0xC0, 0xC0, 0xC0); // silver
m_rb[eBASE] = new ZRectBar(rect, cr);
}
if (m_rb[eFRAM] == NULL) {
rect = CRect(CPoint(top1, left), CPoint(bottom1, left));
cr = RGB(0xFF, 0x00, 0x00); // red
m_rb[eFRAM] = new ZRectBar(rect, cr);
}
if (m_rb[eSESS] == NULL) {
rect = CRect(CPoint(top2, left), CPoint(bottom2, left));
cr = RGB(0x00, 0x80, 0xFF); // blue-ish
m_rb[eSESS] = new ZRectBar(rect, cr);
}
}
//---------------------------------------------------------------------------
void ZProgressBar::Update(ZInfoPacket *pkt) {
m_active = TRUE;
if ((pkt->sessions != 0) && (pkt->frames != 0)) { // prevent division by
zero
m_rb[eSESS]->Update(double(pkt->session / pkt->sessions));
m_rb[eFRAM]->Update(double(pkt->frame / pkt->frames));
}
}
//---------------------------------------------------------------------------
void CMainDlg::DisplayImage(CPaintDC *dc) {
BOOL ok(m_bitmap.GetBitmap(&m_img.Bmp));
CBitmap *poldbmp;
if (ok) {
CDC mDC;
mDC.CreateCompatibleDC(dc);
poldbmp = mDC.SelectObject(&m_bitmap);
if (poldbmp != NULL) {
m_pic = m_img.SetBounds(&m_rect);
if (m_progressBar->Active() == TRUE) {
m_progressBar->PaintDC(&mDC); // paint the progress bar to memDC
before copying it
}
ok = dc->StretchBlt(m_pic.left, m_pic.top, m_pic.Width(),
m_pic.Height(), &mDC,
m_img.BmpR.left, m_img.BmpR.top, m_img.BmpR.Width(),
m_img.BmpR.Height(), SRCCOPY);
if (!ok) {
outList.ConcatTail(CString(TEXT("\n")) +
CString(SysErrorMessage(GetLastError())));
}
mDC.SelectObject(poldbmp); // restore the DC to original state
//mDC.DeleteDC();
m_bitmap.DeleteObject();
}
mDC.ReleaseAttribDC();
} else {
ShowLastError(TEXT("GetBitmap Failed"));
m_img.SetKind(kNONE); // prevents an err loop
Invalidate(); // messes up the background
}
}
//---------------------------------------------------------------------------