MFC:击键后不显示对话框

发布于 2024-11-09 04:39:14 字数 5050 浏览 1 评论 0原文

谢谢您提前抽出时间。

对 MFC 非常陌生,并试图在周日之前完成一个 CS 项目。我们要创建一个简单的程序,在网格中显示多边形。矩形工作得很好,但是当我开始添加三角形时,我无法弄清楚为什么三角形对话框拒绝显示。下面是一些代码:

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

Equi Later.rc (资源文件)

#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
}

所有这些代码与我的矩形文件相同(相同的文件只是用矩形而不是等值,还具有高度和宽度,而不仅仅是 SideLength) - 这是 CShapesWin.cpp (对话框被调用的地方):

#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()

通过调试,我看到尝试调用 equalDialog.DoModel() ,但它失败每次,而我的 rect.Dialog.DoModel() 永远不会失败......我完全迷失了,如果有人可以帮助我,我将非常感激!

编辑:谢谢布莱恩,看来我忘记了如何使用计算机!以下是包含 zip 文件的公共链接:http://dl.dropbox .com/u/1734050/SO%20Polygon%20Project.zip

编辑 2:Martin,非常感谢,我唯一要做的就是确保 CEquilDialog.cpp 文件中的字符串与资源文件。一旦我这样做了,对话框就变得很有魅力。

对于刚接触 MFC 并遇到对话框问题的任何人,请记住以下几点:

在任何dialog.cpp 文件中声明构造函数时:

CYourDialog::CYourDialog() : CDialog("StringToMatchInResourceFile") { m_nSomeVariable = 0; }

“StringToMatchInResourceFile”必须位于.rc 文件中:

#include <afxres.h>
#include "CEquilateralIds.h"

StringToMatchInResourceFile DIALOG 50,50,150,150
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
...

我所做的是在dialog.cpp 中添加“等边三角形”,然后在.rc 文件中添加“等边”。 MFC 新手,*请注意!*a

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2024-11-16 04:39:14

找不到您的对话框资源。当您调用基类构造函数 :CDialog("Equiside Triangles") 时,您是在告诉 MFC 和对话框管理器您有一个字符串 id 为“Equiside Triangles”的对话框模板资源。但你的 rc 文件没有这个。它有一个对话框资源,其 id 为 Equiterior(我没有看到你在任何地方定义)。而是将 IDD_EQUILATERAL 添加为 101 到 equalerialids.h 并在 CDialog 构造函数中引用它。

它可能适用于其他情况,因为您的 ID 及其字符串匹配。

补充一点,如果没有特殊的机制,每个项目只能获得一个 .rc 文件。因此,如果碰巧您有两个 rc 文件,请将它们合并为一个文件。这也可能会出错,因为第二个 RC 文件根本没有去任何地方。

马丁

Your dialog resource can't be found. When you call the base class constructor :CDialog("Equilateral Triangles"), you're telling MFC and the dialog manager that you have a dialog template resource with the string id "Equilateral Triangles". But your rc file doesn't have that. It has a dialog resource with the id Equilateral (which I don't see you define anywhere). Instead add IDD_EQUILATERAL as 101 to equilateralids.h and refer to it in the CDialog constructor.

It probably works right for the other case because your ID and its string match.

As an added point, without special mechanics you only get one .rc file per project. So if by chance you have two rc files, merge them into one file. This may also be going wrong because the second RC file isn't going anywhere at all.

Martyn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文