怎么用c语言写窗体程序
答案:1 悬赏:60 手机版
解决时间 2021-03-04 05:20
- 提问者网友:我的未来我做主
- 2021-03-03 07:40
怎么用c语言写窗体程序
最佳答案
- 五星知识达人网友:妄饮晩冬酒
- 2021-03-03 09:15
步骤:
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;
}
就是一个只有标题栏、关闭按钮、最小化按钮、最大化/还原按钮、显示区域的窗体。
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;
}
就是一个只有标题栏、关闭按钮、最小化按钮、最大化/还原按钮、显示区域的窗体。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