Click here to Skip to main content
15,920,217 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to simulate a Mouse Click Pin
Hamid_RT24-May-07 10:34
Hamid_RT24-May-07 10:34 
AnswerRe: How to simulate a Mouse Click Pin
Perspx22-May-07 20:11
Perspx22-May-07 20:11 
Questiongeneral error c101008a: Failed to save manifest Pin
bruccutler22-May-07 12:33
bruccutler22-May-07 12:33 
AnswerRe: general error c101008a: Failed to save manifest Pin
bruccutler22-May-07 12:49
bruccutler22-May-07 12:49 
GeneralRe: general error c101008a: Failed to save manifest Pin
Member 1127422129-Nov-14 20:08
Member 1127422129-Nov-14 20:08 
Question11 digit numbers Pin
locoone22-May-07 12:27
locoone22-May-07 12:27 
AnswerRe: 11 digit numbers Pin
GameProfessor22-May-07 16:39
GameProfessor22-May-07 16:39 
QuestionTrouble making a custom mfc control... Pin
CoffeeAddict1922-May-07 12:14
CoffeeAddict1922-May-07 12:14 
I'm trying to make a MFC control that will show a pie graph. I've got most of the work done, but I don't know how to go from having a class to "creating" the control on the main window. I also keep getting debug assertions which I did not get when I implemented the same code in the main CFrameWnd class. I'm guessing that's because it won't give me access to the device contexts or something in a regular class. I don't know. Any help would be appreciated.

I'm using CWnd as a base class. Basically I have two bitmaps (m_pieBitmap and m_textBitmap) which are selected into two CDCs (PieMemDC and TextMemDC). When the pie graph and text need to be updated, they are redrawn onto the bitmaps. Both are blitted onto CPaintDC in OnPaint in the control's class.

Here is the header for CPieGraph (the control's class):

class CPieGraph : public CWnd
{
public:
	CPieGraph();
	~CPieGraph();
	void InitPieChart();
	void SetPieChart(PieGraphDisplayType & PieGraph);
	void ResetPieChartAngle(int Angle);
protected:
private:
	void InitDCObjects();
	void RedrawPieChart();
	void RedrawPieText();
	PieGraphDisplayType MainPieGraph;
	CDC PieMemDC;
	CDC TextMemDC;
	CBitmap* OldpieBitmap;
	CBitmap* OldtextBitmap;
	CBitmap m_pieBitmap;
	CBitmap m_textBitmap;
	CBrush CombinedBrush;
	CBrush TargetBrush;
	CPen BlackPen;
	//CBrush WhiteBrush;
	CFont OutputFont;
	//message handlers
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
DECLARE_MESSAGE_MAP()
};


Some of the class code:

CPieGraph::CPieGraph()
{
	InitPieChart();
	InitDCObjects();
	OldpieBitmap = PieMemDC.SelectObject(&m_pieBitmap);
	OldtextBitmap = TextMemDC.SelectObject(&m_textBitmap);
}
CPieGraph::~CPieGraph()
{
	PieMemDC.SelectObject(OldpieBitmap);
	TextMemDC.SelectObject(OldtextBitmap);
	m_pieBitmap.DeleteObject();
	m_textBitmap.DeleteObject();
}
void CPieGraph::InitPieChart()
{
	CRect ClientArea;
	GetClientRect(&ClientArea);

	MainPieGraph.origin.SetPoint(static_cast<int>(ClientArea.right / 1.5), 
		static_cast<int>(ClientArea.bottom / 2));
	MainPieGraph.angle = 0;
	MainPieGraph.PieGraphIsActive = false;
	MainPieGraph.radius = 100;
	MainPieGraph.data.denominator = 0.0;
	MainPieGraph.data.numerator = 0.0;
	MainPieGraph.data.ratio = 0.0;
}
void CPieGraph::InitDCObjects()
{
	CDC pDC;
	VERIFY(OutputFont.CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET,            
			OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
			DEFAULT_PITCH | FF_SWISS, "Arial"));
	BlackPen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
	TargetBrush.CreateSolidBrush(RGB(95, 158, 160));
	CombinedBrush.CreateHatchBrush(HS_DIAGCROSS, RGB(47, 79, 79));
	m_pieBitmap.CreateCompatibleBitmap(&pDC, 600, 600);
	m_textBitmap.CreateCompatibleBitmap(&pDC, 100, 10);
}
void CPieGraph::RedrawPieChart()
{
	ASSERT((MainPieGraph.radius * 2) < 600);

	CBrush* OldBrush = NULL;
	CPen* OldPen = NULL;
	int Angle = 0;

	if(MainPieGraph.PieGraphIsActive)
		Angle = MainPieGraph.angle;
	else
		Angle = 0;
	
	//draw pie 1
	PieMemDC.BeginPath();
	OldBrush = PieMemDC.SelectObject(&TargetBrush);
	OldPen = PieMemDC.SelectObject(&BlackPen);
	PieMemDC.MoveTo(MainPieGraph.radius, MainPieGraph.radius);
	AngleArc(PieMemDC, MainPieGraph.radius, MainPieGraph.radius, MainPieGraph.radius, 0.0, static_cast<FLOAT>(Angle));
	PieMemDC.LineTo(MainPieGraph.radius, MainPieGraph.radius);
	PieMemDC.EndPath();
	PieMemDC.StrokeAndFillPath();
	PieMemDC.SelectObject(OldBrush);
	
	//draw pie 2
	PieMemDC.BeginPath();
	OldBrush = PieMemDC.SelectObject(&CombinedBrush);
	PieMemDC.MoveTo(MainPieGraph.radius, MainPieGraph.radius);
	AngleArc(PieMemDC, MainPieGraph.radius, MainPieGraph.radius, MainPieGraph.radius, ((360-Angle)*-1), 360-Angle);
	PieMemDC.LineTo(MainPieGraph.radius, MainPieGraph.radius);
	PieMemDC.EndPath();
	PieMemDC.StrokeAndFillPath();
	PieMemDC.SelectObject(OldPen);
	PieMemDC.SelectObject(OldBrush);
}
afx_msg void CPieGraph::OnPaint()
{
	CPaintDC dc(this);

	dc.BitBlt(MainPieGraph.origin.x - MainPieGraph.radius,
		MainPieGraph.origin.y - MainPieGraph.radius - 10,
		100, 10, &TextMemDC, 0, 0, SRCCOPY);
	dc.BitBlt(MainPieGraph.origin.x - MainPieGraph.radius,
		MainPieGraph.origin.y - MainPieGraph.radius,
		MainPieGraph.radius * 2,
		MainPieGraph.radius * 2, &PieMemDC, 0, 0, SRCCOPY);
}
afx_msg BOOL CPieGraph::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

