我有一個按鈕控制包裝類,它允許您傳遞一個現有的句柄,只要它是WC_BUTTON
類。我用GetClassName()
來確定這一點。但我有一個問題,在代碼中的註釋應有助於形容它:奇怪的不平等 - WC_BUTTONW不等於L「Button」?
// Initialize from existing handle
Vivify::Button::Button(HWND handle) {
TCHAR cls[256];
GetClassName(handle, cls, sizeof(cls));
Alert(cls); // MessageBox says "Button"
Alert(WC_BUTTON); // MessageBox says "Button" also
Str clsStr = cls;
Str wcStr = WC_BUTTON;
Alert(ToStr<int>(clsStr.length())); // says "6"
Alert(ToStr<int>(wcStr.length())); // says "6" also
// Problem HERE. Evaluates to false. How are they inequal??
if (cls == WC_BUTTON) {
SetHandle(handle); // Never gets executed
m_id = GetDlgCtrlID(handle);
}
}
Str
是std::wstring
的方式,程序是用Unicode。 但兩者的字符串,我從GetClassName()
獲取和WC_BUTTON
都是Unicode字符串,都6
字符,都等於"Button"
,如何在地球上是返回false
行if (cls == WC_BUTTON)
?
有人可以解釋兩個看起來完全相同的字符串可以相等嗎? 或者我怎樣才能確定一個HWND
屬於一個按鈕/編輯/等。控制?
不要將C字符串與==比較。 – chris
@chris那我該如何比較呢? –
如果仍然是C字符串,則爲'strcmp'。因爲'vector'和'array '可以像C字符串一樣工作,所以你也可以使'cls'成爲另一個,並設置'data()'爲'WC_BUTTON',並使用==。我會說'std :: string',但是你不能直接設置它的緩衝區。 –
chris