C++怎么用代码创建窗体?

2024-12-18 15:45:33
推荐回答(5个)
回答1:

所有其他方式创建窗体,归根到底都是使用类似下面的代码,只是VC++帮你做了,你看不到而已。直接使用代码写对窗体可控性更强,更灵活。下面是代码
新建一个win32空项目(不是win32控制台),可以编译通过
还有问题按“在线交谈”
//=====================================================================
// Win32Basic.cpp by Frank Luna (C) 2008 All Rights Reserved.
//
//=====================================================================

// Include the Windows header file; this has all the Win32 API
// structures, types, and function declarations we need to program
// Windows.
#include

// The main window handle; this is used to identify a
// created window.
HWND ghMainWnd = 0;

// Wraps the code necessary to initialize a Windows
// application. Function returns true if initialization
// was successful; otherwise, it returns false.
bool InitWindowsApp(HINSTANCE instanceHandle, int show);

// Wraps the message loop code.
int Run();

// The window procedure handles events our window receives.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

// Windows equivalant to main()
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR pCmdLine, int nShowCmd)
{
// First call our wrapper function (InitWindowsApp) to create
// and initialize the main application window, passing in the
// hInstance and nShowCmd values as arguments.
if(!InitWindowsApp(hInstance, nShowCmd))
return 0;

// Once our application has been created and initialized, we
// enter the message loop. We stay in the message loop until
// a WM_QUIT message is received, indicating the application
// should be terminated.
return Run();
}

bool InitWindowsApp(HINSTANCE instanceHandle, int show)
{
// The first task to creating a window is to describe some of its
// characteristics by filling out a WNDCLASS structure.
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instanceHandle;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"BasicWndClass";

// Next, we register this WNDCLASS instance with Windows so
// that we can create a window based on it.
if(!RegisterClass(&wc))
{
MessageBox(0, L"RegisterClass FAILED", 0, 0);
return false;
}

// With our WNDCLASS instance registered, we can create a
// window with the CreateWindow function. This function
// returns a handle to the window it creates (an HWND).
// If the creation failed, the handle will have the value
// of zero. A window handle is a way to refer to the window,
// which is internally managed by Windows. Many of the Win32 API
// functions that operate on windows require an HWND so that
// they know what window to act on.

ghMainWnd = CreateWindow(
L"BasicWndClass", // Registered WNDCLASS instance to use.
L"Win32Basic", // window title
WS_OVERLAPPEDWINDOW, // style flags
CW_USEDEFAULT, // x-coordinate
CW_USEDEFAULT, // y-coordinate
CW_USEDEFAULT, // width
CW_USEDEFAULT, // height
0, // parent window
0, // menu handle
instanceHandle, // app instance
0); // extra creation parameters

if(ghMainWnd == 0)
{
MessageBox(0, L"CreateWindow FAILED", 0, 0);
return false;
}

// Even though we just created a window, it is not initially
// shown. Therefore, the final step is to show and update the
// window we just created, which can be done with the following
// two function calls. Observe that we pass the handle to the
// window we want to show and update so that these functions know
// which window to show and update.
ShowWindow(ghMainWnd, show);
UpdateWindow(ghMainWnd);

return true;
}

