解码标志无法正常工作

时间:2016-11-19 11:49:55

标签: c++ mfc flags

我设计了一些标志:

enum ImportAssignment
{
    OCLMChairman     = 0x00000001,
    OCLMOpenPrayer   = 0x00000002,
    OCLMClosePrayer  = 0x00000004,
    OCLMConductorCBS = 0x00000008,
    OCLMReaderCBS    = 0x00000016,
    PTChairman       = 0x00000032,
    PTHospitality    = 0x00000064,
    WTConductor      = 0x00000128,
    WTReader         = 0x00000256
};

在我的对话框中,我从/向注册表读取/写入标志:

void CImportOCLMAssignmentHistoryDlg::ReadSettings()
{
    m_dwImportFlags = theApp.GetNumberSetting(theApp.GetActiveScheduleSection(_T("Options")), _T("ImportFlags"), 0);
}


void CImportOCLMAssignmentHistoryDlg::SaveSettings()
{
    theApp.SetNumberSetting(theApp.GetActiveScheduleSection(_T("Options")), _T("ImportFlags"), m_dwImportFlags);
}

SetNumberSetting基本上是GetProfileInt等的包装器。

然后我有两种方法将标志编码和解码为一系列BOOL变量(复选框):

void CImportOCLMAssignmentHistoryDlg::DecodeImportFlags()
{
    m_bImportOCLMChairman = (m_dwImportFlags & ImportAssignment::OCLMChairman);
    m_bImportOCLMOpenPrayer = (m_dwImportFlags & ImportAssignment::OCLMOpenPrayer);
    m_bImportOCLMClosePrayer = (m_dwImportFlags & ImportAssignment::OCLMClosePrayer);
    m_bImportOCLMConductorCBS = (m_dwImportFlags & ImportAssignment::OCLMConductorCBS);
    m_bImportOCLMReaderCBS = (m_dwImportFlags & ImportAssignment::OCLMReaderCBS);
    m_bImportPTChairman = (m_dwImportFlags & ImportAssignment::PTChairman);
    m_bImportPTHospitality = (m_dwImportFlags & ImportAssignment::PTHospitality);
    m_bImportWTConductor = (m_dwImportFlags & ImportAssignment::WTConductor);
    m_bImportWTReader = (m_dwImportFlags & ImportAssignment::WTReader);
}


void CImportOCLMAssignmentHistoryDlg::EncodeImportFlags()
{
    m_dwImportFlags = 0; // Reset
    if (m_bImportOCLMChairman) m_dwImportFlags |= ImportAssignment::OCLMChairman;
    if (m_bImportOCLMOpenPrayer) m_dwImportFlags |= ImportAssignment::OCLMOpenPrayer;
    if (m_bImportOCLMClosePrayer) m_dwImportFlags |= ImportAssignment::OCLMClosePrayer;
    if (m_bImportOCLMConductorCBS) m_dwImportFlags |= ImportAssignment::OCLMConductorCBS;
    if (m_bImportOCLMReaderCBS) m_dwImportFlags |= ImportAssignment::OCLMReaderCBS;
    if (m_bImportPTChairman) m_dwImportFlags |= ImportAssignment::PTChairman;
    if (m_bImportPTHospitality) m_dwImportFlags |= ImportAssignment::PTHospitality;
    if (m_bImportWTConductor) m_dwImportFlags |= ImportAssignment::WTConductor;
    if (m_bImportWTReader) m_dwImportFlags |= ImportAssignment::WTReader;
}

当我第一次运行应用程序时,复选框未被取消。然后我勾选一对。我关闭对话框并重新打开它。总是前两个被勾选。

我支持64x和32x版本。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

在进一步调试时,我找到了解决问题的方法。

我必须调整 DecodeImportFlags 方法:

void CImportOCLMAssignmentHistoryDlg::DecodeImportFlags()
{
    m_bImportOCLMChairman = (m_iImportFlags & ImportAssignment::OCLMChairman) ? TRUE : FALSE;
    m_bImportOCLMOpenPrayer = (m_iImportFlags & ImportAssignment::OCLMOpenPrayer) ? TRUE : FALSE;
    m_bImportOCLMClosePrayer = (m_iImportFlags & ImportAssignment::OCLMClosePrayer) ? TRUE : FALSE;
    m_bImportOCLMConductorCBS = (m_iImportFlags & ImportAssignment::OCLMConductorCBS) ? TRUE : FALSE;
    m_bImportOCLMReaderCBS = (m_iImportFlags & ImportAssignment::OCLMReaderCBS) ? TRUE : FALSE;
    m_bImportPTChairman = (m_iImportFlags & ImportAssignment::PTChairman) ? TRUE : FALSE;
    m_bImportPTHospitality = (m_iImportFlags & ImportAssignment::PTHospitality) ? TRUE : FALSE;
    m_bImportWTConductor = (m_iImportFlags & ImportAssignment::WTConductor) ? TRUE : FALSE;
    m_bImportWTReader = (m_iImportFlags & ImportAssignment::WTReader) ? TRUE : FALSE;
}

m_iImportFlags & ImportAssignment::XXXXX返回实际的标志值。所以我需要条件测试才能正确设置BOOL

更新:这些是我现在声明标志的方式:

enum ImportAssignment
{
    /*
    OCLMChairman     = 1,
    OCLMOpenPrayer   = 2,
    OCLMClosePrayer  = 4,
    OCLMConductorCBS = 8,
    OCLMReaderCBS    = 16,
    PTChairman       = 32,
    PTHospitality    = 64,
    WTConductor      = 128,
    WTReader         = 256*/
    None             = 0,
    OCLMChairman     = 1 << 0,
    OCLMOpenPrayer   = 1 << 1,
    OCLMClosePrayer  = 1 << 2,
    OCLMConductorCBS = 1 << 3,
    OCLMReaderCBS    = 1 << 4,
    PTChairman       = 1 << 5,
    PTHospitality    = 1 << 6,
    WTConductor      = 1 << 7,
    WTReader         = 1 << 8
};