-
Special Characters
Hi,
I was wondering if it were possible to output special characters to a static control or another control. For example, the square root symbol (√), which is character # 251, shows up just fine in the DOS window. But when I try to set that to a static control, it doesn't work (it shows a weird character). Does anyone know how to do this? Thanks. I am using Visual C++ 2008 Express Edition.
-
DOS and Windows uses different character sets.
Why not try
Code:
wchar_t* str = "√";
// Set window text using str here
?
-
You also have to consider what font the control is using. Do Start->Run "charmap.exe". Use that app to inspect the glyphs used by a font and choose one for your control to use. If you register the class or create the window, use RegisterClassW() and CreateWindowW(). When you send it the WM_SETTEXT method, use SendMessageW() and use a wide character string. I checked a few fonts and the Unicode character for square root seems to be 0x221a. Here's how you could use it in a string literal:
Code:
const wchar_t *p = L"The \x221a of 4 is 2";
gg
-
-
Hey thanks everyone, that worked. The only problem is that I would like to combine it with an std::string, and I don't know how to do that. Can anyone help me? Thanks.
-
Code:
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;
wstring my_str = L"The \x221a of 4 is 2";
my_str = L"√";
That should do it, I believe.
-
Uhhh... Elysia that gives me a couple of errors, such as:
Code:
1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(125) : error C2872: 'wstring' : ambiguous symbol
1> could be 'c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\formula helper.h(17) : std::basic_string<_Elem,_Traits,_Ax> wstring'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Ax=std::allocator<wchar_t>
1> ]
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\xstring(2212) : std::wstring'
1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(126) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Ax=std::allocator<wchar_t>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(919): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Ax=std::allocator<wchar_t>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(924): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Ax=std::allocator<wchar_t>
1> ]
1> while trying to match the argument list '(wstring, const char [2])'
1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(129) : error C2440: 'type cast' : cannot convert from 'wstring' to 'LPARAM'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I don't know what I did wrong, I defined "wstring" in my header file, and then tried to use it in my code. Did I do something stupid?
-
Oops, forgot L before the last string.
If it doesn't compile with the fixed code, why not show what line the compile errors occur on?
-
The lines were the errors occur are:
Code:
wstring my_str = L"The \x221a of 4 is 2";
my_str = L"√";
SendMessageW(GetDlgItem(hwnd, IDC_RADDIRECTIONS), WM_SETTEXT, 0, (LPARAM)(my_str));
-
Also, the wstring typedef already exists in the standard library. No need to redefine it. (That's the ambiguity error.)
-
Ok, I removed the place where I defined wstring, but know it won't let me convert from "wstring" to "LPARAM" in the SendMessageW function. Anyway, though, how would using a wstring allow me to use std::strings with them?
-
Do my_str.c_str() to get the buffer and pass it to SendMessageW.
-
Thank you Elysia, that solved the SendMessage problem. However, I still don't know how to use it with an std::string. Here's what I tried:
Code:
wstring my_str;
string str = " test";
my_str = L"√";
strcat((char *)my_str.c_str(), str.c_str());
SendMessageW(GetDlgItem(hwnd, IDC_RADDIRECTIONS), WM_SETTEXT, 0, (LPARAM)my_str.c_str());
However, it gives me an output that looks like this: "√琠獥t쳌쳌쳌쳌". Does anyone know what's wrong, or a better way? I believe it's because in strcat I converted it to a char *, but maybe not.
-
wstring IS string.
string holds char.
wstring holds wchar.
-
Ok, I get it now. However, is there any possible way to use an std::string in conjunction with a wstring? I could always just set the text with the wstring, and then set the std::string on the end of that, but it just erases what ever was there first. Is there a WM_APPENDTEXT message or something like that?