我编写了一段代码来将窗口样式值解析为文本宏,但输出与样式值有很大不同;为什么以及如何解决它?
输出:
WS_CHILD | WS_VISIBLE | WS_VISIBLE | BS_GROUPBOX | BS_DEFPUSHBUTTON | BS_DEFPUSHBUTTON | BS_3STATE | BS_AUTOCHECKBOX | BS_自动单选按钮 | BS_OWNERDRAW | BS_TYPEMASK | SS_ETCHEDVERT | SS_OWNERDRAW | SS_TYPEMASK |
代码:
#include <windows.h>
#include <unordered_map>
#include <string>
#include <iostream>
class StyleMapSingleton {
private:
std::unordered_map<DWORD, std::wstring> styleMap;
StyleMapSingleton() {
styleMap = {
{WS_OVERLAPPED, L"WS_OVERLAPPED"},
{WS_POPUP, L"WS_POPUP"},
{WS_VISIBLE, L"WS_VISIBLE"},
{WS_CHILD, L"WS_CHILD"},
{WS_MINIMIZE, L"WS_MINIMIZE"},
{WS_DISABLED, L"WS_DISABLED"},
{WS_CLIPSIBLINGS, L"WS_CLIPSIBLINGS"},
{WS_CLIPCHILDREN, L"WS_CLIPCHILDREN"},
{WS_MAXIMIZE, L"WS_MAXIMIZE"},
{WS_BORDER, L"WS_BORDER"},
{WS_DLGFRAME, L"WS_DLGFRAME"},
{WS_VSCROLL, L"WS_VSCROLL"},
{WS_HSCROLL, L"WS_HSCROLL"},
{WS_SYSMENU, L"WS_SYSMENU"},
{WS_THICKFRAME, L"WS_THICKFRAME"},
{WS_GROUP, L"WS_GROUP"},
{WS_TABSTOP, L"WS_TABSTOP"},
{WS_MINIMIZEBOX, L"WS_MINIMIZEBOX"},
{WS_MAXIMIZEBOX, L"WS_MAXIMIZEBOX"},
{BS_PUSHBUTTON, L"BS_PUSHBUTTON"},
{BS_DEFPUSHBUTTON, L"BS_DEFPUSHBUTTON"},
{BS_RADIOBUTTON, L"BS_RADIOBUTTON"},
{BS_CHECKBOX, L"BS_CHECKBOX"},
{BS_3STATE, L"BS_3STATE"},
{BS_AUTO3STATE, L"BS_AUTO3STATE"},
{BS_AUTOCHECKBOX, L"BS_AUTOCHECKBOX"},
{BS_AUTORADIOBUTTON, L"BS_AUTORADIOBUTTON"},
{BS_GROUPBOX, L"BS_GROUPBOX"},
{BS_USERBUTTON, L"BS_USERBUTTON"},
{BS_BITMAP, L"BS_BITMAP"},
{BS_ICON, L"BS_ICON"},
{BS_FLAT, L"BS_FLAT"},
{BS_MULTILINE, L"BS_MULTILINE"},
{BS_OWNERDRAW, L"BS_OWNERDRAW"},
{BS_PUSHLIKE, L"BS_PUSHLIKE"},
{BS_LEFT, L"BS_LEFT"},
{BS_RIGHT, L"BS_RIGHT"},
{BS_CENTER, L"BS_CENTER"},
{BS_TOP, L"BS_TOP"},
{BS_BOTTOM, L"BS_BOTTOM"},
{BS_VCENTER, L"BS_VCENTER"},
{BS_RIGHTBUTTON, L"BS_RIGHTBUTTON"},
{BS_LEFTTEXT, L"BS_LEFTTEXT"},
{BS_TEXT, L"BS_TEXT"},
{BS_NOTIFY, L"BS_NOTIFY"},
{BS_TYPEMASK, L"BS_TYPEMASK"},
{CBS_AUTOHSCROLL, L"CBS_AUTOHSCROLL"},
{CBS_DISABLENOSCROLL, L"CBS_DISABLENOSCROLL"},
{CBS_DROPDOWN, L"CBS_DROPDOWN"},
{CBS_DROPDOWNLIST, L"CBS_DROPDOWNLIST"},
{CBS_HASSTRINGS, L"CBS_HASSTRINGS"},
{CBS_LOWERCASE, L"CBS_LOWERCASE"},
{CBS_NOINTEGRALHEIGHT, L"CBS_NOINTEGRALHEIGHT"},
{CBS_OEMCONVERT, L"CBS_OEMCONVERT"},
{CBS_OWNERDRAWFIXED, L"CBS_OWNERDRAWFIXED"},
{CBS_OWNERDRAWVARIABLE, L"CBS_OWNERDRAWVARIABLE"},
{CBS_SIMPLE, L"CBS_SIMPLE"},
{CBS_SORT, L"CBS_SORT"},
{ES_AUTOHSCROLL, L"ES_AUTOHSCROLL"},
{ES_AUTOVSCROLL, L"ES_AUTOVSCROLL"},
{ES_LEFT, L"ES_LEFT"},
{ES_CENTER, L"ES_CENTER"},
{ES_RIGHT, L"ES_RIGHT"},
{ES_LOWERCASE, L"ES_LOWERCASE"},
{ES_UPPERCASE, L"ES_UPPERCASE"},
{ES_OEMCONVERT, L"ES_OEMCONVERT"},
{ES_NUMBER, L"ES_NUMBER"},
{ES_PASSWORD, L"ES_PASSWORD"},
{ES_MULTILINE, L"ES_MULTILINE"},
{ES_WANTRETURN, L"ES_WANTRETURN"},
{ES_NOHIDESEL, L"ES_NOHIDESEL"},
{ES_READONLY, L"ES_READONLY"},
{SS_BLACKFRAME, L"SS_BLACKFRAME"},
{SS_GRAYFRAME, L"SS_GRAYFRAME"},
{SS_WHITEFRAME, L"SS_WHITEFRAME"},
{SS_ETCHEDFRAME, L"SS_ETCHEDFRAME"},
{SS_ETCHEDHORZ, L"SS_ETCHEDHORZ"},
{SS_ETCHEDVERT, L"SS_ETCHEDVERT"},
{SS_BLACKRECT, L"SS_BLACKRECT"},
{SS_WHITERECT, L"SS_WHITERECT"},
{SS_GRAYRECT, L"SS_GRAYRECT"},
{SS_CENTERIMAGE, L"SS_CENTERIMAGE"},
{SS_REALSIZEIMAGE, L"SS_REALSIZEIMAGE"},
{SS_REALSIZECONTROL, L"SS_REALSIZECONTROL"},
{SS_LEFT, L"SS_LEFT"},
{SS_CENTER, L"SS_CENTER"},
{SS_RIGHT, L"SS_RIGHT"},
{SS_RIGHTJUST, L"SS_RIGHTJUST"},
{SS_EDITCONTROL, L"SS_EDITCONTROL"},
{SS_SIMPLE, L"SS_SIMPLE"},
{SS_SUNKEN, L"SS_SUNKEN"},
{SS_ENDELLIPSIS, L"SS_ENDELLIPSIS"},
{SS_PATHELLIPSIS, L"SS_PATHELLIPSIS"},
{SS_WORDELLIPSIS, L"SS_WORDELLIPSIS"},
{SS_ICON, L"SS_ICON"},
{SS_BITMAP, L"SS_BITMAP"},
{SS_ENHMETAFILE, L"SS_ENHMETAFILE"},
{SS_LEFTNOWORDWRAP, L"SS_LEFTNOWORDWRAP"},
{SS_NOPREFIX, L"SS_NOPREFIX"},
{SS_NOTIFY, L"SS_NOTIFY"},
{SS_OWNERDRAW, L"SS_OWNERDRAW"},
{SS_TYPEMASK, L"SS_TYPEMASK"},
};
}
StyleMapSingleton(const StyleMapSingleton&) = delete;
void operator=(const StyleMapSingleton&) = delete;
public:
static StyleMapSingleton& getInstance() {
static StyleMapSingleton instance;
return instance;
}
const std::unordered_map<DWORD, std::wstring>& getStyleMap() const {
return styleMap;
}
};
void resolveWindowStyle(DWORD style, const std::unordered_map<DWORD, std::wstring>& styleMap) {
for (const auto& entry : styleMap) {
if (style & entry.first) {
std::wcout << entry.second << L" | ";
}
}
std::wcout << std::endl;
}
int main() {
const auto& styleMapInst = StyleMapSingleton::getInstance();
DWORD style = WS_CHILD | WS_VISIBLE | ES_CENTER;
resolveWindowStyle(style, styleMapInst.getStyleMap());
return 0;
}
常量不保证是唯一的。例如,
ES_CENTER
与其他三个 1
、BS_DEFPUSHBUTTON
和 CBS_SIMPLE
共享其值 (SS_CENTER
)。由于 unordered_map
中的 Key必须是唯一的,因此仅存储其中之一。您可能需要使用多重贴图以便能够显示所有常量。
某些常量设置了多个位。列表中显示的像
SS_TYPEMASK
这样的常量的值为 31
,它设置了 5 位。这些也可以与其他常量共享。
由于这两个原因,不可能将映射返回到您使用的确切常量。这种关系不是双射。