以下代码贴上就可以跑了
但为什么我输入比如: A A A
时候,每个字符间隙总是出现黑色的方块啊,怎么才能去掉呢
求解啊(请前辈帮我修改一下代码贴上 )
拜了
========================================================
#include<windows.h>
#include<stdio.h>
#include<fstream>
#include<string>
using namespace std;
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//填写窗口类
WNDCLASS wndobj;
wndobj.hInstance=(HINSTANCE)1;
wndobj.lpszClassName="NO_1";//所以这里要使用UNICODE
wndobj.lpszMenuName =NULL;
wndobj.cbClsExtra =NULL;
wndobj.cbWndExtra =NULL;
wndobj.lpfnWndProc=WindowProc;
wndobj.style =CS_HREDRAW|CS_VREDRAW;
wndobj.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndobj.hCursor = LoadCursor(NULL,IDC_CROSS);
wndobj.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);//申请注册窗口类
if (!RegisterClass(&wndobj))//使用UNICODE方式注册
{
MessageBox(NULL,"Register Error!","information",MB_ICONSTOP);
return 0;
}
//实例化窗口类
HWND hwnd;
hwnd=0;
hwnd=CreateWindow(
"NO_1",
"First Window",
WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,//加上滚动条*关注点*
CW_USEDEFAULT,
CW_USEDEFAULT,
400,400,
NULL,NULL,
(HINSTANCE)1,NULL);
if (!hwnd)
{
MessageBox(NULL,"CreateWindow Error!","information",MB_ICONSTOP);
return 0;
}
//显示窗体
ShowWindow(hwnd,SW_SHOWDEFAULT);
UpdateWindow(hwnd);
//处理Windows消息
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}//======================回调函数定义==============================
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static int i=0,i2=0;
static int val;
static HFONT hfont;//字体句柄
static HDC hdc ; //设备句柄
static PAINTSTRUCT ps ; //数据结构
static RECT rect ;//数据结构
static TEXTMETRIC tm; //数据结构
static int MaxCharNumber=0;
static char buff[50]={0};
static POINT point;
switch (uMsg)
{
case WM_CREATE://窗体创建消息
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
GetClientRect(hwnd,&rect);
MaxCharNumber=(rect.right-rect.left)/tm.tmMaxCharWidth ;
ReleaseDC(hwnd,hdc);
point.x =0;point.y=0;
break;
case WM_PAINT://窗体绘画消息
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,100,70,buff,sprintf(buff,"R=%d,L=%d,T=%d,B=%d",rect.left,rect.right,rect.top,rect.bottom));
TextOut(hdc,100,100,buff,sprintf(buff,"MaxCharNumber=%d",MaxCharNumber));
TextOut(hdc,100,130,buff,sprintf(buff,"CWidth=%d,CHeight=%d",tm.tmAveCharWidth,tm.tmHeight));
EndPaint(hwnd,&ps);
break;
case WM_INPUTLANGCHANGE://输入法切换消息
break;
case WM_SETFOCUS://获得焦点消息
CreateCaret(hwnd,NULL,tm.tmMaxCharWidth ,tm.tmHeight);
SetCaretPos(point.x,point.y);
ShowCaret(hwnd);
break;
case WM_KILLFOCUS://失去焦点消息
GetCaretPos(&point);
HideCaret (hwnd) ;
DestroyCaret () ;
break;
case WM_CHAR:
InvalidateRect (hwnd, NULL, false) ;
hdc=BeginPaint(hwnd,&ps);
/// SetBkColor(hdc,RGB(0,0,0));
sprintf(buff,"%c",wParam);
TextOut(hdc,point.x,point.y,buff,1);
point.x =point.x+tm.tmMaxCharWidth ;
SetCaretPos(point.x,point.y);
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN://键盘按键消息
break;
case WM_KEYUP://键盘释放消息
break;
case WM_SYSKEYDOWN://键盘系统按键消息
break;
case WM_SYSKEYUP://键盘系统释放消息
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
===============================================
但为什么我输入比如: A A A
时候,每个字符间隙总是出现黑色的方块啊,怎么才能去掉呢
求解啊(请前辈帮我修改一下代码贴上 )
拜了
========================================================
#include<windows.h>
#include<stdio.h>
#include<fstream>
#include<string>
using namespace std;
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//填写窗口类
WNDCLASS wndobj;
wndobj.hInstance=(HINSTANCE)1;
wndobj.lpszClassName="NO_1";//所以这里要使用UNICODE
wndobj.lpszMenuName =NULL;
wndobj.cbClsExtra =NULL;
wndobj.cbWndExtra =NULL;
wndobj.lpfnWndProc=WindowProc;
wndobj.style =CS_HREDRAW|CS_VREDRAW;
wndobj.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndobj.hCursor = LoadCursor(NULL,IDC_CROSS);
wndobj.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);//申请注册窗口类
if (!RegisterClass(&wndobj))//使用UNICODE方式注册
{
MessageBox(NULL,"Register Error!","information",MB_ICONSTOP);
return 0;
}
//实例化窗口类
HWND hwnd;
hwnd=0;
hwnd=CreateWindow(
"NO_1",
"First Window",
WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,//加上滚动条*关注点*
CW_USEDEFAULT,
CW_USEDEFAULT,
400,400,
NULL,NULL,
(HINSTANCE)1,NULL);
if (!hwnd)
{
MessageBox(NULL,"CreateWindow Error!","information",MB_ICONSTOP);
return 0;
}
//显示窗体
ShowWindow(hwnd,SW_SHOWDEFAULT);
UpdateWindow(hwnd);
//处理Windows消息
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}//======================回调函数定义==============================
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static int i=0,i2=0;
static int val;
static HFONT hfont;//字体句柄
static HDC hdc ; //设备句柄
static PAINTSTRUCT ps ; //数据结构
static RECT rect ;//数据结构
static TEXTMETRIC tm; //数据结构
static int MaxCharNumber=0;
static char buff[50]={0};
static POINT point;
switch (uMsg)
{
case WM_CREATE://窗体创建消息
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
GetClientRect(hwnd,&rect);
MaxCharNumber=(rect.right-rect.left)/tm.tmMaxCharWidth ;
ReleaseDC(hwnd,hdc);
point.x =0;point.y=0;
break;
case WM_PAINT://窗体绘画消息
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,100,70,buff,sprintf(buff,"R=%d,L=%d,T=%d,B=%d",rect.left,rect.right,rect.top,rect.bottom));
TextOut(hdc,100,100,buff,sprintf(buff,"MaxCharNumber=%d",MaxCharNumber));
TextOut(hdc,100,130,buff,sprintf(buff,"CWidth=%d,CHeight=%d",tm.tmAveCharWidth,tm.tmHeight));
EndPaint(hwnd,&ps);
break;
case WM_INPUTLANGCHANGE://输入法切换消息
break;
case WM_SETFOCUS://获得焦点消息
CreateCaret(hwnd,NULL,tm.tmMaxCharWidth ,tm.tmHeight);
SetCaretPos(point.x,point.y);
ShowCaret(hwnd);
break;
case WM_KILLFOCUS://失去焦点消息
GetCaretPos(&point);
HideCaret (hwnd) ;
DestroyCaret () ;
break;
case WM_CHAR:
InvalidateRect (hwnd, NULL, false) ;
hdc=BeginPaint(hwnd,&ps);
/// SetBkColor(hdc,RGB(0,0,0));
sprintf(buff,"%c",wParam);
TextOut(hdc,point.x,point.y,buff,1);
point.x =point.x+tm.tmMaxCharWidth ;
SetCaretPos(point.x,point.y);
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN://键盘按键消息
break;
case WM_KEYUP://键盘释放消息
break;
case WM_SYSKEYDOWN://键盘系统按键消息
break;
case WM_SYSKEYUP://键盘系统释放消息
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
===============================================
InvalidateRect (hwnd, NULL, false) ;
hdc=BeginPaint(hwnd,&ps);
/// SetBkColor(hdc,RGB(0,0,0));
sprintf(buff,"%c",wParam);
TextOut(hdc,point.x,point.y,buff,1);
point.x =point.x+tm.tmMaxCharWidth ;
SetCaretPos(point.x,point.y);
EndPaint(hwnd,&ps);
break;BeginPaint, EndPaint只能用在处理WM_PAINT的时候, 在这里用是不对的
你应该用 GetDC来获得客户区设备环境