怎样用C语言编写出窗体程序?

我学C语言已经一年了,只能编出黑屏也就是所谓的控制台程序。怎么样才能写出窗体程序?... 我学C语言已经一年了,只能编出黑屏也就是所谓的控制台程序。怎么样才能写出窗体程序? 展开
 我来答
情系小太阳
推荐于2016-11-18 · TA获得超过148个赞
知道小有建树答主
回答量:121
采纳率:0%
帮助的人:141万
展开全部
#include <windows.h>

//Function prototype.
int WINAPI WinMain(HINSTANCE,HINSTANCE ,LPSTR ,int);
LRESULT CALLBACK MainWndProc(HWND ,UINT ,WPARAM,LPARAM);

InitApplication(HINSTANCE);
InitInstance(HINSTANCE,int);

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch(nMsg)
{
case WM_COMMAND:
{
hdc = GetDC((HWND)lParam);
GetClientRect ((HWND)lParam, &rect) ;
DrawText(hdc,"HOHO",-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC((HWND)lParam,hdc);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
GetClientRect (hwnd, &rect) ;
DrawText(hdc,"Hello Word",-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd,&ps);
}
break;
//按键
case WM_KEYDOWN:

if(wParam == VK_ESCAPE)
SendMessage(hwnd,WM_CLOSE,0,0);

break;
case WM_CHAR:
switch(wParam)
{
case 'w':
MessageBox(hwnd,"UP","KeyBorad Input",MB_OK);
break;
case 's':
MessageBox(hwnd,"DOWN","KeyBorad Input",MB_OK);
break;
case 'a':
MessageBox(hwnd,"LEFT","KeyBorad Input",MB_OK);
break;
case 'd':
MessageBox(hwnd,"RIGHT","KeyBorad Input",MB_OK);
break;
}
break;

//关闭
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd,nMsg,wParam,lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInStance,
LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;

if(!InitApplication(hInstance))
{
MessageBox(NULL,"InitApplication Failed","Error!",MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
if(!InitInstance(hInstance,nCmdShow)){
MessageBox(NULL,"InitInstance Failed","Error!",MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}

while(GetMessage(&msg,(HWND) NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;

wc.style = CS_DBLCLKS;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MainWClass";
return RegisterClass(&wc);
}

InitInstance(HINSTANCE hInstance,int nCmdShow){
HWND hwnd;
HWND hwndCloseButton;
hwnd = CreateWindow(
"MainWClass",
"Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,
(HMENU)NULL,
hInstance,
(LPVOID)NULL);

if(!hwnd)
return FALSE;

hwndCloseButton = CreateWindow(
"BUTTON",
"CLOSE",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100,
100,
100,
80,
hwnd,
(HMENU)NULL,
hInstance,
(LPVOID)NULL);

if(!hwndCloseButton)
return FALSE;

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lgf126
2008-10-29 · TA获得超过1101个赞
知道小有建树答主
回答量:1077
采纳率:33%
帮助的人:459万
展开全部
简单而最正确的回答就是:
请用VC!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式