From: Joseph M. Newcomer on
On Wed, 17 Feb 2010 22:07:30 -0800 (PST), Luigino <npuleio(a)rocketmail.com> wrote:

>HI Joe,
>
>I figured to draw correctly horizontal scrolling having origin point
>at right bottom so the graphic starts from right to left but it
>flickers.
>So I created a memory DC with CreateCompatibleDC but looks like memory
>dc doesn't behave really the same about mapping as you can see in this
>code:
>
>void CMyDLL::PrepareDC(CDC & dc, CRect rc)
>{
> dc.SetMapMode(MM_ANISOTROPIC);
>
> switch (iGraphType)
****
Why is iGraphType not a parameter to this function? Depending on global state is bad
style
****
> {
> case GRAPH_BARS:
> {
> //dc.Rectangle()
> }
> break;
>
> case GRAPH_LINES:
> {
> dc.SetWindowExt(-rc.Width(), iYMaxInterval);
> dc.SetViewportExt(rc.Width(), -rc.Height());
> dc.SetWindowOrg(rc.Width(), 0);
> dc.SetViewportOrg(0, rc.Height());
> }
> break;
>
> default:
> break;
> }
>}
>
>void CMyDLL::DoDrawing(CDC & dc, CRect rc)
>{
> // *********** graphic component ***********
> // based on choice of graph type
> CPen qPolylinePen(PS_SOLID, 1, RGB(0, 255, 0));
>
> switch (iGraphType)
> {
> case GRAPH_BARS:
> {
> //dc.Rectangle()
> }
> break;
>
> case GRAPH_LINES:
> {
> if (vtPoints.capacity() == 1)
> {
>
> for (int i=vtPoints[0].size()-1;i>=0;i--) {
> vtToDraw[0].at(i).x = vtPoints[0].at(i).x;
> vtToDraw[0].at(i).y = ( (vtPoints[0].at(i).y > 0) ?
>vtPoints[0].at(i).y : 86 );
> }
>
> dc.SelectObject(qPolylinePen);
> vectfPoints* pointsline = &vtToDraw[0];
> dc.Polyline(&(*pointsline)[0], (int)pointsline->size());
> //dc.PolyPolyline()
> }
> }
> break;
>
> default:
> break;
> }
> GDI_FLUSH();
>}
>
>void CNyDLL::OnPaint()
>{
> CPaintDC dc(this); // device context for painting
>
> CRect r;
> dc.GetClipBox(&r);
> dc.SetBkColor(RGB(0,0,0));
****
GetClipBox makes no sense. GetClientRect does. The clip box and the client rectangle are
unrelated.
****
>
> int saveobject = dc.SaveDC();
>
> if(bSetDraw)
> {
> // ********** Background ***********
> // Grid
> if (firstTime) {
> PrepareDC(dc, r);
> firstTime = FALSE;
> }
>
> CDC memDC;
> CBitmap bBitmap; // Offscreen bitmap
> CBitmap* bOldBitmap; // bitmap originally found
****
Why? What's wrong with SaveDC/RestoreDC here?
****
>
> memDC.CreateCompatibleDC(&dc);
> bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
****
Did you look at the size? Do you know how big this actually is? It must be the client
rectangle size, and the clip box is unrelated to this except by accident.
****
> bOldBitmap = memDC.SelectObject(&bBitmap);
>
> memDC.FillSolidRect(r, dc.GetBkColor());
> memDC.SetMapMode(dc.GetMapMode());
> memDC.SetWindowExt(dc.GetWindowExt());
> memDC.SetViewportExt(dc.GetViewportExt());
****
Why did you not call PrepareDC on the memory DC? What good is it to have an abstraction
if you ignore it?
****
> memDC.SetWindowOrg(dc.GetWindowOrg());
> memDC.SetViewportOrg(dc.GetViewportOrg());
>
> if (bActivateGrid)
> {
> CPen qLinePen(PS_SOLID, 0, RGB(0,139,0));
> memDC.SelectObject(qLinePen);
>
> // Grid - Horizontal lines
> for(int y = 0; y < r.bottom; y += (int)12) {
> /* scan y */
> memDC.MoveTo(r.left, y);
> memDC.LineTo(r.right, y);
> } /* scan y */
>
> // Grid - Vertical lines
> for(int x = 0 - (int)gridOffset; x <= r.right - (int)gridOffset; x
>+= (int)12 ) {
> /* scan x */
> memDC.MoveTo(x, r.top);
> memDC.LineTo(x, r.bottom);
> } /* scan x */
> }
>
> DoDrawing(memDC, r);
>
> // Copy the offscreen bitmap onto the screen.
> dc.BitBlt(r.left, r.top, r.Width(), r.Height(),
> &memDC, r.left, r.top, SRCCOPY);
****
Since r is the clipbox, and usually has nothing to do with the client rectangle except by
accident, using it as a reference point is erroneous. Note also that since you are moving
relative to logical coordinates of the dc, and you've remapped those, the BitBlt moves the
image to the lower right corner so it hangs off the right edge, e.g.,

+---+
| A|
+---+---+
| B |
+---+

(depending on your fonts, this image may or may not look right, but you just did a
transfer into position B, because 0,0 is at the bottom right)

Key here: you do NOT map the raw DC; you do NOT use the ClipBox as a size reference, and
you ONLY call PrepareDC on the memory DC.
joe
****
>
> //Swap back the original bitmap.
> memDC.SelectObject(bOldBitmap);
> }
> //}
> dc.RestoreDC(saveobject);
>}
>
>
>because the PolyLine now starts from left to rigtht and on the other
>hand it looks like it doesn't apply correctly the Y interval...
>Did I maybe forgot something or is there a possible alternative to
>avoid flickering if CreateCompatibleDC isn't the right solution for
>this?...
>
>Thanks,
>Ciao
>Luigi
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Luigino on
HI Joe!

