步骤:
1、注册窗口类;
2、创建窗体;
3、消息循环;
4、编写窗口消息处理函数。
代码:
#include <windows.h>#include<tchar.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR szCmdLine, int nCmdShow)
{
WNDCLASS wc
wc.style = CS_HREDRAW | CS_VREDRAW
wc.lpfnWndProc = WindowProc
wc.cbClsExtra = 0
wc.cbWndExtra = 0
wc.hInstance = hInstance
wc.hIcon = NULL
wc.hCursor = LoadCursor(NULL,IDC_ARROW)
wc.hbrBackground = (HBRUSH)COLOR_WINDOW//(HBRUSH)GetStockObject()
wc.lpszMenuName = NULL
wc.lpszClassName = _T("MyWindowClass")
if (!RegisterClass(&wc))
{
MessageBox (NULL, _T("无法注册窗口类"), _T("错误"), MB_OK)
return 0
}
HWND newWindow = CreateWindow(
_T("MyWindowClass"),
_T("我的第一个winapi程序"),
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
)
if (NULL == newWindow)
{
MessageBox (NULL, _T("无法创建窗体"), _T("错误"), MB_OK)
return 0
}
ShowWindow(newWindow, nCmdShow)
UpdateWindow(newWindow)
MSG msg
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg)
DispatchMessage(&msg)
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0)
break
}
default :
return DefWindowProc(hwnd, uMsg, wParam, lParam)
}
return 0
}
就是一个只有标题栏、关闭按钮、最小化按钮、最大化/还原按钮、显示区域的窗体。
#include<stdio.h>int main()
{
while(1) //设置一个死循环,使程序重复运行
{
system("cls") //清屏,清除窗口上之前的输出
int a,b
printf("请输入两个加数:")
scanf("%d%d",&a,&b)
printf("两数之和为%d",a+b)
getch() //按任意键从头开始
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)