Visual Studio 中的 MFC 设计器是否像 .NET 中的 Windows 窗体一样?

2022-01-12 00:00:00 visual-studio-2008 mfc

我是一名 .NET 程序员.我从来没有在 MFC 中开发过任何东西.目前我不得不为一些图像处理任务编写一个 C++ 应用程序(控制台).我写完了.但关键是我还需要为此设计 GUI.嗯,不会有什么复杂的.只是一个带有几个按钮、单选按钮、复选框、图片框和几个滑块.而已.

I'm a .NET programmer. I've never developed anything in MFC. Currently I had to write a C++ application (console) for some image processing task. I finished writing it. But the point is I need to design GUI also for this. Well, there won't be anything complex. Just a window with few Buttons, RadioButtons, Check Boxes, PicturesBox & few sliders. thats it.

我正在使用 VS 2008,并期待一个 .NET 样式的表单设计器.

I'm using VS 2008 and was expecting a .NET style form designer.

只是为了测试,我创建了一个 MFC 项目(所有默认配置),这些文件是默认创建的:

Just to test, I created a MFC project (with all default configuration) and these files were created by default:

ChildFrm.cpp
MainFrm.cpp
mfc.cpp
mfcDoc.cpp
mfcView.cpp
stdafx.cpp

现在,我找不到 Designer.没有视图 > 设计器.我已经打开了上述所有 *.cpp 并在代码编辑器中右键单击以查看设计器视图".ToolBox 是空的,因为我处于代码编辑器模式.

Now, I'm unable to find a Designer. There is no View > Designer. I've opened all the above *.cpp and in the code editor right clicked to see "Designer View". ToolBox is just empty because I'm in code editor mode.

如何打开设计师?

推荐答案

这扩展了 Alex Farber 的回答,基本上是正确的,但有些不完整.

This expands on Alex Farber's answer, which is basically correct but somewhat incomplete.

当您第一次创建应用程序时,您可以选择应用程序类型:

When you're first creating your application, you get to select an application type:

如您所见,Application type 的默认选择是Multiple Documents",但在其下方是Dialog Based".选择它会生成一个应用程序,其主窗口(默认情况下)有一个确定"按钮和一个取消"按钮(以及一个显示在此处添加控件"之类的静态控件).完成创建应用程序后,您可以添加更多控件以使其执行一些有用的操作.这往往最适合寿命相对较短的应用程序――即,您打开它们,填写一些字段,然后单击确定"(或取消")再次关闭它们.它也适用于其他场景,但这确实是它的主要优势.

As you can see, the default selection for the Application type is "Multiple Documents", but just below that is "Dialog Based". Selecting that will produce an application whose main window (by default) has an "Ok" button and a "Cancel" button (and a static control that says something like "add controls here"). When you finish creating the application, you can add more controls to get it to do something useful. That tends to work best for applications that are relatively short-lived -- i.e., you open them, fill in a few fields, and click "Ok" (or "Cancel") to close them again. It can work for other scenarios as well, but that's really its primary strength.

对于更像典型 .NET 应用程序的东西,具有普通菜单等,而且能够在窗口表面放置控件,您通常会在此处选择单一应用程序",但当您进入生成的类"屏幕:

For something more like a typical .NET application, with a normal menu and such, but also the ability to place controls on the window's surface, you'd typically select "Single Application" here, but when you get to the "Generated Classes" screen:

在视图类的基类下拉列表中,您需要将默认CView 的选择更改为CFormView.这为您提供了一种组合:您的应用程序作为一个整体基于 Document/View 架构,但您的 View 类基本上就像一个对话框,因此它可以承载控件.当您单击完成"按钮时,它会警告您打印支持将不可用.假设您同意,它将创建您的应用程序.要为您的窗口编辑表单(与您正在寻找的设计器的一般顺序相同),您将在左侧的资源视图"工具窗口中,并在对话框列表中打开表单:

In the drop-down list for the base class of your View Class, you need to change the selection for the default CView to CFormView. This gives you kind of a combination: your application as a whole is based on the Document/View architecture, but your View class basically acts like a dialog, so it can host controls. When you click the "Finish" button, it'll warn you that Printing support won't be available. Assuming you agree to that, it'll then create your application. To edit the form for your window (on the same general order as the Designer you're looking for), you'll in the tool window on the left for the "Resource View", and open the form in the list of dialogs:

打开它将(再次)让您使用对话框编辑器在表单上放置控件等:

Opening that will (again) let you use the dialog editor to put controls and such on your form:

总而言之:MFC 为您提供了更多选择.这些选择中的一个(或者说是两个)与您习惯使用的 .NET/WinForms 相当接近.其他的则完全不同――碰巧的是,默认选项属于不同"类别.

To summarize: MFC gives you quite a few more choices. One (or, sort of, two) of those choices correspond fairly closely to what you're accustomed to with .NET/WinForms. Others are quite different -- and as it happens, the default choices fall into the "different" category.

相关文章