> ****
> SelectObject of what?  Presumably, a compatible bitmap, or the DC has no value.

of this:

memDC.CreateCompatibleDC(&dc);
bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
bOldBitmap = memDC.SelectObject(&bBitmap);

it's a SelectObject of the compatible bitmap....
From: Joseph M. Newcomer on
It's always useful to be clear. But I think I discovered your bug already, which is that
you must map ONLY the DC in which you are drawing, not all DCs. Your PrepareDC function
should be applied ONLY to the memory DC. And absolutely NOT to the CPaintDC.
joe

On Sat, 20 Feb 2010 13:51:57 -0800 (PST), Luigino <npuleio(a)rocketmail.com> wrote:

>HI Joe!
>
>> ****
>> SelectObject of what? �Presumably, a compatible bitmap, or the DC has no value.
>
>of this:
>
> memDC.CreateCompatibleDC(&dc);
> bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
> bOldBitmap = memDC.SelectObject(&bBitmap);
>
>it's a SelectObject of the compatible bitmap....
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Luigino on
HI Joe

> >    switch (iGraphType)
>
> ****
> Why is iGraphType not a parameter to this function?  Depending on global state is bad
> style

Because it's an attribute of the class object where later could draw
polylines or rectangles (vertical bars)...

> >    int saveobject = dc.SaveDC();
>
> ****
> Why?  What's wrong with SaveDC/RestoreDC here?
> ****

Nothing here... .just to "release" objects like CPen

> >            memDC.CreateCompatibleDC(&dc);
> >            bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r..Height());
>
> ****
> Did you look at the size?  Do you know how big this actually is?  It must be the client
> rectangle size, and the clip box is unrelated to this except by accident.
> ****

I forgot to change GetClipBox with GetClientRect....
anyway the r values are of the rectangle size of the custom control..

>            bOldBitmap = memDC.SelectObject(&bBitmap);
>
> >            memDC.FillSolidRect(r, dc.GetBkColor());
> >            memDC.SetMapMode(dc.GetMapMode());
> >            memDC.SetWindowExt(dc.GetWindowExt());
> >            memDC.SetViewportExt(dc.GetViewportExt());
>
> ****
> Why did you not call PrepareDC on the memory DC?  What good is it to have an abstraction
> if you ignore it?
> ****
>
> >            memDC.SetWindowOrg(dc.GetWindowOrg());
> >            memDC.SetViewportOrg(dc.GetViewportOrg());
>
> >            if (bActivateGrid)
> >            {
> >                    CPen qLinePen(PS_SOLID, 0, RGB(0,139,0));
> >                    memDC.SelectObject(qLinePen);
>
> >                    // Grid - Horizontal lines
> >                    for(int y = 0; y < r.bottom; y += (int)12) {
> >                            /* scan y */
> >                            memDC.MoveTo(r.left, y);
> >                            memDC.LineTo(r.right, y);
> >                    }       /* scan y */
>
> >                    // Grid - Vertical lines
> >                    for(int x = 0 - (int)gridOffset; x <= r.right - (int)gridOffset; x
> >+= (int)12  ) {
> >                            /* scan x */
> >                            memDC.MoveTo(x, r.top);
> >                            memDC.LineTo(x, r.bottom);
> >                    }       /* scan x */
> >            }
>
> >            DoDrawing(memDC, r);
>
> >            // Copy the offscreen bitmap onto the screen.
> >            dc.BitBlt(r.left, r.top, r.Width(), r.Height(),
> >                    &memDC, r.left, r.top, SRCCOPY);
>

