Prev: MFC SDI Application without a default “New Document” on Startup
Next: How to handle Windows 7 screen resolution change?
From: Saint Atique on 20 Apr 2010 15:00 I'm trying to add an Edit Control to MainFrame. I'm creating an instance of CEdit in the paint message like this: void SAFrame::OnPaint() { CFrameWnd::OnPaint(); CEdit *PEdit = new CEdit; PEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 50, 150, 70), this, 0x1552); } But the control is not displayed. I think you have better ideas. -- Saint Atique [Novice MFC Programmer]
From: David Ching on 20 Apr 2010 15:08 "Saint Atique" <unix9n(a)gmail.com> wrote in message news:1db59444-17d4-4bc1-bc7a-1adcf3b75b9b(a)v8g2000vbh.googlegroups.com... > I'm trying to add an Edit Control to MainFrame. I'm creating an > instance of CEdit in the paint message like this: > > void SAFrame::OnPaint() { > CFrameWnd::OnPaint(); > > CEdit *PEdit = new CEdit; > > PEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | > WS_BORDER, > CRect(10, 50, 150, 70), this, 0x1552); > } > > But the control is not displayed. I think you have better ideas. > Don't call CFrameWnd::OnPaint() from within SAFrame::OnPaint(). It is not necessary and causes an error because EndPaint() is called before you have a chance to paint. Move the code to create the edit control into SAFrame::OnCreate() which is called once, not every time the window is painted. Rename PEdit to m_edit and make it a member of SAFrame. (Not a pointer, but a CEdit instance). Child windows like this are meant to have member instances and not pointers (due to the way the object is destroyed). -- David
From: Scott McPhillips [MVP] on 20 Apr 2010 16:07 > "Saint Atique" <unix9n(a)gmail.com> wrote in message > news:1db59444-17d4-4bc1-bc7a-1adcf3b75b9b(a)v8g2000vbh.googlegroups.com... >> I'm trying to add an Edit Control to MainFrame. I'm creating an >> instance of CEdit in the paint message like this: >> >> void SAFrame::OnPaint() { >> CFrameWnd::OnPaint(); >> >> CEdit *PEdit = new CEdit; >> >> PEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | >> WS_BORDER, >> CRect(10, 50, 150, 70), this, 0x1552); >> } >> >> But the control is not displayed. I think you have better ideas. I agree with David Ching's comments. In addition, if this is a document/view app you will not see the edit control because the client area of the CMainframe is always hidden behind the view window(s). What are you trying to accomplish, and have you considered using a CFormView for the control(s)? -- Scott McPhillips [VC++ MVP]
From: Joseph M. Newcomer on 20 Apr 2010 21:32 This is without a doubt some of the worst code I have seen! If you want to add a control to a main frame, you should add it at the point where the main frame is created! OnInitInstance. Doing anything in OnPaint EXCEPT painting is stupid. Actually, if you are doing an SDI app, create a CEditView instead; this is the correct approach. B ut NEVER, EVER do something this bad! Alternatively, have you considered a CFormView for your app, where you place the controls on a dialog template? Note that EVERY time the window is painted, a NEW edit control is created! I don't even know where you got those completely random numbers like 10 and 50 and 150 and 70. Did you roll dice? Or did you want to create an app that only works on your machine this week, with your current video card, your current display driver, your current display screen, your current display resolution, your current default font, and for all I know, for the current angular position between Jupiter and Mars. If you want to create a control, it MUST be based upon computations that are done relative to the font size in the control (I note you do not select a font for the control, which means you get the ugly default system font!), and other values derived from the current display settings and font information (see ::GetSystemMetrics, ::GetTextMetrics, and quite possibly CWnd::GetFont, CWnd::SetFont, GetStockObject, etc.) As soon as you hardwire a number like this into a graphics routine, you can be dead certain you have a fundamental design defect. Here';s a simple rule: if your OnPaint handler does ANYTHING other than GDI commands to draw, or computations of parameters to GDI calls to draw, then you have made a terrible, terrible mistake in your design, and you must rewrite the code so there is no longer anything done in the OnPaint handler EXCEPT graphics drawing operations. This is an example of an absolutely unacceptable practice. joe On Tue, 20 Apr 2010 12:00:38 -0700 (PDT), Saint Atique <unix9n(a)gmail.com> wrote: >I'm trying to add an Edit Control to MainFrame. I'm creating an >instance of CEdit in the paint message like this: > >void SAFrame::OnPaint() { > CFrameWnd::OnPaint(); > > CEdit *PEdit = new CEdit; > > PEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | >WS_BORDER, > CRect(10, 50, 150, 70), this, 0x1552); >} > >But the control is not displayed. I think you have better ideas. > >-- Saint Atique >[Novice MFC Programmer] Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Oliver Regenfelder on 21 Apr 2010 09:26
Hello, David Ching wrote: > Rename PEdit to m_edit and make it a member of SAFrame. (Not a pointer, > but a CEdit instance). Child windows like this are meant to have member > instances and not pointers (due to the way the object is destroyed). Could you explain this a bit more in detail please? What would be the problem if I make it CEdit* m_edit; allocate it inside OnCreate m_edit = new .... // and so on and then deallocate it inside the destructor like if(m_edit) delete m_edit I would be interested especially in windows API/MFC implications of doing this not any strictly C++ ones (like use std::auto_ptr/std::unique_ptr). Best regards, Oliver |