Class where CPieGraph is decleared:
class CRatiosWin : public CFrameWnd
{
public:
	CRatiosWin(); //default constructor
	~CRatiosWin(); //destructor
...
protected:
private:
	//classes
	CRatiosNewSearchDialog NewSearchDialog;
	CRatiosSearchTimeParameters TimeParametersDialog;
	CStudentCourseLinkedList TestLinkedList;
	CPieGraph PieGraph;
	CDC logo_memDC;
	CBitmap LogoOffScreenBmp;
	CBitmap* OldLogoBitmap;
	CEdit ResultsBox;
	//graphics related members
	CPoint LogoOffSet;
	CPoint TextOffSet;
...

}

AnswerRe: Trouble making a custom mfc control... Pin
Optimus Chaos22-May-07 20:29
Optimus Chaos22-May-07 20:29 
AnswerRe: Trouble making a custom mfc control... Pin
Mark Salsbery23-May-07 6:52
Mark Salsbery23-May-07 6:52 
Questiondll to lib Pin
llp00na22-May-07 11:37
llp00na22-May-07 11:37 
AnswerRe: dll to lib Pin
User 58385222-May-07 19:39
User 58385222-May-07 19:39 
GeneralRe: dll to lib Pin
llp00na23-May-07 1:56
llp00na23-May-07 1:56 
AnswerRe: dll to lib Pin
Mark Salsbery23-May-07 6:55
Mark Salsbery23-May-07 6:55 
QuestionAccessing the CView class from the CWinApp class? Pin
MasterShin22-May-07 11:03
MasterShin22-May-07 11:03 
AnswerRe: Accessing the CView class from the CWinApp class? Pin
Arman S.22-May-07 19:55
Arman S.22-May-07 19:55 
QuestionBitmap sappear as different colors Pin
Reagan Conservative22-May-07 9:56
Reagan Conservative22-May-07 9:56 
QuestionRe: Bitmap sappear as different colors Pin
CPallini22-May-07 10:08
mveCPallini22-May-07 10:08 
AnswerRe: Bitmap sappear as different colors Pin
Reagan Conservative22-May-07 10:17
Reagan Conservative22-May-07 10:17 
QuestionRe: Bitmap sappear as different colors Pin
Mark Salsbery23-May-07 6:58
Mark Salsbery23-May-07 6:58 
AnswerRe: Bitmap sappear as different colors Pin
Reagan Conservative23-May-07 8:03
Reagan Conservative23-May-07 8:03 
GeneralRe: Bitmap sappear as different colors Pin
Mark Salsbery23-May-07 8:55
Mark Salsbery23-May-07 8:55 
GeneralRe: Bitmap sappear as different colors Pin
Reagan Conservative23-May-07 9:27
Reagan Conservative23-May-07 9:27 
GeneralRe: Bitmap sappear as different colors Pin
Mark Salsbery23-May-07 12:35
Mark Salsbery23-May-07 12:35 
GeneralRe: Bitmap sappear as different colors Pin
Reagan Conservative24-May-07 3:10
Reagan Conservative24-May-07 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.