Q: Controls within CChildView? 
Author Message
 Q: Controls within CChildView?

Greetings all,

How do I manually create a control resource in CChildView? I'm using
the simple & bare skeleton code generated by the VC6 MFC wizard, SDI
with no doc/view.

For example: When I put
CButton m_button;
in Class CChildView as protected, then call

m_button.Create(Create  ("Calc Cost",
           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
           CRect(CPoint(1,1), CSize(100,25)),
           this, 500);

in the constructor CChildView::CChildView, it doesn't show up... :(

When I did the same in a bare minimum MFC application that I manually
wrote (without the wizard, where I only had CMainFrame and CMyApp and
practically everything take place in CMainFrame), it works.

But when I use this CChildView-philosophy based auto generated code,
it won't work for some reason. Usual GDI drawings work though. For
example, dc.Rectangle(r) in CChildView::OnPaint works. In this case,
OnPaint in CChildView seems to behave in the same way as OnPaint in
CMainFrame I did previously.

I understand that the philosophy behind CChildView is to separate the
drawing area from menu/icon/statusbar (and whatever else). Or is that
where I'm going wrong?

Thank you.



Sun, 11 Sep 2005 20:31:07 GMT  
 Q: Controls within CChildView?

Quote:

> Greetings all,

> How do I manually create a control resource in CChildView? I'm using
> the simple & bare skeleton code generated by the VC6 MFC wizard, SDI
> with no doc/view.

> For example: When I put
> CButton m_button;
> in Class CChildView as protected, then call

> m_button.Create(Create  ("Calc Cost",
>            WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
>            CRect(CPoint(1,1), CSize(100,25)),
>            this, 500);

> in the constructor CChildView::CChildView, it doesn't show up... :(

> When I did the same in a bare minimum MFC application that I manually
> wrote (without the wizard, where I only had CMainFrame and CMyApp and
> practically everything take place in CMainFrame), it works.

> But when I use this CChildView-philosophy based auto generated code,
> it won't work for some reason. Usual GDI drawings work though. For
> example, dc.Rectangle(r) in CChildView::OnPaint works. In this case,
> OnPaint in CChildView seems to behave in the same way as OnPaint in
> CMainFrame I did previously.

> I understand that the philosophy behind CChildView is to separate the
> drawing area from menu/icon/statusbar (and whatever else). Or is that
> where I'm going wrong?

> Thank you.

Your problem is that you can't create a child window before the parent
window exists.  So you can't do this in the constructor.  Override the
CChildView Create function and create your button after the call to
CWnd::Create returns.

MFC uses "two phase construction" for windows.  Creating any CWnd
derived object does not create a window, only a sort of container for an
HWND, which is initially NULL.  Then a call to the object's Create
function is needed to create the underlying window and HWND.

--
Scott McPhillips [VC++ MVP]



Mon, 12 Sep 2005 00:44:53 GMT  
 Q: Controls within CChildView?
On Wed, 26 Mar 2003 18:44:53 -0500, Scott McPhillips

Quote:

>Your problem is that you can't create a child window before the parent
>window exists.  So you can't do this in the constructor.  Override the
>CChildView Create function and create your button after the call to
>CWnd::Create returns.

>MFC uses "two phase construction" for windows.

Thank you, that makes a lot of sense now!

On another note, how do I change the size of a control (such as
buttons and lists) after the initial creation? Create() usually takes
a RECT structure which determines the initial size, which would only
do good if the size was to be permanently fixed. Isn't there some sort
of method that allows size change, like say m_button.ChangeSize(rect)?

I ask that because I have the following concerns:

(1) GetClientSize seems to return 0,0,0,0 inside CChildView::Create,
meaning anything to determine size is useless in here. (I wish there
was PostCreateWindow() or something!)

(2) Size change is necessary anyway, upon messages like WM_SIZE.

Any insight would help, thank you.



Mon, 12 Sep 2005 17:57:22 GMT  
 Q: Controls within CChildView?

Quote:

> On Wed, 26 Mar 2003 18:44:53 -0500, Scott McPhillips

> >Your problem is that you can't create a child window before the parent
> >window exists.  So you can't do this in the constructor.  Override the
> >CChildView Create function and create your button after the call to
> >CWnd::Create returns.

> >MFC uses "two phase construction" for windows.

> Thank you, that makes a lot of sense now!

> On another note, how do I change the size of a control (such as
> buttons and lists) after the initial creation? Create() usually takes
> a RECT structure which determines the initial size, which would only
> do good if the size was to be permanently fixed. Isn't there some sort
> of method that allows size change, like say m_button.ChangeSize(rect)?

> I ask that because I have the following concerns:

> (1) GetClientSize seems to return 0,0,0,0 inside CChildView::Create,
> meaning anything to determine size is useless in here. (I wish there
> was PostCreateWindow() or something!)

> (2) Size change is necessary anyway, upon messages like WM_SIZE.

> Any insight would help, thank you.

In your search for control methods, keep in mind that all controls are
derived from CWnd.  Functions that are common to all controls are
inherited from CWnd.  Like MoveWindow, ShowWindow, EnableWindow,
DestroyWindow...

Sizing and resizing of child windows should be done in WM_SIZE
(OnSize).  There is a gotcha when doing this: the control may not exist
when OnSize is called.  So look before you leap:

void CFtView::OnSize(UINT nType, int cx, int cy)
{
 if (IsWindow(m_hWnd) && IsWindow(m_Grid.m_hWnd))
 {
   CFormView::OnSize(nType, cx, cy);
   m_Grid.MoveWindow(0, CAPTION_HEIGHT, cx, cy - CAPTION_HEIGHT);
 }

Quote:
}

--
Scott McPhillips [VC++ MVP]


Tue, 13 Sep 2005 02:53:26 GMT  
 Q: Controls within CChildView?
On Thu, 27 Mar 2003 20:53:26 -0500, Scott McPhillips

Quote:

>In your search for control methods, keep in mind that all controls are
>derived from CWnd.  Functions that are common to all controls are
>inherited from CWnd.  Like MoveWindow, ShowWindow, EnableWindow,
>DestroyWindow...

>Sizing and resizing of child windows should be done in WM_SIZE
>(OnSize).  There is a gotcha when doing this: the control may not exist
>when OnSize is called.  So look before you leap:

>void CFtView::OnSize(UINT nType, int cx, int cy)
>{
> if (IsWindow(m_hWnd) && IsWindow(m_Grid.m_hWnd))
> {
>   CFormView::OnSize(nType, cx, cy);
>   m_Grid.MoveWindow(0, CAPTION_HEIGHT, cx, cy - CAPTION_HEIGHT);
> }
>}

Thank you very much, I begin to get the picture now... while it seems
overcomplicated, I can see there is a very uniform philosophy that the
framework is rigidly following in all this, but still a mine field.

I couldn't find a way to add the message map for ON_WM_SIZE() using
Class Wizard for some reason... (why is this?? and yes CChildView was
generated by the app wizard so it should be aware) so I made the
necessary changes manually as I used to do when I wasn't using
AppWizard to generate MFC application. Is that ok right? It works
anyway, and cx,cy holds the correct width/height too, which is
wonderful.

All is well for now anyhow, so thank you :) MFC seems to be an
incredible science from OOP point of view.



Tue, 13 Sep 2005 15:53:57 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Flow Layout of custom controls within a parent control or page

2. retrieve handle of the control from within the control when the container is vb

3. retrieiving handle of the control....from within control

4. Newbie: Control within a Control

5. Controls locking up the application in sheet contained within a TabCtrl ( Controls, CDialog and CTabCtrl problems )

6. Controls locking up the application in sheet contained within a TabCtrl ( Controls, CDialog and CTabCtrl problems )

7. Controls within ActiveX controls

8. VB ActiveX Control encapsulated within a VC Control

9. CChildView question

10. CChildView being updated constantly!

11. Accessing CChildView's m_hWnd

12. can't use ClientToScreen() with CChildView

 

 
Powered by phpBB® Forum Software