> ****
> Since r is the clipbox, and usually has nothing to do with the client rectangle except by
> accident, using it as a reference point is erroneous.  Note also that since you are moving
> relative to logical coordinates of the dc, and you've remapped those, the BitBlt moves the
> image to the lower right corner so it hangs off the right edge, e.g.,
>
> +---+
> |  A|
> +---+---+
>       | B |
>       +---+
>
> (depending on your fonts, this image may or may not look right, but you just did a
> transfer into position B, because 0,0 is at the bottom right)
>
> Key here: you do NOT map the raw DC; you do NOT use the ClipBox as a size reference, and
> you ONLY call PrepareDC on the memory DC.

So as Stephen said, I should avoid to map DC and directly map memDC
then re-map the memDC to MM_TEXT right before the BitBlt to set the
0,0 point origin at the original point at top-left so the bitblt would
be done in that position right?...
I tried that and I figured it doesn't appear at all, I mean it doesn't
even draw the black background rectangle on the form...

Here I paste the example class files I'm implementing:

**************************************************************************
************************ GRAPHCLASS.H ****************************
**************************************************************************

#pragma once

#include <vector>
using namespace std;

enum GRAPH_TYPE { GRAPH_BARS=0, GRAPH_LINES };

#define LOCAL_CLASSNAME _T("CGraphClassCtrl") // Window class name

// CGraphClass
class CGraphClass : public CWnd
{
DECLARE_DYNAMIC(CGraphClass)

// matrix of POINT for each element to represent
typedef vector<POINT> vectfPoints; // array representing points
values for each single source
typedef vector<vectfPoints> vectPoints; // array representing list
of sources having their POINT values

typedef vector<POINT> vect_to_draw;
typedef vector<vect_to_draw> vectToDraw;

public:
CGraphClass();
virtual ~CGraphClass();

// methods
public:
BOOL InitializeGraph(int maxsizearray, int maxinterval, GRAPH_TYPE
typegraph=GRAPH_LINES);
void AddLinesElement(UINT iIndexSource, LPCTSTR description, double
value);

// Attributes
public:
void set_GraphType(GRAPH_TYPE graphtype);
GRAPH_TYPE get_GraphType();

protected:
BOOL RegisterWindowClass();

protected:
int iGraphType;
int iYMaxInterval;
BOOL bActivateGrid;
UINT iTimerVal;
UINT nElapse;
double gridOffset;
BOOL bSetDraw;
int iSourceMaxSizeArray;

vectPoints vtPoints; // matrix of sources representing POINT
values
vectToDraw vtToDraw; // matrix of sources in POINT as logical
unit to draw

BOOL EnableTimer();

protected:
afx_msg void OnPaint();
afx_msg void OnTimer(UINT TimerVal);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
DECLARE_MESSAGE_MAP()
};


**************************************************************************
********************** GRAPHCLASS.CPP **************************
**************************************************************************

// GraphClass.cpp : implementation file
//

#include "stdafx.h"
#include "test_compatibledc.h"
#include "GraphClass.h"
#include <algorithm>

// CGraphClass
#define IDT_TIMER_0 ( WM_USER + 1000 )

IMPLEMENT_DYNAMIC(CGraphClass, CWnd)

CGraphClass::CGraphClass()
{
RegisterWindowClass();
}

CGraphClass::~CGraphClass()
{
}

