关于Visual Studio2010字符集的问题

来源:互联网 发布:越南女孩 知乎 编辑:程序博客网 时间:2024/05/16 08:25

        今天在用Visual Studio写C时,出现了不能正常显示的问题,然后做了个小实验,代码如下:

        实验结果:static WCHAR f=L'你';   这里f采集到的是一个2字节的数据20320('你'的Unicode码),而如果用 static WCHAR f='你';  采集到的数据是50403(你的GBK码),而显示时如果用的L则正常显示“你”,不用L则不能正常显示。用TEXT()采集的数据也能正常显示。

        个人分析了下原因:本人用的是搜狗输入法,而搜狗输入法采用的是GBK输入,因此'你'是用一个值为50403的双字节数据表示的,而Visual Studio默认采用的是Unicode字符集,Unicode中的50403不是表示'你'的,因此不能够正常显示。而Visual Studio2010提供了字符集间的转换方法:TEXT()将多字符集字符串转换为Unicode字符集的字符串,L将多字符集字符转换为Unicode字符集字符。还可以在工程的设置里面更改默认字符集。

#include<windows.h>
//LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 RECT rect;
 static int a=0;
 static WCHAR e[10];
 static WCHAR d[]={TEXT("你!")};//use different ways to gather static ?
 static WCHAR f=L'你'; // in GBK it is 50403 ,and in Unicode it is 20320
 static WCHAR g='!';
 static WCHAR h[]=L"你!";
 //char *in=(char*)(&d[0]);
 switch(message)
 {
 case WM_CREATE:
  return 0;
 case WM_PAINT:
  hdc=BeginPaint(hwnd,&ps);
  GetClientRect(hwnd,&rect);
  int c;
  if(a==0)
   DrawText(hdc,TEXT("Liu Wangsheng !"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  else
   c=DrawText(hdc,e,-1,&rect,DT_SINGLELINE);
  EndPaint(hwnd,&ps);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 case WM_KEYDOWN:
  switch(wParam)
  {
   case VK_UP:
    a++;
    //SendMessage(hwnd,WM_PAINT,0,0);
    for(int i=0;i<a*2;i++)
    {
     e[i*2]=g;
     e[i*2+1]=d[1];

    }
    e[a*2]='\0';
    InvalidateRect(hwnd,NULL,TRUE);
    return 0;
   case VK_DOWN:
    PostQuitMessage(0);
    return 0;
  }
  return 0;
 }
 return DefWindowProc(hwnd,message,wParam,lParam);
 //return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR  lpCmdLine,int nCmdShow)
{
  // TODO: Place code here.
 static TCHAR szAppName[]=TEXT("LWS");
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;

 wndclass.style=CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 wndclass.lpszMenuName=NULL;
 wndclass.lpszClassName=szAppName;
 RegisterClass(&wndclass);
 hwnd=CreateWindow(szAppName,
  TEXT("hello!"),
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL);
 ShowWindow(hwnd,nCmdShow);
 UpdateWindow(hwnd);
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;
}

 

 

原创粉丝点击