爱格格的小黄鸭 2015-11-07 02:41 采纳率: 0%
浏览 916

想请教各位,VS2013MFC控件变量是灰色的,是怎么回事,谢谢

图片说明

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 17:23
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Visual Studio 2013中,当您尝试访问一个MFC控件的变量时,如果这个变量没有被声明或初始化为适当的值,则可能会导致控制台应用程序崩溃。这通常发生在试图访问未定义或不可见的对象时。

    对于您的情况,可能的原因有:

    1. 控件变量尚未初始化:确保在类中正确地初始化了IDC_Enable控件变量。
    2. 变量类型的不匹配:确保变量类型与控件类型一致,并且变量名和控件名称相同。
    3. 变量未设置:确保已经为控件变量设置了正确的数据。

    例如,如果您在类中定义了一个名为EnableButton的成员变量,但没有为其分配任何初始值,那么在调用按钮状态方法时就会出现错误。

    以下是一个简单的示例,演示如何为一个MFC按钮创建一个控件变量并将其赋值:

    #include <windows.h>
    #include <mfcui.h>
    
    class MyForm : public CFormBase
    {
        DECLARE_DYNAMIC(MyForm)
    
    protected:
        // Declare a member variable for the button.
        CButton *pButton;
    
    public:
        MyForm();
        ~MyForm();
    
    private:
        void OnButtonClick(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
    
        // This function is called when the button state changes.
        void ButtonStateChanged(CButton* pButton);
    };
    
    MyForm::MyForm()
    {
        // Initialize the member variable.
        pButton = new CButton;
    }
    
    MyForm::~MyForm()
    {
        delete pButton;
    }
    
    void MyForm::OnButtonClick(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
    {
        if (pButton->GetState() == CBRS_DEFAULT)
        {
            // Change the state of the button to pressed.
            pButton->SetState(CBRS_PRESSED);
            bHandled = TRUE;
        }
    }
    
    void MyForm::ButtonStateChanged(CButton* pButton)
    {
        // Do something with the button's state change here.
    }
    

    在这个例子中,我们首先创建了一个CButton对象,然后通过AddRef()方法给它增加引用计数,以便我们可以对其进行管理和释放。然后,我们在OnButtonClick函数中检查按钮的状态,如果是默认状态,我们就将它的状态更改为按下状态。最后,在ButtonStateChanged函数中,我们处理按钮状态的变化。

    评论

报告相同问题?