怎样用C语言编写出窗体程序?
2个回答
展开全部
#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;
}
//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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询