int Run()
{
MSG msg = {0};

// Loop until we get a WM_QUIT message. The function
// GetMessage will only return 0 (false) when a WM_QUIT message
// is received, which effectively exits the loop. The function
// returns -1 if there is an error. Also, note that GetMessage
// puts the application thread to sleep until there is a
// message.
BOOL bRet = 1;
while( (bRet = GetMessage(&msg, 0, 0, 0)) != 0 )
{
if(bRet == -1)
{
MessageBox(0, L"GetMessage FAILED", L"Error", MB_OK);
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int)msg.wParam;
}

LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Handle some specific messages. Note that if we handle a
// message, we should return 0.
switch( msg )
{
// In the case the left mouse button was pressed,
// then display a message box.
case WM_LBUTTONDOWN:
MessageBox(0, L"Hello, World", L"Hello", MB_OK);
return 0;

// In the case the Escape key was pressed, then
// destroy the main application window.
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
DestroyWindow(ghMainWnd);
return 0;

// In the case of a destroy message, then send a
// quit message, which will terminate the message loop.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

// Forward any other messages we did not handle above to the
// default window procedure. Note that our window procedure
// must return the return value of DefWindowProc.
return DefWindowProc(hWnd, msg, wParam, lParam);
}

回答2:

Access 中的窗体是一种数据库对象,可用于创建数据库应用程序的用户界面。“绑定”窗体直接连接到表或查询之类的数据源,可用于输入、编辑或显示来自该数据源的数据。或者也可以创建“未绑定”窗体。

回答3:

创建窗体只有两个步骤。比如说你要创建一个按钮窗体。
那么可以这样做:
1.声明一个按钮类对象CButon m_Button.
2创建。m_Button.Create(.....)

回答4:

不麻烦,自己写个模板,以后每次照着套用就行了.

回答5:

1.C++ 代码创建窗体,代码如下:
#include
HWND HMainwnd = NULL; //窗体句柄
int run(); //消息循环函数
bool InitWindowsApp(HINSTANCE instanceHandle, int show); //窗体初始化函数
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);//窗体处理回调函数
//main函数
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
if(!InitWindowsApp(hInstance, nShowCmd))//判断窗体初始化是否成功
{
return 0;
}
run(); //初始化成功后循环接收消息
return 0;
}
bool InitWindowsApp(HINSTANCE stanceHandle, int show)
{
WNDCLASS wc; //窗体类结构体
wc.style = CS_HREDRAW | CS_VREDRAW; //窗体风格
wc.lpfnWndProc = WndProc; //窗体处理函数
wc.cbClsExtra = 0; //窗体类是否由扩展
wc.cbWndExtra = 0; //窗体实例是否由扩展
wc.hInstance = stanceHandle; //窗体句柄
wc.hIcon = LoadIcon(0, IDI_APPLICATION); //窗体图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //窗体鼠标样式
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗体背景颜色
wc.lpszMenuName = NULL; //窗体菜单
wc.lpszClassName = "BaseWndClass"; //窗体类名
if(!RegisterClass(&wc)) //判断窗体是否注册成功
{
MessageBox(0, "注册失败", 0, 0);
return false;
}
//创建窗体
HMainwnd = CreateWindow("BaseWndClass", //窗体类名
"MyFrom", //窗体标题名
WS_OVERLAPPEDWINDOW, //窗体风格
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
NULL, //窗体的父窗体
NULL, //窗体的子菜单
stanceHandle, //窗体句柄
NULL); // 如果函数成功,返回值为新窗口的句柄:如果函数失败,返回值为NULL
if(HMainwnd == 0)//判断创建窗体是否成功
{
MessageBox(0, "创建窗体失败", 0, 0);
return false;
}
ShowWindow(HMainwnd, SW_SHOW); //显示窗体
UpdateWindow(HMainwnd); //绘制窗体;
return true;
}
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)//判断消息的种类
{
case WM_LBUTTONDOWN: //左键单击
MessageBox(0, "Hello World", "myMbox", MB_OK);
return 0;
case WM_KEYDOWN: //键盘消息
if(wp == VK_ESCAPE) //ESC键
DestroyWindow(HMainwnd); //销毁窗体
return 0;
case WM_DESTROY:
PostQuitMessage(0); //终止消息
return 0;
}
return DefWindowProc(wnd, msg, wp, lp); //返回缺省消息函数
}
int run()
{
MSG msg = {0}; //消息结构
BOOL bRet = 1; //获取消息
while( (bRet = GetMessage(&msg, 0, 0, 0)) != 0)//消息循环
{
if(bRet == -1)//判断消息是否接收失败
{
MessageBox(0, "接受消息失败", 0, 0);
break;
}
else
{
TranslateMessage(&msg); //转换消息为字符消息
DispatchMessage(&msg); //发送消息给窗口
}
}
return (int)msg.wParam;
}