输入英文字母
作者:互联网
题目:
在窗口中输入26个英文字母,从左到右依次位置提高10个像素单位并且颜色变为红色,然后回到正常位置。到达最右端后改变方向,依次从右向左调整位置。
第二行英文字母从正常到斜体,颜色从黑色到天蓝色。
(1)定义一个全局变量nchar来标志跳起的26个字母的位置,因为红色的字母移动的方向是左右可以循环的定义全局变量bRight和bLeft,以标志当前移动的方向
从左到右:bRight=TRUE,bLeft=FALSE;
(2)调用WM_ CREATE调用SetTimer()函数创建定时器,每0.1秒调用WM_TIMER消息,然后定时器消息处理过程调用InvalidateRect(hwnc,NULL,1)发出刷新用户区的请求,发出WM_PAINT消息,实现用户区的动态显示。
(3)处理第一行然后处理第二行
#include<windows.h>
int nchar=0;
HFONT CreateFont(HDC hdc,int nCharHight,int nCmdshow);
BOOL bRight=TRUE,bLeft=FALSE,bItalic=FALSE;
//窗口处理函数WndProc()的声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//主函数,程序的入口,相当于之前的main()函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "HelloWin";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;//WNDCLASSEX也行,会多几个分量,配合RegisterClassEX使用
//设置WNDCLASS结构体,为下一步调用RegisterClass()函数做准备
wndclass.style = CS_HREDRAW | CS_VREDRAW;//支持重绘,不能为0
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;
//注册
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, "This program requires Windows NT!",
szAppName, MB_ICONERROR);
return 0;
}
//调用CreateWindow()函数创建窗体
hwnd = CreateWindow (szAppName, // window class name
"The Windows Program", // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
//显示和更新窗口
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
//消息循环,不停接收消息直至收到WM_QUIT消息后,跳出循环,程序结束
while (GetMessage(&msg, NULL, 0, 0))//如果函数取得WM_QUIT之外的其他消息,返回非零值。如果函数取得WM_QUIT消息,返回值是零
{
TranslateMessage(&msg);//翻译消息,将按键消息转换为字符消息
DispatchMessage(&msg);//以此消息为参数转发给相应的窗口消息处理程序
}
return msg.wParam;
}
//窗口处理函数,函数格式固定,函数名无所谓
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Code here!
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
HFONT hF;
TEXTMETRIC tm;
char m[]="abcdefghijklmnopqrstuvwxyz";
int ncharlen=strlen(m);
int x=0,y=0;
int i;
//分支结构,处理各种消息
switch (message)
{
//绘图,很多情况下会被调用
case WM_CREATE:
{
SetTimer(hwnd,1,300,NULL);
break;
}
case WM_TIMER:
{
InvalidateRect(hwnd,NULL,1);
break;
}
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
hF=CreateFont(hdc,40,0);
SelectObject(hdc,hF);
GetTextMetrics(hdc,&tm);
y=tm.tmExternalLeading+10;
for(i=0;i<nchar;i++)
{
SetTextColor(hdc,RGB(0,0,0));
x=x+tm.tmAveCharWidth*2;
TextOut(hdc,x,y,&m[i],1);
}
SetTextColor(hdc,RGB(255,0,0));//红
x=x+tm.tmAveCharWidth*2;
y=tm.tmExternalLeading;
TextOut(hdc,x,y,&m[nchar],1);
y=tm.tmExternalLeading+10;
for(i=nchar+1;i<ncharlen;i++)
{
SetTextColor(hdc,RGB(0,0,0));
x=x+tm.tmAveCharWidth*2;
TextOut(hdc,x,y,&m[i],1);
}
if(nchar==ncharlen)
{
bRight=FALSE;
bLeft=TRUE;
}
else if(nchar==0)
{
bRight=TRUE;
bLeft=FALSE;
}
if(bRight==TRUE)
nchar++;
else nchar--;
DeleteObject(hF);
hF=CreateFont(hdc,40,bItalic);
SelectObject(hdc,hF);
x=tm.tmAveCharWidth*2;
y=tm.tmHeight*2;
if(bItalic==TRUE)
SetTextColor(hdc,RGB(0,0,0));
else
SetTextColor(hdc,RGB(0,255,255));
TextOut(hdc,x,y,m,strlen(m));
if(bItalic==TRUE)
bItalic=FALSE;
else
bItalic=TRUE;
DeleteObject(hF);
EndPaint (hwnd, &ps) ;
return 0 ;
//用户点击关闭窗体按钮时触发
case WM_DESTROY:
PostQuitMessage (0) ;//发出WM_QUIT消息,终止消息循环
KillTimer(hwnd,1);
return 0 ;
}
//缺省采用系统消息默认处理函数
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
HFONT CreateFont(HDC hdc,int nCharHeight,BOOL bItalic)
{
HFONT hFont;
hFont=CreateFont(
nCharHeight,
0,
0,
0,
400,
bItalic,
0,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
"煞笔");
if(hFont==NULL)
return NULL;
else
return hFont;
}
运行截屏:
标签:英文字母,hwnd,WM,int,wndclass,hdc,NULL,输入 来源: https://blog.csdn.net/m0_49317668/article/details/123588910