Alex Turbin
Alex Turbin

Reputation: 2702

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..? 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..

Upvotes: 0

Views: 3192

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308101

Instead of SDI, use a Dialog based application. 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. Override OnSizing if you need to set a minimum size for the window.

The easiest way to do arbitrary drawing on the dialog is to override 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.

Upvotes: 0

kmontgom
kmontgom

Reputation: 1429

Its been a few years for me, but here goes:

I don't think that MFC has a specific canvas control. 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.

I set up an OnPaint message handler for the CWnd, and voila, instant graphics canvas, or a canvas for whatever else you may need.

Now, this was last done five years ago, and MFC may have advanced incrementally since then, but this is the general mechanism.

Upvotes: 2

Related Questions