views:

283

answers:

3

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

+2  A: 

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.

kmontgom
Alternatively a CFormView can be used instead of a CDialog as the parent window.
adam
Thank you, it helped me very much. :)
Alex Stamper
A: 

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.

Mark Ransom
A: 
@kmontgom
How do you get the size and location of GroupBox you've added?
And where do you use this arguments? In CWnd::Create method?
In which class do you define OnPaint() handler for CWnd? In dialog class?
I already have that handler generated in dialog class, by the app wizard.
And where do I, later,  use for example, methods for drawing lines?
In OnPaint() method?

Sorry, I'm working in MFC for a couple of days, and I'm so confused about it.
Thanks in advance, 
Cheers
kobac
If you have given the GroupBox its own ID, then you can use CWnd::GetDlgItem(ID) to get a pointer to the GroupBox window. After that, you can use the regular methods to get the size/location of the Group Box. I can't quite remember the MFC/Win32 API call to do this (I'm neck-deep in WPF these days), but it is fairly easy.
kmontgom
I should also say that you can do this wherever/whenever required, after your window or dialog has been properly rendered the first time.One approach is to overload OnInitDialog, and in that method, get the size/location of the GroupBox, and store as a CRect somewhere.In OnPaint, retrieve that CRect, and draw whatever you want inside the bounds of the CRect; you may have to set up a clipping region to ensure that all drawing remains inside the CRect bounds.Hope that helps.
kmontgom
Ok, I'll try it.Thank you!
kobac