读书人

这到底是咋回事啊跪求

发布时间: 2013-11-25 13:22:27 作者: rapoo

这到底是怎么回事啊,跪求
菜鸟入们写了一个,在vs2010中写了个简单的打字程序,但是窗口标题栏出现乱码啊,还有不能输入中文。。。但用vc6可以,这是哪里问题啊,vs的char类型用不了,怎样处理为unicode字符啊。。。求前辈们耐心指点指点。。。
#include <iostream>
#include <windows.h>
#include <string>
#include "resource.h"
using namespace std;

LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//窗口类
CHAR szClassName[]="MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=MainWndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=::LoadIcon(NULL,IDI_APPLICATION);
wndclass.lpszClassName=(LPCWSTR)szClassName;
wndclass.lpszMenuName=(LPCWSTR)IDR_Typer;
wndclass.hIconSm=NULL;

//注册窗口类
::RegisterClassEx(&wndclass);

//创建窗口
HWND hwnd=::CreateWindowEx(0,
(LPCWSTR)szClassName,
(LPCWSTR)"mywindow!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(hwnd==NULL){
::MessageBoxA(NULL,"创建窗口失败!!","error",MB_OK);
return -1;

}

//显示窗口
::ShowWindow(hwnd,nCmdShow);

//刷新窗口区域
::UpdateWindow(hwnd);

//消息循环
MSG msg;
while(::GetMessage(&msg,NULL,0,0)){ //取消息

//转化键盘消息
::TranslateMessage(&msg);

//将消息发给窗口函数
::DispatchMessage(&msg);

}

return msg.wParam;//当GetMessage函数返回false时循环结束

}//WinMain结束(窗口创建结束)

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){//窗口过程函数

char szText[]="FirstFrame";
static string str,str_c;//必须设置为静态变量(否则str只是暂时变量。。)
static int str_n=0;
static bool iscount=false;

//消息处理***

switch(message){

case WM_CREATE:{

::SetWindowText(hwnd,(LPCWSTR)"typer");

return 0;

}

case WM_PAINT:{//需要重画窗口客户区
HDC hdc;
PAINTSTRUCT ps;
hdc=::BeginPaint(hwnd,&ps);

//这里可以加入自由内容
::TextOutA(hdc,0,0,str.c_str(),str.length());
if(iscount){
char szP[50];
wsprintfA(szP,"当前字符数为:%d",str_n);
str_c=szP;
::TextOutA(hdc,200,200,str_c.c_str(),str_c.length());
}

::EndPaint(hwnd,&ps);
return 0;
}
//add the extra message item


//add the extra message item above
case WM_COMMAND:{
switch(LOWORD(wParam)){
case ID_FILE_EXIT:{
::SendMessage(hwnd,WM_CLOSE,0,0);
break;
}
case ID_COUNT:{
iscount=true;
//char szP[50];
//wsprintfA(szP,"当前字符数为:%d",str_n);
//str_c=szP;
::InvalidateRect(hwnd,NULL,0);
break;
}
case ID_CAN_COUNT:{
iscount=false;
::InvalidateRect(hwnd,NULL,1);
break;
}

}
return 0;
}
case WM_CHAR:{
if(wParam==0x08){
if(str.length()!=0){
int n=str.length()-1;
str.erase(n,1);
str_n=str.length();
}
}
else{
str=str+(char)wParam;//**保存输入的字符串
str_n=str.length();
}
//RECT rc={0,0,20,20};

::InvalidateRect(hwnd,NULL,1);//使整个窗口变得无效

return 0;


}

case WM_DESTROY:{
::PostQuitMessage(0);
return 0;
}

//不处理的消息交由默认消息处理函数处理
default: return::DefWindowProc(hwnd,message,wParam,lParam);

}//switch

}

[解决办法]
TCHAR szClassName[] = TEXT("MainWClass");

//创建窗口
HWND hwnd=::CreateWindowEx(0,
szClassName,
TEXT("mywindow!"),

SetWindowText(hwnd,TEXT("typer"));


这种问题很多人问过了


[解决办法]
把项目修改成多字节的


或使用向导生成一个看看 就明白了



[解决办法]
提供一个方法:
多用google直接把编译的错误google下

读书人网 >VC/MFC

热点推荐