【问题标题】:No class or namespace on creating a dialog Box创建对话框时没有类或命名空间
【发布时间】:2012-07-16 03:05:27
【问题描述】:

我在 ATL dll 文件中创建了一个对话框,并添加了一个类来控制其性能。我最初设法使它编译,但现在(因为可能包括调整)我在编译时收到了奇怪的消息:

CTestDlg : 没有类或命名空间

下面我引用了header的源代码和cpp文件:

#pragma once

#include "resource.h" // Hauptsymbole
#include <atlhost.h>


class CTestDlg : public CAxDialogImpl<CTestDlg>
{
private:
bool m_cancel;


public:
CTestDlg()
{
    m_cancel = true;    
}

~CTestDlg()
{
}

enum { IDD = IDD_TESTDLG };

BEGIN_MSG_MAP(CTestDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
COMMAND_HANDLER(IDC_EDIT1, EN_CHANGE, OnEnChangeEdit1)
CHAIN_MSG_MAP(CAxDialogImpl<CTestDlg>)
END_MSG_MAP()



LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    CAxDialogImpl<CTestDlg>::OnInitDialog(uMsg, wParam, lParam, bHandled);
    bHandled = TRUE;

    //CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
    //pEdit->SetWindowTextW(L"Hello");
    CWindow textBox(GetDlgItem(IDC_EDIT1));
    textBox.SetWindowTextW(L"hello");
    //textBox.SendMessageW(WM_SETTEXT, 0, (LPARAM)L"test!!!");
    return 1;  // Das System kann den Fokus festlegen
}

LRESULT OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    EndDialog(wID);
    m_cancel = false;
    return 0;
}

LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    EndDialog(wID);
    return 0;
}

LRESULT OnEnChangeEdit1(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

bool IsCancelled() const { return m_cancel; }
bool saveFile();

};

//.cpp 文件

#include "CTestDlg.h"
#include "stdafx.h"




LRESULT CTestDlg::OnEnChangeEdit1(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
return 0;
}

bool CTestDlg::saveFile()
{

OPENFILENAME ofn;
WCHAR szFileName[MAX_PATH] = L"";


ZeroMemory( &ofn , sizeof( ofn));

ofn.lStructSize = sizeof(ofn); 
ofn.hwndOwner = NULL;
ofn.lpstrFilter = (LPCWSTR)L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = (LPWSTR)szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = (LPCWSTR)L"txt";

if(GetSaveFileNameW(&ofn))
{
    HANDLE hFile = CreateFile(ofn.lpstrFile, 
        GENERIC_WRITE,
        0,
        NULL, 
        CREATE_NEW,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    DWORD dwBytesWritten = 0;
    char str[] = "Example text testing WriteFile";
    WriteFile( hFile, str, strlen(str), &dwBytesWritten, NULL );
    CloseHandle(hFile); 

    return true;
}
else 
    return false;

}

有任何关于代码似乎有问题的迹象吗?

【问题讨论】:

  • 我们需要完整的错误信息。
  • 你在哪里使用这个类?在哪个文件中?
  • 错误 C2653:CTestDlg:没有类或命名空间。对不起,我提供的最初错误并不完全正确。我已经编辑了问题。
  • 你在 CTestDlg.h 中添加了#pragma once 吗?

标签: c++ dll dialog atl


【解决方案1】:

你的预编译头文件应该在 .cpp 文件中首先包含,更改:

#include "CTestDlg.h"
#include "stdafx.h"

#include "stdafx.h"
#include "CTestDlg.h"

还要仔细检查包含的文件名是否正确

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多