// Register the window class if it has not already been registered.
BOOL CGraphClass::RegisterWindowClass()
{
WNDCLASS wndcls;

if (!(::GetClassInfo(AfxGetInstanceHandle(), LOCAL_CLASSNAME,
&wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW |
CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()-
>LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = LOCAL_CLASSNAME;

if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}

return TRUE;
}

BEGIN_MESSAGE_MAP(CGraphClass, CWnd)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// CGraphClass message handlers
void CGraphClass::OnTimer(UINT TimerVal)
{
// ****** processing event ******
if (TimerVal == IDT_TIMER_0)
{
gridOffset++;
if (gridOffset > 12.5)
gridOffset = 0.0;

Invalidate(); // to call OnPaint()
UpdateWindow();
}
// call base class handler
CWnd::OnTimer(TimerVal);
}

void CGraphClass::OnPaint()
{
CPaintDC dc(this); // device context for painting

CRect r;
GetClientRect(&r);
dc.SetBkColor(RGB(0,0,0));

int saveobject = dc.SaveDC();

if(bSetDraw)
{
// ********** Background ***********
// Grid

CDC memDC;
CBitmap bBitmap; // Offscreen bitmap
CBitmap* bOldBitmap; // bitmap originally found in CMemDC

memDC.CreateCompatibleDC(&dc);
bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
bOldBitmap = memDC.SelectObject(&bBitmap);

memDC.SetMapMode(MM_ANISOTROPIC);
memDC.SetWindowExt(-r.Width(), iYMaxInterval);
memDC.SetViewportExt(r.Width(), -r.Height());
memDC.SetWindowOrg(r.Width(), 0);
memDC.SetViewportOrg(0, r.Height());

memDC.FillRect(r,
CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));

if (bActivateGrid)
{
CPen qLinePen(PS_SOLID, 1, RGB(0,139,0));
memDC.SelectObject(qLinePen);

// Grid - Horizontal lines
for(int y = 0; y < r.bottom; y += (int)12) {
/* scan y */
memDC.MoveTo(r.left, y);
memDC.LineTo(r.right, y);
} /* scan y */

// Grid - Vertical lines
for(int x = 0 - (int)gridOffset; x <= r.right - (int)gridOffset; x
+= (int)12 ) {
/* scan x */
memDC.MoveTo(x, r.top);
memDC.LineTo(x, r.bottom);
} /* scan x */
}

if (vtPoints.capacity() == 1)
{
for (int i=vtPoints[0].size()-1;i>=0;i--) {
vtToDraw[0].at(i).x = vtPoints[0].at(i).x;
vtToDraw[0].at(i).y = ( (vtPoints[0].at(i).y > 0) ?
vtPoints[0].at(i).y : 86 );
}
//int savedraw = memDC.SaveDC();

CPen qPolylinePen(PS_SOLID, 1, RGB(0, 255, 0));
memDC.SelectObject(qPolylinePen);
vectfPoints* pointsline = &vtToDraw[0];
memDC.Polyline(&(*pointsline)[0], (int)pointsline->size());
//dc.PolyPolyline()

//memDC.RestoreDC(savedraw);
}

// Copy the offscreen bitmap onto the screen.
//memDC.SetMapMode(MM_TEXT);

dc.BitBlt(r.left, r.top, r.Width(), r.Height(),
&memDC, r.left, r.top, SRCCOPY);

//Swap back the original bitmap.
memDC.SelectObject(bOldBitmap);
}
dc.RestoreDC(saveobject);
}

BOOL CGraphClass::OnEraseBkgnd(CDC* pDC)
{
//return CWnd::OnEraseBkgnd(pDC);
return FALSE;
}

// ****************************** ATTRIBUTES
******************************
void CGraphClass::set_GraphType(GRAPH_TYPE graphtype)
{
iGraphType = graphtype;
}

GRAPH_TYPE CGraphClass::get_GraphType()
{
return (GRAPH_TYPE)iGraphType;
}

// ******************************* METHODS
*******************************

BOOL CGraphClass::InitializeGraph(int maxsizearray, int maxinterval,
GRAPH_TYPE typegraph/*=GRAPH_LINES*/)
{
// Initializing graph's DC...
iGraphType = typegraph;
gridOffset = 0;
bActivateGrid = TRUE;
bSetDraw = TRUE;
nElapse = 1000;
iTimerVal = 0;

iYMaxInterval = maxinterval;
iSourceMaxSizeArray = maxsizearray; // getting max size array of
each source which has to be fixed
vtPoints.resize(1);
vtPoints[0].resize(iSourceMaxSizeArray);
vtToDraw.resize(1);
vtToDraw[0].resize(iSourceMaxSizeArray);

CRect rc;
GetClientRect(&rc);
int iPointOfOrigin = 0;

for (int i=vtPoints[0].size()-1;i>=0;i--) // initializing this
first source to initial values
{ // which are consecutive integer values for X-axis
vtPoints[0].at(i).x = iPointOfOrigin++; // and bottom screen value
for Y-axis
vtPoints[0].at(i).y = 0;
vtToDraw[0].at(i).x = iPointOfOrigin;
vtToDraw[0].at(i).y = 0;
}

iTimerVal = SetTimer(IDT_TIMER_0, nElapse, 0);

return TRUE;
}

void CGraphClass::AddLinesElement(UINT iIndexSource, LPCTSTR
description, double value)
{
// add in value format because CPoint depends from resize...
int i;
CRect rc;
GetClientRect(&rc);

vtPoints.resize(iIndexSource+1); // setting the array to max
size defined
vtPoints[iIndexSource].resize(iSourceMaxSizeArray);
int iPointOfOrigin = 0;
for (i=vtPoints[iIndexSource].size()-1;i>=0;i--) // initializing
this first source to initial values
{ // which are consecutive integer values for X-axis
vtPoints[iIndexSource].at(i).x = iPointOfOrigin++; // and bottom
screen value for Y-axis
vtPoints[iIndexSource].at(i).y = 0;
}

// Now I add element value to the source
rotate(vtPoints[iIndexSource].begin(), vtPoints[iIndexSource].begin()
+1, vtPoints[iIndexSource].end()); // SHIFT-LEFT the array of POINTs
to add new value
vtPoints[iIndexSource].at(vtPoints[iIndexSource].size() - 1).y =
(LONG)value;
iPointOfOrigin = 0;
for (i=vtPoints[iIndexSource].size()-1;i>=0;i--)
vtPoints[iIndexSource].at(i).x = iPointOfOrigin++;

Invalidate();
UpdateWindow();
}

BOOL CGraphClass::EnableTimer()
{
//
***************************************************************************
// if enabled it's a realtime task manager, otherwise it'd be a
graphical
// representation of values passed from external source
//
***************************************************************************

iTimerVal = SetTimer(IDT_TIMER_0, nElapse, 0);

return TRUE;
}

Ciao,
Luigi
From: Stephen Myers on
See below.

Luigino wrote:
> HI Joe
>
>>> switch (iGraphType)
>> ****
>> Why is iGraphType not a parameter to this function? Depending on global state is bad
>> style
>
> Because it's an attribute of the class object where later could draw
> polylines or rectangles (vertical bars)...
>
>>> int saveobject = dc.SaveDC();
>> ****
>> Why? What's wrong with SaveDC/RestoreDC here?
>> ****
>
> Nothing here... .just to "release" objects like CPen
>
>>> memDC.CreateCompatibleDC(&dc);
>>> bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r..Height());
>> ****
>> Did you look at the size? Do you know how big this actually is? It must be the client
>> rectangle size, and the clip box is unrelated to this except by accident.
>> ****
>
> I forgot to change GetClipBox with GetClientRect....
> anyway the r values are of the rectangle size of the custom control..
>
>> bOldBitmap = memDC.SelectObject(&bBitmap);
>>
>>> memDC.FillSolidRect(r, dc.GetBkColor());
>>> memDC.SetMapMode(dc.GetMapMode());
>>> memDC.SetWindowExt(dc.GetWindowExt());
>>> memDC.SetViewportExt(dc.GetViewportExt());
>> ****
>> Why did you not call PrepareDC on the memory DC? What good is it to have an abstraction
>> if you ignore it?
>> ****
>>
>>> memDC.SetWindowOrg(dc.GetWindowOrg());
>>> memDC.SetViewportOrg(dc.GetViewportOrg());
>>> if (bActivateGrid)
>>> {
>>> CPen qLinePen(PS_SOLID, 0, RGB(0,139,0));
>>> memDC.SelectObject(qLinePen);
>>> // Grid - Horizontal lines
>>> for(int y = 0; y < r.bottom; y += (int)12) {
>>> /* scan y */
>>> memDC.MoveTo(r.left, y);
>>> memDC.LineTo(r.right, y);
>>> } /* scan y */
>>> // Grid - Vertical lines
>>> for(int x = 0 - (int)gridOffset; x <= r.right - (int)gridOffset; x
>>> += (int)12 ) {
>>> /* scan x */
>>> memDC.MoveTo(x, r.top);
>>> memDC.LineTo(x, r.bottom);
>>> } /* scan x */
>>> }
>>> DoDrawing(memDC, r);
>>> // Copy the offscreen bitmap onto the screen.
>>> dc.BitBlt(r.left, r.top, r.Width(), r.Height(),
>>> &memDC, r.left, r.top, SRCCOPY);
>
>> ****
>> Since r is the clipbox, and usually has nothing to do with the client rectangle except by
>> accident, using it as a reference point is erroneous. Note also that since you are moving
>> relative to logical coordinates of the dc, and you've remapped those, the BitBlt moves the
>> image to the lower right corner so it hangs off the right edge, e.g.,
>>
>> +---+
>> | A|
>> +---+---+
>> | B |
>> +---+
>>
>> (depending on your fonts, this image may or may not look right, but you just did a
>> transfer into position B, because 0,0 is at the bottom right)
>>
>> Key here: you do NOT map the raw DC; you do NOT use the ClipBox as a size reference, and
>> you ONLY call PrepareDC on the memory DC.
>
> So as Stephen said, I should avoid to map DC and directly map memDC
> then re-map the memDC to MM_TEXT right before the BitBlt to set the
> 0,0 point origin at the original point at top-left so the bitblt would
> be done in that position right?...
> I tried that and I figured it doesn't appear at all, I mean it doesn't
> even draw the black background rectangle on the form...
>
> Here I paste the example class files I'm implementing:
>
> **************************************************************************
> ************************ GRAPHCLASS.H ****************************
> **************************************************************************
>
> #pragma once
>
> #include <vector>
> using namespace std;
>
> enum GRAPH_TYPE { GRAPH_BARS=0, GRAPH_LINES };
>
> #define LOCAL_CLASSNAME _T("CGraphClassCtrl") // Window class name
>
> // CGraphClass
> class CGraphClass : public CWnd
> {
> DECLARE_DYNAMIC(CGraphClass)
>
> // matrix of POINT for each element to represent
> typedef vector<POINT> vectfPoints; // array representing points
> values for each single source
> typedef vector<vectfPoints> vectPoints; // array representing list
> of sources having their POINT values
>
> typedef vector<POINT> vect_to_draw;
> typedef vector<vect_to_draw> vectToDraw;
>
> public:
> CGraphClass();
> virtual ~CGraphClass();
>
> // methods
> public:
> BOOL InitializeGraph(int maxsizearray, int maxinterval, GRAPH_TYPE
> typegraph=GRAPH_LINES);
> void AddLinesElement(UINT iIndexSource, LPCTSTR description, double
> value);
>
> // Attributes
> public:
> void set_GraphType(GRAPH_TYPE graphtype);
> GRAPH_TYPE get_GraphType();
>
> protected:
> BOOL RegisterWindowClass();
>
> protected:
> int iGraphType;
> int iYMaxInterval;
> BOOL bActivateGrid;
> UINT iTimerVal;
> UINT nElapse;
> double gridOffset;
> BOOL bSetDraw;
> int iSourceMaxSizeArray;
>
> vectPoints vtPoints; // matrix of sources representing POINT
> values
> vectToDraw vtToDraw; // matrix of sources in POINT as logical
> unit to draw
>
> BOOL EnableTimer();
>
> protected:
> afx_msg void OnPaint();
> afx_msg void OnTimer(UINT TimerVal);
> afx_msg BOOL OnEraseBkgnd(CDC* pDC);
> DECLARE_MESSAGE_MAP()
> };
>
>
> **************************************************************************
> ********************** GRAPHCLASS.CPP **************************
> **************************************************************************
>
> // GraphClass.cpp : implementation file
> //
>
> #include "stdafx.h"
> #include "test_compatibledc.h"
> #include "GraphClass.h"
> #include <algorithm>
>
> // CGraphClass
> #define IDT_TIMER_0 ( WM_USER + 1000 )
>
> IMPLEMENT_DYNAMIC(CGraphClass, CWnd)
>
> CGraphClass::CGraphClass()
> {
> RegisterWindowClass();
> }
>
> CGraphClass::~CGraphClass()
> {
> }
>
> // Register the window class if it has not already been registered.
> BOOL CGraphClass::RegisterWindowClass()
> {
> WNDCLASS wndcls;
>
> if (!(::GetClassInfo(AfxGetInstanceHandle(), LOCAL_CLASSNAME,
> &wndcls)))
> {
> // otherwise we need to register a new class
> wndcls.style = CS_DBLCLKS | CS_HREDRAW |
> CS_VREDRAW;
> wndcls.lpfnWndProc = ::DefWindowProc;
> wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
> wndcls.hInstance = AfxGetInstanceHandle();
> wndcls.hIcon = NULL;
> wndcls.hCursor = AfxGetApp()-
>> LoadStandardCursor(IDC_ARROW);
> wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
> wndcls.lpszMenuName = NULL;
> wndcls.lpszClassName = LOCAL_CLASSNAME;
>
> if (!AfxRegisterClass(&wndcls))
> {
> AfxThrowResourceException();
> return FALSE;
> }
> }
>
> return TRUE;
> }
>
> BEGIN_MESSAGE_MAP(CGraphClass, CWnd)
> ON_WM_PAINT()
> ON_WM_TIMER()
> ON_WM_ERASEBKGND()
> END_MESSAGE_MAP()
>
> // CGraphClass message handlers
> void CGraphClass::OnTimer(UINT TimerVal)
> {
> // ****** processing event ******
> if (TimerVal == IDT_TIMER_0)
> {
> gridOffset++;
> if (gridOffset > 12.5)
> gridOffset = 0.0;
>
> Invalidate(); // to call OnPaint()
> UpdateWindow();
> }
> // call base class handler
> CWnd::OnTimer(TimerVal);
> }
>
> void CGraphClass::OnPaint()
> {
> CPaintDC dc(this); // device context for painting
>
> CRect r;
> GetClientRect(&r);
> dc.SetBkColor(RGB(0,0,0));
>
> int saveobject = dc.SaveDC();
>
> if(bSetDraw)
> {
> // ********** Background ***********
> // Grid
>
> CDC memDC;
> CBitmap bBitmap; // Offscreen bitmap
> CBitmap* bOldBitmap; // bitmap originally found in CMemDC
>
> memDC.CreateCompatibleDC(&dc);
> bBitmap.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
> bOldBitmap = memDC.SelectObject(&bBitmap);
>
> memDC.SetMapMode(MM_ANISOTROPIC);
> memDC.SetWindowExt(-r.Width(), iYMaxInterval);
> memDC.SetViewportExt(r.Width(), -r.Height());
> memDC.SetWindowOrg(r.Width(), 0);
> memDC.SetViewportOrg(0, r.Height());
>
> memDC.FillRect(r,
> CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));
>
> if (bActivateGrid)
> {
> CPen qLinePen(PS_SOLID, 1, RGB(0,139,0));
> memDC.SelectObject(qLinePen);
>
> // Grid - Horizontal lines
> for(int y = 0; y < r.bottom; y += (int)12) {
> /* scan y */
> memDC.MoveTo(r.left, y);
> memDC.LineTo(r.right, y);
> } /* scan y */
>
> // Grid - Vertical lines
> for(int x = 0 - (int)gridOffset; x <= r.right - (int)gridOffset; x
> += (int)12 ) {
> /* scan x */
> memDC.MoveTo(x, r.top);
> memDC.LineTo(x, r.bottom);
> } /* scan x */
> }
>
> if (vtPoints.capacity() == 1)
> {
> for (int i=vtPoints[0].size()-1;i>=0;i--) {
> vtToDraw[0].at(i).x = vtPoints[0].at(i).x;
> vtToDraw[0].at(i).y = ( (vtPoints[0].at(i).y > 0) ?
> vtPoints[0].at(i).y : 86 );
> }
> //int savedraw = memDC.SaveDC();
>
> CPen qPolylinePen(PS_SOLID, 1, RGB(0, 255, 0));
> memDC.SelectObject(qPolylinePen);
> vectfPoints* pointsline = &vtToDraw[0];
> memDC.Polyline(&(*pointsline)[0], (int)pointsline->size());
> //dc.PolyPolyline()
>
> //memDC.RestoreDC(savedraw);
> }
>
> // Copy the offscreen bitmap onto the screen.
> //memDC.SetMapMode(MM_TEXT);
>
> dc.BitBlt(r.left, r.top, r.Width(), r.Height(),
> &memDC, r.left, r.top, SRCCOPY);
>

What happens with
memDC.SetMapMode(MM_TEXT);
dc.BitBlt(0,0,r.Width(),r.Height(),&memDC,0,0,SRCCPY);

???
Both memDC and dc are then using MM_TEXT, have the same size bitmaps, so
this should work. Is there something going on with bSetDraw?

Steve



> //Swap back the original bitmap.
> memDC.SelectObject(bOldBitmap);
> }
> dc.RestoreDC(saveobject);
> }
>
> BOOL CGraphClass::OnEraseBkgnd(CDC* pDC)
> {
> //return CWnd::OnEraseBkgnd(pDC);
> return FALSE;
> }
>
> // ****************************** ATTRIBUTES
> ******************************
> void CGraphClass::set_GraphType(GRAPH_TYPE graphtype)
> {
> iGraphType = graphtype;
> }
>
> GRAPH_TYPE CGraphClass::get_GraphType()
> {
> return (GRAPH_TYPE)iGraphType;
> }
>
> // ******************************* METHODS
> *******************************
>
> BOOL CGraphClass::InitializeGraph(int maxsizearray, int maxinterval,
> GRAPH_TYPE typegraph/*=GRAPH_LINES*/)
> {
> // Initializing graph's DC...
> iGraphType = typegraph;
> gridOffset = 0;
> bActivateGrid = TRUE;
> bSetDraw = TRUE;
> nElapse = 1000;
> iTimerVal = 0;
>
> iYMaxInterval = maxinterval;
> iSourceMaxSizeArray = maxsizearray; // getting max size array of
> each source which has to be fixed
> vtPoints.resize(1);
> vtPoints[0].resize(iSourceMaxSizeArray);
> vtToDraw.resize(1);
> vtToDraw[0].resize(iSourceMaxSizeArray);
>
> CRect rc;
> GetClientRect(&rc);
> int iPointOfOrigin = 0;
>
> for (int i=vtPoints[0].size()-1;i>=0;i--) // initializing this
> first source to initial values
> { // which are consecutive integer values for X-axis
> vtPoints[0].at(i).x = iPointOfOrigin++; // and bottom screen value
> for Y-axis
> vtPoints[0].at(i).y = 0;
> vtToDraw[0].at(i).x = iPointOfOrigin;
> vtToDraw[0].at(i).y = 0;
> }
>
> iTimerVal = SetTimer(IDT_TIMER_0, nElapse, 0);
>
> return TRUE;
> }
>
> void CGraphClass::AddLinesElement(UINT iIndexSource, LPCTSTR
> description, double value)
> {
> // add in value format because CPoint depends from resize...
> int i;
> CRect rc;
> GetClientRect(&rc);
>
> vtPoints.resize(iIndexSource+1); // setting the array to max
> size defined
> vtPoints[iIndexSource].resize(iSourceMaxSizeArray);
> int iPointOfOrigin = 0;
> for (i=vtPoints[iIndexSource].size()-1;i>=0;i--) // initializing
> this first source to initial values
> { // which are consecutive integer values for X-axis
> vtPoints[iIndexSource].at(i).x = iPointOfOrigin++; // and bottom
> screen value for Y-axis
> vtPoints[iIndexSource].at(i).y = 0;
> }
>
> // Now I add element value to the source
> rotate(vtPoints[iIndexSource].begin(), vtPoints[iIndexSource].begin()
> +1, vtPoints[iIndexSource].end()); // SHIFT-LEFT the array of POINTs
> to add new value
> vtPoints[iIndexSource].at(vtPoints[iIndexSource].size() - 1).y =
> (LONG)value;
> iPointOfOrigin = 0;
> for (i=vtPoints[iIndexSource].size()-1;i>=0;i--)
> vtPoints[iIndexSource].at(i).x = iPointOfOrigin++;
>
> Invalidate();
> UpdateWindow();
> }
>
> BOOL CGraphClass::EnableTimer()
> {
> //
> ***************************************************************************
> // if enabled it's a realtime task manager, otherwise it'd be a
> graphical
> // representation of values passed from external source
> //
> ***************************************************************************
>
> iTimerVal = SetTimer(IDT_TIMER_0, nElapse, 0);
>
> return TRUE;
> }
>
> Ciao,
> Luigi