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

我知道VC可以用画图式创建窗体,可怎么有人说写代码比画图式操作更方便?到底是怎么回事????用代码写窗体是不是很麻烦?还有用代码写窗体的好处?为什么程序员都喜欢用代码写窗... 我知道VC可以用画图式创建窗体,可怎么有人说写代码比画图式操作更方便?
到底是怎么回事????
用代码写窗体是不是很麻烦?
还有用代码写窗体的好处?
为什么程序员都喜欢用代码写窗体而不用画图式操作?那样不是更快吗?
展开
 我来答
xx8833
推荐于2018-02-27 · TA获得超过1629个赞
知道小有建树答主
回答量:342
采纳率:0%
帮助的人:477万
展开全部
所有其他方式创建窗体,归根到底都是使用类似下面的代码,只是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 <windows.h>

// 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);
}
access开发易登软件
2020-12-18 · 超过18用户采纳过TA的回答
知道答主
回答量:79
采纳率:100%
帮助的人:6.7万
展开全部

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

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
督诺0GMffd
2010-07-26 · TA获得超过2013个赞
知道大有可为答主
回答量:3770
采纳率:0%
帮助的人:2045万
展开全部
创建窗体只有两个步骤。比如说你要创建一个按钮窗体。
那么可以这样做:
1.声明一个按钮类对象CButon m_Button.
2创建。m_Button.Create(.....)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
popapple7
2010-07-26 · TA获得超过172个赞
知道小有建树答主
回答量:232
采纳率:0%
帮助的人:180万
展开全部
不麻烦,自己写个模板,以后每次照着套用就行了.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dong_1984dd
推荐于2016-01-09 · TA获得超过1.1万个赞
知道小有建树答主
回答量:1070
采纳率:100%
帮助的人:223万
展开全部
1.C++ 代码创建窗体,代码如下:
#include <windows.h>
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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式