C语言如何编写一个窗体,上面显示hello,world。具体解释一下代码,不能用MFC
1个回答
展开全部
#include "stdafx.h"
#include <windows.h>
LRESULT CALLBACK wndproc(HWND ,UINT ,WPARAM ,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
RECT rect,rect1;
WNDCLASS ws;
MSG msg;
HWND hwnd;
ws.style=CS_HREDRAW|CS_VREDRAW;
ws.hInstance=hInstance;
ws.lpfnWndProc=wndproc;
ws.cbClsExtra=0;
ws.cbWndExtra=0;
ws.hCursor=NULL;
ws.hIcon=NULL;
ws.lpszClassName="ch";
ws.lpszMenuName=NULL;
ws.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(255,255,255));
RegisterClass(&ws);
hwnd=CreateWindow("ch","cheng",WS_OVERLAPPEDWINDOW,0,0,500,500,NULL,NULL,hInstance,NULL);
GetClientRect(GetDesktopWindow(),&rect);
GetClientRect(hwnd,&rect1);
MoveWindow(hwnd,(rect.right-rect1.right)/2,(rect.bottom-rect1.bottom)/2,500,500,0);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int i=0,x=0,y=10;
HDC hdc;
HBRUSH bs;
PAINTSTRUCT ps;
RECT rect;
HFONT hfont;
HDC zd;
LOGFONT lft;
HPEN hpen;
switch(msg)
{
case WM_CREATE:
SetTimer(hwnd,1,100,NULL);
return 0;
case WM_TIMER:
//////实现进程的动画
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rect);
if(i<rect.right-1)
bs=CreateSolidBrush(RGB(0,0,255));
else
bs=CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,bs);
i=i+1;
Rectangle(hdc,0,40,i,20);
if(i==rect.right)
{
KillTimer(hwnd,1);
}
ReleaseDC(hwnd,hdc);
DeleteObject(bs);
memset(&lft,0,sizeof(lft));
lft.lfHeight=30;
lft.lfWidth=10;
x=x+20;
lft.lfEscapement=x;
lstrcpy(lft.lfFaceName,"hello word!");
hfont=CreateFontIndirect(&lft);
zd=GetDC(hwnd);
hpen=CreatePen(PS_DASH,1,RGB(255,0,0));
SelectObject(zd,hpen);
SelectObject(zd,hfont);
TextOut(zd,200,270,"hello word!",11);
ReleaseDC(hwnd,zd);
DeleteObject(hpen);
DeleteObject(hfont);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//////画外围边框
bs=CreateSolidBrush(RGB(0,200,0));
SelectObject(hdc,bs);
GetClientRect(hwnd,&rect);
hpen=CreatePen(PS_DASH,5,RGB(25,0,255));
SelectObject(hdc,hpen);
Rectangle(hdc,0,40,rect.right,20);
DeleteObject(bs);
DeleteObject(hpen);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
以前写的了
#include <windows.h>
LRESULT CALLBACK wndproc(HWND ,UINT ,WPARAM ,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
RECT rect,rect1;
WNDCLASS ws;
MSG msg;
HWND hwnd;
ws.style=CS_HREDRAW|CS_VREDRAW;
ws.hInstance=hInstance;
ws.lpfnWndProc=wndproc;
ws.cbClsExtra=0;
ws.cbWndExtra=0;
ws.hCursor=NULL;
ws.hIcon=NULL;
ws.lpszClassName="ch";
ws.lpszMenuName=NULL;
ws.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(255,255,255));
RegisterClass(&ws);
hwnd=CreateWindow("ch","cheng",WS_OVERLAPPEDWINDOW,0,0,500,500,NULL,NULL,hInstance,NULL);
GetClientRect(GetDesktopWindow(),&rect);
GetClientRect(hwnd,&rect1);
MoveWindow(hwnd,(rect.right-rect1.right)/2,(rect.bottom-rect1.bottom)/2,500,500,0);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int i=0,x=0,y=10;
HDC hdc;
HBRUSH bs;
PAINTSTRUCT ps;
RECT rect;
HFONT hfont;
HDC zd;
LOGFONT lft;
HPEN hpen;
switch(msg)
{
case WM_CREATE:
SetTimer(hwnd,1,100,NULL);
return 0;
case WM_TIMER:
//////实现进程的动画
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rect);
if(i<rect.right-1)
bs=CreateSolidBrush(RGB(0,0,255));
else
bs=CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,bs);
i=i+1;
Rectangle(hdc,0,40,i,20);
if(i==rect.right)
{
KillTimer(hwnd,1);
}
ReleaseDC(hwnd,hdc);
DeleteObject(bs);
memset(&lft,0,sizeof(lft));
lft.lfHeight=30;
lft.lfWidth=10;
x=x+20;
lft.lfEscapement=x;
lstrcpy(lft.lfFaceName,"hello word!");
hfont=CreateFontIndirect(&lft);
zd=GetDC(hwnd);
hpen=CreatePen(PS_DASH,1,RGB(255,0,0));
SelectObject(zd,hpen);
SelectObject(zd,hfont);
TextOut(zd,200,270,"hello word!",11);
ReleaseDC(hwnd,zd);
DeleteObject(hpen);
DeleteObject(hfont);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//////画外围边框
bs=CreateSolidBrush(RGB(0,200,0));
SelectObject(hdc,bs);
GetClientRect(hwnd,&rect);
hpen=CreatePen(PS_DASH,5,RGB(25,0,255));
SelectObject(hdc,hpen);
Rectangle(hdc,0,40,rect.right,20);
DeleteObject(bs);
DeleteObject(hpen);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
以前写的了
更多追问追答
追问
你包含MFC的头文件了
追答
去掉也可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询