>>>There are few flaws in it.
>>>1. The first and most important thing is that the drawing part of this
>>>code
>>>should be in the OnPaint of the child control (IDC_PICT), and not the
>>>dialog. Because if it is not in the OnPaint of the control, then when
>>>the
>>>dialog is covered by another window it will not repaint itself when it is
>>>uncovered.
>>>2. The fact that it's not scaleable with the data might become a problem
>>>later, only 10 items......
>>>3. It is not releasing the DC of the child control, which causes a
>>>resource
>>>leak.
>>>4. It is not scalable when it comes to the size of the control.
>>>5. It doesn't have any labels or axes markers.
>>>
>>>Otherwise, if you like it feel free to use it.
>>>
>>>AliR.
"Kahlua" <kah...@right.here> wrote in message
news:G%7jj.10188$ac7.2274@trndny03...
> "Kahlua" <kah...@right.here> wrote in message
> news:zb6jj.7554$6F6.4662@trndny09...
>> Can anyone show me how to display a bargraph within my mfc app?
>> Or direct me to the resource to do it.
>> Lets say I have an unsigned char string of 16bytes of data ranging in
>> value from 01h to FFh.
>> I want to display a bargraph with the 16 vertical bars reflecting the
>> data value.
>> Height and width of bargraph not important at this time.
>> Thanks in advance.
>>
>>
>
> I found this simple code that seems to work ok.
> Anything wrong with it?
> Why should I not use it?
>
> void CBar1Dlg::OnBar()
> {
> unsigned char bar[10];
> int numbars=10;
> int a,b,c,d,i;
>
> for (i=0;i<10;i++) //initialize
> bargraph values for testing
> bar[i]=i*10;
>
> CRect cr;
> CBrush grn(RGB(100,255,100));
> c=15,b=5; //initialize
> positions
> GetDlgItem(IDC_PIC)->GetClientRect(cr); //get drawing area of PIC
> CWnd* pWnd=GetDlgItem(IDC_PIC);
>
> pControlDC=pWnd->GetDC();
>
> d = (cr.Width()-20)/numbars;
> pControlDC->SelectObject(wht); //draw
> background
> pControlDC->Rectangle(0,0,cr.Width(), cr.Height());
>
> pControlDC->SelectObject(grn); //select color of
> bar
> for (b=0; b<numkeys; b++)
> {
> a = bar[b];
> pControlDC->Rectangle (c, cr.Height(), c+10, cr.Height()-a);
> c+=d;
> }
> pControlDC->SelectStockObject(BLACK_BRUSH);
> }
>
>
Kahlua:
Reading about CBrush, CDC::SelectObject() and CDC::Rectangle() would be
a good start for a very simple bar graph.
--
David Wilkinson
Visual C++ MVP
Why aren't you using one of the chart controls posted on code project?
http://www.codeproject.com/KB/miscctrl/CBarChart.aspx
There are tons of them here on CodeGuru:
http://www.codeguru.com/cpp/controls/controls/
AliR.
"Kahlua" <kah...@right.here> wrote in message
news:Ogpjj.14360$Y63.1295@trnddc03...
"AliR (VC++ MVP)" <Al...@online.nospam> wrote in message
news:Toqjj.33375$4V6....@newssvr14.news.prodigy.net...
Kahlua:
For the simplest bar graph, just use CDC::Rectangle() to draw a series
of rectangles of the correct height.
Yes, you must do your painting in OnPaint() or OnDraw() if you want your
window to repaint after being covered. This is how the Windows printing
model works.
In order to accomplish what you want you will need to know how to make some
GDI calls to draw rectangles using brushes and pens, which I gave you link
for that. Take a look at the documentation for CDC and you should have
everything you need in order to do what you want.
The code that you posted does most of the work, but it had a resource leak,
which can be fixed by calling ReleaseDC at the end. But again the drawing
portion of that code should be in the OnPaint method of the control, and not
in the dialogbox.
I'll try to put an example together.
AliR.
"Kahlua" <kah...@right.here> wrote in message
news:0Drjj.5944$YW6.1505@trndny07...
//////////////////////////////////////////////////////////////////////////
// BarChart.h
#pragma once
#include <afxtempl.h>
// CBarChart
typedef CArray <int> IntArray;
class CBarChart : public CStatic
{
DECLARE_DYNAMIC(CBarChart)
public:
CBarChart();
virtual ~CBarChart();
void SetNumData(int NumData);
void SetData(int Index,int Value);
protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
private:
IntArray m_Array;
};
///////////////////////////////////////////////////////////////////////
// BarChart.cpp : implementation file
//
#include "stdafx.h"
#include "BarChart.h"
// CBarChart
IMPLEMENT_DYNAMIC(CBarChart, CStatic)
CBarChart::CBarChart()
{
}
CBarChart::~CBarChart()
{
}
BEGIN_MESSAGE_MAP(CBarChart, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CBarChart message handlers
void CBarChart::SetNumData(int NumData)
{
m_Array.SetSize(NumData);
}
void CBarChart::SetData(int Index,int Value)
{
if (Index < m_Array.GetCount())
{
m_Array.SetAt(Index,Value);
}
}
void CBarChart::OnPaint()
{
CPaintDC dc(this); // device context for painting
int SavedDC = dc.SaveDC();
CRect cr;
GetClientRect(&cr);
CBrush GreenBrush(RGB(100,255,100));
CBrush WhiteBrush(RGB(255,255,255));
int c=15,b=5;
int d = (cr.Width()-20)/m_Array.GetCount();
dc.SelectObject(WhiteBrush);
dc.Rectangle(0,0,cr.Width(), cr.Height());
dc.SelectObject(GreenBrush);
for (b=0; b<m_Array.GetCount(); b++)
{
int a = m_Array.GetAt(b);
dc.Rectangle(c, cr.Height(), c+20, cr.Height()-a);
c+=d;
}
dc.RestoreDC(SavedDC);
}
Put a Static control on your dialog, give an id other than IDC_STATIC. Then
attach a variable to it. Change the variable type from CStatic to
CBarChart. Then call SetNumData and SetData in your OnInitDialog
m_BarChart.SetNumData(5);
m_BarChart.SetData(0,10);
m_BarChart.SetData(1,20);
m_BarChart.SetData(2,50);
m_BarChart.SetData(3,100);
m_BarChart.SetData(4,35);
AliR.
"AliR (VC++ MVP)" <Al...@online.nospam> wrote in message
news:hBtjj.38666$lD6....@newssvr27.news.prodigy.net...