在winform中,声明
//获取窗口标题
[DllImport(“user32”, SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd,//窗口句柄
StringBuilder lpString,//标题
int nMaxCount //最大值
);
已知句柄的情况下:
StringBuilder title = new StringBuilder(2048);
GetWindowText(hd, title,title.Length);//得到窗口的标题
假设此时窗口标题为넘버나인 ,那么title读出的则为乱码(由几个问号组成),求指导
//获取窗口标题
[DllImport(“user32”, SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd,//窗口句柄
StringBuilder lpString,//标题
int nMaxCount //最大值
);
已知句柄的情况下:
StringBuilder title = new StringBuilder(2048);
GetWindowText(hd, title,title.Length);//得到窗口的标题
假设此时窗口标题为넘버나인 ,那么title读出的则为乱码(由几个问号组成),求指导
解决方案
1
是user32.dll的问题吧,应为他是在你电脑上的,所以肯定是中文格式的,假如调用韩文的,就不支持了
2
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
仅供参考:
仅供参考:
#pragma comment(lib,"user32") #pragma comment(lib,"gdi32") #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> HWND WINAPI GetConsoleWindow(); void HideTheCursor() { CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = FALSE; SetConsoleCursorInfo(hStdOut, &cciCursor); } } void ShowTheCursor() { CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = TRUE; SetConsoleCursorInfo(hStdOut, &cciCursor); } } int main() { HWND hwnd; HDC hdc; HFONT hfont; system("color F0"); system("cls"); HideTheCursor(); hwnd = GetConsoleWindow(); hdc = GetDC(hwnd); hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, "Arial Unicode MS"); SelectObject(hdc,hfont); TextOutW(hdc,10,10,L"你好",2); DeleteObject(hfont); hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, JOHAB_CHARSET , 0, 0, 0, 0, "Arial Unicode MS"); SelectObject(hdc,hfont); TextOutW(hdc,10,80,L"\xb7f0\xb7f0\xb2dd\xb2dd",4); DeleteObject(hfont); ReleaseDC(hwnd,hdc); getch(); system("color 07"); system("cls"); ShowTheCursor(); return 0; }
2
全部乱码问题 十有八九都是 编码问题
把stringbuilder 换成 byte[] 试试
然后 Encoding.UTF8(各种编码试试).GetString(byte[]).Trim(“\0”);
把stringbuilder 换成 byte[] 试试
然后 Encoding.UTF8(各种编码试试).GetString(byte[]).Trim(“\0”);
4
设置一下DllImport特性的CharSet属性试试
1
本人觉得应该是你字符编码问题,把字符编码转换一下试试
10
将其替换为:
[DllImport("user32.dll", CharSet =EUC_KR , SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);