コード例 #1
0
LRESULT CALLBACK ZLWin32ApplicationWindow::TextEditParameter::ComboBoxCallback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	TextEditParameter &parameter =
		*(TextEditParameter*)GetWindowLong(hWnd, GWL_USERDATA);

	if ((uMsg == WM_COMMAND) && (HIWORD(wParam) == CBN_SELCHANGE)) {
		HWND comboBox = parameter.myComboBox;
		const int index = SendMessage(comboBox, CB_GETCURSEL, 0, 0);
		const int length = SendMessage(comboBox, CB_GETLBTEXTLEN, index, 0);
		ZLUnicodeUtil::Ucs2String buffer;
		if (length > 0) {
			buffer.assign(length + 1, '\0');
			SendMessage(comboBox, CB_GETLBTEXT, index, (LPARAM)&buffer.front());
			buffer.pop_back();
		}
		std::string value;
		ZLUnicodeUtil::ucs2ToUtf8(value, buffer);
		parameter.setValue(value);

		parameter.myApplication.doAction(parameter.myParameterItem.actionId());
		SetFocus(parameter.myMainWindow);
	}

	WndProc orig = parameter.myOriginalComboBoxCallback;
	return orig(hWnd, uMsg, wParam, lParam);
}
コード例 #2
0
void ZLWin32EncodingConverter::convert(std::string &dst, const char *srcStart, const char *srcEnd) {
	if ((srcStart == srcEnd) || (myCode == 0)) {
		return;
	}

	myBuffer.clear();

	bool hasFirstChar = false;
	if (myUseStoredCharacter) {
		WCHAR symbol;
		char buf[2] = { myStoredCharacter, *srcStart };
		if (MultiByteToWideChar(myCode, 0, buf, 2, &symbol, 1) == 1) {
			hasFirstChar = true;
			myBuffer.push_back(symbol);
			srcStart++;
		}
		myUseStoredCharacter = false;
	}

	int len = srcEnd - srcStart;
	if (len > 0) {
		myBuffer.insert(myBuffer.end(), len, 0);
		WCHAR *bufferStart = (WCHAR*)&myBuffer.front();
		if (hasFirstChar) {
			bufferStart++;
		}
		int ucs2Len = MultiByteToWideChar(myCode, 0, srcStart, len, bufferStart, len);
		myBuffer.erase(myBuffer.begin() + ucs2Len + (hasFirstChar ? 1 : 0), myBuffer.end());

		if (ucs2Len != len) {
			myRBuffer.append(len, '\0');
			char defaultChar = 'X';
			BOOL usedDefaultChar = false;
			int len1 = WideCharToMultiByte(myCode, 0, bufferStart, ucs2Len, (char*)myRBuffer.data(), len, &defaultChar, &usedDefaultChar);
			if (len1 == len - 1) {
				myUseStoredCharacter = true;
				myStoredCharacter = *(srcEnd - 1);
			}
			myRBuffer.erase();
		}
	}

	std::string toAppend;
	ZLUnicodeUtil::ucs2ToUtf8(toAppend, myBuffer);
	dst += toAppend;
	myBuffer.clear();
}