|
-
January 19th, 2012, 05:10 PM
#8
Re: ComboBox + LPWSTR
>> wcstombs_s return EILSEQ
For wcstombs() to work on anything other than "C" locale characters (A-Z, a-z, 0-9, etc...), you must call setlocale(LC_CTYPE, "") so that the CRT will convert using the system ANSI codepage (ACP).
Even then, wcstombs() will fail if it encounters any characters that aren't in the ACP. I don't think that "ả" (U+1EA3) or "ị" (U+1ECB) are in any of the Windows codepages, so wcstombs() will never work on windows for those characters.
>> strWText = L"Quảng Trị";
Putting extended characters directly into your source can be dangerous. Anyone who edits/saves that source file will have to save it with a Unicode encoding.
Code:
#include <windows.h>
#include <stdio.h>
HFONT CreateFont(HDC hdc, int pitch, const wchar_t *facename);
const wchar_t *g_wide_string = L"Qu\u1EA3ng Tr\u1ECB";
//------------------------------------------------------------------------------
int main()
{
const char *className = "ANSI_MsgLoop_Test";
WNDCLASSA wincl = {0};
wincl.hInstance = GetModuleHandle(0);
wincl.lpszClassName = className;
wincl.lpfnWndProc = DefWindowProcA;
wincl.style = CS_DBLCLKS;
wincl.hIcon = LoadIcon(0, IDI_APPLICATION);
wincl.hCursor = LoadCursor(0, IDC_ARROW);
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
if (!RegisterClassA(&wincl))
return 0;
HWND parent;
parent = CreateWindowA(className, "ANSI Msg Loop",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
300, 100, HWND_DESKTOP, 0,
GetModuleHandle(0), 0);
if (!parent)
return 0;
HWND combo;
combo = CreateWindowW(L"ComboBox", L"",
WS_CHILD | WS_VISIBLE | WS_DISABLED | CBS_DROPDOWNLIST,
10, 10, 260, 30,
parent, (HMENU)1,
GetModuleHandle(0), 0);
if (!combo)
return 0;
// give it a nice font
HDC hdc = GetDC(combo);
HFONT hfont = CreateFont(hdc, 12, L"Times New Roman");
ReleaseDC(combo, hdc);
SendMessageA(combo, WM_SETFONT, (WPARAM)hfont, 0);
// add string and select it
SendMessageW(combo, CB_ADDSTRING, 0, (LPARAM)g_wide_string);
SendMessageA(combo, CB_SETCURSEL, 0, 0);
MSG msg;
BOOL bRet;
for (;;)
{
bRet = GetMessageA(&msg, parent, 0, 0);
if ((bRet == 0) || (bRet == -1))
break;
if (!IsWindow(parent) || !IsDialogMessageA(parent, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//if
}//while
DeleteObject(hfont);
return 0;
}//main
//------------------------------------------------------------------------------
HFONT CreateFont(HDC hdc, int pitch, const wchar_t *facename)
{
LOGFONTW lf = {0};
wcscpy_s(lf.lfFaceName, sizeof(lf.lfFaceName)/sizeof(*lf.lfFaceName),
facename);
lf.lfHeight = -MulDiv(pitch, GetDeviceCaps(hdc, LOGPIXELSY), 72);
lf.lfWeight = FW_NORMAL;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
lf.lfClipPrecision = CLIP_STROKE_PRECIS;
lf.lfQuality = CLEARTYPE_QUALITY;
return CreateFontIndirectW(&lf);
}//CreateFont
Notes:
- No extended characters in source (save source as Unicode otherwise)
- ComboBox created with CreateWindowW()
- Created a nice font that should have support for those Unicode characters (was getting "best fit" characters without it)
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|