如何在编辑控件(本机Win32应用程序)中显示特殊字符 [英] How to show special characters in an Edit Control (native Win32 app)

查看:60
本文介绍了如何在编辑控件(本机Win32应用程序)中显示特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在C ++的本机Win32应用程序构建中的编辑控件中显示特殊字符.

我用:
-编辑控件,
-Courier New作为此控件上的字体
-带有WM_SETTEXT消息的功能SendMessage.
-Win XP SP3和VS2010.

我想显示一个二进制文件,问题是每个字符<仅显示一个黑色正方形. 32,但是当我在编辑控件上键入ALT-16时(例如),它显示►(这就是我想要的),因此字体正确,因为它显示了<字符. 32当您键入它们时,但SendMessage不能让我正确显示这些字符.

如果您能帮助我,我感激不尽.
ThanX.

I want to know how to show special characters in an Edit Control in a native Win32 aplication build in C++.

I use :
- an Edit Control,
- Courier New as font over this control
- The function SendMessage with the WM_SETTEXT message.
- Win XP SP3 and VS 2010.

I want to display a binary file, the problem is it only shows a black square for every character < 32, but when I type ALT-16 on the Edit Control (for example) it shows the ► (this is what I want), so the font is right because it displays the characters < 32 when you type them, but the SendMessage doesnt let me display correctly these characteres.

If you could help I appreciate it.
ThanX.

推荐答案

使用alt代码的原因是它们会生成非ascii字符.使用SetWindowText或WM_SETTEXT直接处理ascii字符.

此外,alt-248不对应于ASCII字符248(alt-248 = ASCII 176 =度符号-°)

您可以看到此处显示的代码: http://www.alt-codes.net/ [ http://www.ascii-code.com/ [
The thing about using the alt-codes is that they generate non-ascii characters. Using SetWindowText or WM_SETTEXT directly deal with ascii characters.

Furthermore, alt-248 does not correspond to ascii char 248 (alt-248 = ascii 176 = degrees symbol - °)

You can see that the codes presented here: http://www.alt-codes.net/[^]
are different to those shown here: http://www.ascii-code.com/[^]

If I use the following character string:
char mStr[] = {'a','b', 149, '°', 176, 230, 0};


然后我得到这个显示(这是预期的输出)


Then I get this shown (which is the expected output)

ab•°°æ


只需尝试一下,下面的代码对我来说可以任意组合使用.请注意,您可能需要将编辑控件的字体设置为某些等宽字体.
Just tried and the code below works fine for me with any combination. Note that you might want to set the font for your edit control to some monospaced type.
#include "stdafx.h"

#include <windows.h>
HINSTANCE g_hInstance = (HINSTANCE)GetModuleHandle(NULL);
HWND g_hMainWnd = NULL;
bool g_MovingMainWnd = false;
POINT g_OrigCursorPos;
POINT g_OrigWndPos;
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HFONT font = CreateFontW(
		25, 0, 0, 0,
		FW_DONTCARE,
		FALSE,
		FALSE,
		FALSE,
		DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH,
		L"Terminal");

	switch (uMsg)
	{
	case WM_CREATE:
		if (HWND hEdit = ::CreateWindowExW(0, L"Edit", L"", WS_CHILD|WS_VISIBLE|WS_BORDER, 0, 0, 150, 50, hWnd, (HMENU)1, g_hInstance, NULL))
		{
			SendMessageW(hEdit, WM_SETFONT, (WPARAM)font, 0);
			SendMessageW(hEdit, WM_SETTEXT, 0, (LPARAM)L"\x07\x08\x10\x11_XYZ");
		}
		break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			if (HDC dc = BeginPaint(hWnd, &ps))
			{
				printf("%p\n", font);
				HGDIOBJ orig_font = SelectObject(dc, font);
				RECT rt;
				GetClientRect(hWnd, &rt);
				rt.top += 60;
				static const wchar_t TXT[] = L"\x04\x0b\x11_XYZ";
				DrawTextW(dc, TXT, wcslen(TXT), &rt, DT_LEFT|DT_TOP|DT_NOPREFIX|DT_INTERNAL);
				SelectObject(dc, orig_font);
				EndPaint(hWnd, &ps);
			}
		}
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
bool CreateMainWnd()
{
	static const wchar_t CLASS_NAME[] = L"MainWndClass";
	WNDCLASSW wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance = g_hInstance;
	wc.lpfnWndProc = &MainWndProc;
	wc.lpszClassName = CLASS_NAME;
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	if (!RegisterClassW(&wc))
		return false;
	g_hMainWnd = CreateWindowExW(
		0,
		CLASS_NAME,
		L"Main Window",
		WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
		NULL,
		NULL,
		g_hInstance,
		NULL
		);
	return true;
}
int main()
{
	if (!CreateMainWnd())
		return -1;
	ShowWindow(g_hMainWnd, SW_SHOW);
	UpdateWindow(g_hMainWnd);
	MSG msg;
	while (GetMessageW(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessageW(&msg);
	}
	return (int)msg.wParam;
}


修改了源代码.原始代码是在Win7上编写的.我在家里的WinXP SP2上对其进行了测试,但对我不起作用.我的看法是,它与字体有关(它们不包含32位以下的字符),因为在我的系统上,唯一起作用的字体是Terminal,但是该字体实际上只有糟糕的低分辨率字符.我建议您获取包含每个字符的良好字体,例如将其作为资源嵌入到您的程序中,然后像下面这样为您的程序加载它:
http://msdn.microsoft.com/en-us/library/cc195060.aspx [ ^ ].也许在较新的操作系统上,他们修补了一些字体...
您应该在互联网上搜索免费的IMB 437 DOS代码页或其他字体.在将字体嵌入到您的应用程序之前,请不要忘记签出该字体的许可证!
我还没有检查为什么旧代码在Win7上能正常工作,但是它可能有以下原因:
1.那里的默认字体包含控制字符.
2. Win7上的DrawText()可能使用了字符替换,该字符替换使用另一种字体绘制了缺少的控制字符

我检查了是否可以在文字处理器中复制粘贴有许多字体的特殊字符,但这可能是字体替换的结果.我建议您使用字体编辑器程序检查二进制字体文件.一个免费的多平台字体编辑器程序是fontforge,它运行良好,但是其跨平台gui有点糟糕.目前我没有时间.


Modified the source code. The original code was written on Win7. I tested it on my WinXP SP2 at home and it didn''t work for me. My opinion is that it has something to do with the fonts (they don''t contain characters below 32) because on my system the only font that worked is the Terminal, but that font has only really crappy low resolution characters. What I recommend is to get a good font that contains every characters, embed it to your program for example as a resource and then load it for your program like this: How to Use a Font Without Installing it[^].
I checked some MS codepages on the internet and they don''t contain glyphs for control characters: http://msdn.microsoft.com/en-us/library/cc195060.aspx[^]. Maybe on newer operating systems they patched some of their fonts...
You should search for free IMB 437 DOS codepage or whaterver fonts on the internet. Don''t forget to check out the license of the font before embedding it to your app!!!

I havent check why did the old code work on Win7 but it can have the following reasons:
1. The default font there contained the control characters.
2. DrawText() on Win7 might used character substitution that drew the missing control characters using another font

I checked that in a Word processor I can copy paste in special characters with many fonts, but that might be the effect of font substitution. What I recommend is checking out your binary font files with a font editor program. A free multiplatform font editor program is fontforge, it works quite well but its crossplatform gui is a bit awful. Currently I don''t have time for this.


这篇关于如何在编辑控件(本机Win32应用程序)中显示特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