thank you for you time in advance.
Very new to MFC and attempting to finish a cs project before Sunday. We are to create a simple program that displays polygons in a grid. Rectangles are working great, but as I begin to add triangles, I cannot for the of me figure out why the triangle dialog box refuses to display. Here is some code:
CEquilDialog.h:
#include <afxwin.h>
class CEquilDialog : public CDialog {
public:
CEquilDialog();
afx_msg void OnOK();
afx_msg void OnCancel();
int m_nSideLength;
COLORREF m_Color;
private:
DECLARE_MESSAGE_MAP()
};
CEquilDialog.cpp:
#include "CEquilDialog.h"
#include "CEquilateralIds.h"
const int TEXT_MAX = 20;
CEquilDialog::CEquilDialog() : CDialog("Equilateral Traingle") {
m_nSideLength = 0;
}
afx_msg void CEquilDialog::OnOK() {
char editText[TEXT_MAX + 1];
CEdit* SideLengthEdit = (CEdit* )(GetDlgItem(IDC_SideLength));
SideLengthEdit->GetWindowText(editText,TEXT_MAX);
m_nSideLength = atoi(editText);
if (m_nSideLength <= 0) {
EndDialog(!IDOK);
return;
}
int color = GetCheckedRadioButton(IDC_Red, IDC_Blue);
switch(color) {
case IDC_Red:
m_Color = RGB(255,0,0);
break;
case IDC_Yellow:
m_Color = RGB(255,255,0);
break;
case IDC_Blue:
m_Color = RGB(0,0,255);
break;
default:
m_Color = RGB(255,255,255);
}
EndDialog(IDOK);
}
afx_msg void CEquilDialog::OnCancel() {
m_nSideLength = 0;
EndDialog(!IDOK);
}
BEGIN_MESSAGE_MAP(CEquilDialog, CDialog)
ON_COMMAND(IDC_OK, OnOK)
ON_COMMAND(IDC_Cancel, OnCancel)
END_MESSAGE_MAP()
CEquilateralIds.h
#define IDC_OK 2000
#define IDC_Cancel 2011
#define IDC_SideLength 2012
#define IDC_Red 2013
#define IDC_Yellow 2014
#define IDC_Blue 2015
Equilateral.rc (resource file)
#include <afxres.h>
#include "CEquilateralIds.h"
Equilateral DIALOG 50,50,150,150
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Create Triangle"
{
LTEXT "Enter Side Length", IDC_STATIC, 10, 5, 50, 8
EDITTEXT IDC_SideLength, 25, 15, 60, 16
GROUPBOX "Select color", IDC_STATIC, 10, 70, 60+15, 50
AUTORADIOBUTTON "Red", IDC_Red, 25, 80, 50, 16, WS_GROUP
AUTORADIOBUTTON "Yellow", IDC_Yellow, 25, 91, 50, 16
AUTORADIOBUTTON "Blue", IDC_Blue, 25, 102, 50, 16
PUSHBUTTON "OK", IDC_OK, 10, 125, 30, 15, NOT WS_TABSTOP
PUSHBUTTON "Cancel", IDC_Cancel, 10+60+15, 125, 30, 15, NOT WS_TABSTOP
}
All of this code is identical to my rectangle files (same files just with rectangle instead of equil, also with height and width instead of just SideLength) -- and here is the CShapesWin.cpp (where the dialog box gets called):
#include <afxwin.h>
#include "CShapesWin.h"
#include "CRectDialog.h"
#include "CEquilDialog.h"
#include "CRectangleIds.h"
#include "CEquilateralIds.h"
CShapesWin::CShapesWin() {
Create(NULL, "DrawShapes");
}
afx_msg void CShapesWin::OnPaint() {
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
m_doc.Paint(dc, rect);
}
afx_msg void CShapesWin::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
CRectDialog rectDialog;
CEquilDialog equilDialog;
switch(nChar) {
case 38: // Up arrow
case 40: // Down arrow
// Pop up a dialog box and get the response
if (rectDialog.DoModal() == IDOK) {
if (m_doc.Add(new CRectangle(rectDialog.m_nHeight,
rectDialog.m_nWidth, rectDialog.m_Color)) == TRUE) {
Invalidate(TRUE);
}
}
break;
case 39: // Right arrow
case 37: // Left arrow
if (equilDialog.DoModal() == IDOK) {
if (m_doc.Add(new CEquilateral(equilDialog.m_nSideLength,
equilDialog.m_Color)) == TRUE) {
Invalidate(TRUE);
}
}
else {
MessageBox("Whoops, no dialoge box... :(");
}
break;
default:
MessageBox("Key not recognized");
}
}
BEGIN_MESSAGE_MAP(CShapesWin, CFrameWnd)
ON_WM_PAINT()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
With debugging, I have seen the call to equilDialog.DoModel() attempt to be called, but it fails everytime, whereas my rect.Dialog.DoModel() never fails.... I am at a complete lost, if anyone could help I would be so grateful!
EDIT: Thank you Brian, It seems I forgot how to use a computer! Here is a public link with the zip file: http://dl.dropbox.com/u/1734050/SO%20Polygon%20Project.zip
EDIT 2: Martin, Thank you so much, The only thing I had to do was make sure that the string in the CEquilDialog.cpp file matched in the resource file. Once I did that, the dialog box worked like a charm.
FOR ANYONE new to MFC and having Dialog box issues, please remeber the following:
In any of your dialog.cpp files when you declare the constructor:
CYourDialog::CYourDialog() : CDialog("StringToMatchInResourceFile") {
m_nSomeVariable = 0;
}
the "StringToMatchInResourceFile" must also be in the .rc file:
#include <afxres.h>
#include "CEquilateralIds.h"
StringToMatchInResourceFile DIALOG 50,50,150,150
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
...
What I did was have "Equilateral Triangle" in the dialog.cpp and then "Equilateral" in the .rc file. MFC Newbies, *take note!*a