简体   繁体   English

如何在MFC对话框中添加画布?

[英]How to add canvas in MFC Dialog?

I want to create an application which main window has canvas (or something where I can draw custom things) and some controls, like buttons and edit fields. 我想创建一个应用程序,该应用程序的主窗口具有画布(或可以在其中绘制自定义对象的画布)和一些控件,例如按钮和编辑字段。 But I got stuck about how to do it. 但是我对如何做到这一点感到困惑。

I tried to create MFC with SDI, but how to add control to CDC..? 我试图用SDI创建MFC,但是如何将控件添加到CDC ..? I tried to create one dialog with buttons and edit fields, but which control refers to something I can draw at..? 我试图用按钮和编辑字段创建一个对话框,但是哪个控件指向我可以在其中绘制的内容。

Please, enlighten me how to do this.. 拜托,请赐教我该怎么做。

Its been a few years for me, but here goes: 对我来说已经有几年了,但是这里有:

I don't think that MFC has a specific canvas control. 我认为MFC没有特定的画布控件。 Instead, when I wanted a drawing surface, I added a group box to the form in design mode. 相反,当我需要绘图表面时,我在设计模式下向表单添加了一个组框。 I made the group box invisible, so it would not show up at runtime. 我使组框不可见,因此它不会在运行时显示。

In the OnCreate handler for the form view, I created a CWnd, and gave it the size and location of the invisible group box. 在窗体视图的OnCreate处理程序中,我创建了一个CWnd,并为其指定了不可见组框的大小和位置。

I set up an OnPaint message handler for the CWnd, and voila, instant graphics canvas, or a canvas for whatever else you may need. 我为CWnd设置了一个OnPaint消息处理程序,然后瞧瞧,即时图形画布或其他可能需要的画布。

Now, this was last done five years ago, and MFC may have advanced incrementally since then, but this is the general mechanism. 现在,这是五年前完成的,此后MFC可能已逐步改进,但这是一般机制。

Instead of SDI, use a Dialog based application. 代替SDI,使用基于对话框的应用程序。 You can easily add any controls you wish to a dialog. 您可以轻松地将所需的任何控件添加到对话框。

You will probably want to make the app resizable. 您可能需要使应用程序可调整大小。 Set the border style to Thick and enable the minimize and Maximize buttons. 将边框样式设置为“厚”,然后启用“最小化”和“最大化”按钮。 Override OnSize to move and/or resize the controls as the dialog size changes. 覆盖OnSize以在对话框大小更改时移动和/或调整控件大小。 Override OnSizing if you need to set a minimum size for the window. 如果需要为窗口设置最小尺寸,请覆盖OnSizing。

The easiest way to do arbitrary drawing on the dialog is to override OnPaint. 在对话框上进行任意绘制的最简单方法是覆盖OnPaint。 Define an area of the dialog to contain your custom drawing, perhaps surrounding it with a frame control, and just draw into the DC that OnPaint creates. 定义对话框的区域以包含您的自定义图形,也许用框架控件将其包围,然后将其绘制到OnPaint创建的DC中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM