Prev: Changing the Backcolor of Tab Control in MFC ,Winxp
Next: Multiple closed networks and UDP. Please help me.
From: Stephen Myers on 19 Oct 2009 12:04 Luigino wrote: >> CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); >> >> is not at all the way things should be done. There is no reason to >> create WndStatic. GetDlgItem should not be used in MFC. Check the >> source of your code snippets. Chances are very good they are either not >> written for MFC or written by someone who had not figured out what you >> get with MFC. >> >> Use c_MyStaticControl. instead of WndStatic-> >> >> This will not cirrect your problem. >> >> I suspect that the real problem is due to accessing the control before >> it is created (in OnInitDialog). >> >> Steve > > HI Steve, > > the fact is I created a CStatic in the resource window, and I created > a Control Variable for this CStatic so the variable created is a > CStatic type and the first parameter of SetWindowPos() which is a > const CWnd *pWndInsertAfter wouldn't cast from CStatic to const CWnd, > that's why I declared that variable. > Actually I can "create" my cwnd derived class which now fires an > OnCreate event and it executes those instructions: > > CPaintDC dc(this); > CDC MemDC; > > if (!MemDC.CreateCompatibleDC(&dc)) > return; > > CRect rect; > GetClientRect(rect); // because it is a client > MemDC.FillRect(rect, CBrush::FromHandle((HBRUSH) GetStockObject > (BLACK_BRUSH))); > MemDC.Ellipse(rect); > > but when I SetWindowPos with WndCStatic it appears the window without > showing the control... > > in the OnInitDialog I coded like here: > > CRect rect; > c_MyStaticControl.GetWindowRect(&rect); > ScreenToClient(&rect); > CCaChart c_MyControl; > CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); > c_MyControl.Create(_T("MFCCaChartCtrl"), _T(""), WS_CHILD|WS_VISIBLE| > WS_BORDER, rect, this, IDC_MYCONTROL); > c_MyControl.SetWindowPos(c_WndStatic, 0, 0, 0, 0, SWP_NOSIZE| > SWP_NOMOVE); > c_MyStaticControl.DestroyWindow(); > > from an example at begin of this post I tried to put c_MyStaticControl > in the SetWindowPost but no cast available... Try .... SetWindowPos(&wndTop,...); or .... SetWindowPos(&c_MyStaticControl,...); Please remove CWnd* WndStatic ... ; It is similar to the following double a; double *b = (double *)(&a); c = *b; Steve
From: Luigino on 19 Oct 2009 18:22 On 19 Ott, 18:04, Stephen Myers <""StephenMyers \"@discussi...(a)microsoft.com"> wrote: > Luigino wrote: > >> CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); > > >> is not at all the way things should be done. There is no reason to > >> create WndStatic. GetDlgItem should not be used in MFC. Check the > >> source of your code snippets. Chances are very good they are either not > >> written for MFC or written by someone who had not figured out what you > >> get with MFC. > > >> Use c_MyStaticControl. instead of WndStatic-> > > >> This will not cirrect your problem. > > >> I suspect that the real problem is due to accessing the control before > >> it is created (in OnInitDialog). > > >> Steve > > > HI Steve, > > > the fact is I created a CStatic in the resource window, and I created > > a Control Variable for this CStatic so the variable created is a > > CStatic type and the first parameter of SetWindowPos() which is a > > const CWnd *pWndInsertAfter wouldn't cast from CStatic to const CWnd, > > that's why I declared that variable. > > Actually I can "create" my cwnd derived class which now fires an > > OnCreate event and it executes those instructions: > > > CPaintDC dc(this); > > CDC MemDC; > > > if (!MemDC.CreateCompatibleDC(&dc)) > > return; > > > CRect rect; > > GetClientRect(rect); // because it is a client > > MemDC.FillRect(rect, CBrush::FromHandle((HBRUSH) GetStockObject > > (BLACK_BRUSH))); > > MemDC.Ellipse(rect); > > > but when I SetWindowPos with WndCStatic it appears the window without > > showing the control... > > > in the OnInitDialog I coded like here: > > > CRect rect; > > c_MyStaticControl.GetWindowRect(&rect); > > ScreenToClient(&rect); > > CCaChart c_MyControl; > > CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); > > c_MyControl.Create(_T("MFCCaChartCtrl"), _T(""), WS_CHILD|WS_VISIBLE| > > WS_BORDER, rect, this, IDC_MYCONTROL); > > c_MyControl.SetWindowPos(c_WndStatic, 0, 0, 0, 0, SWP_NOSIZE| > > SWP_NOMOVE); > > c_MyStaticControl.DestroyWindow(); > > > from an example at begin of this post I tried to put c_MyStaticControl > > in the SetWindowPost but no cast available... > > Try > > ... SetWindowPos(&wndTop,...); > > or > > ... SetWindowPos(&c_MyStaticControl,...); > > Please remove CWnd* WndStatic ... ; It is similar to the following > > double a; > double *b = (double *)(&a); > > c = *b; > > Steve I tried to change putting &c_MyStaticControl and I put those three lines of code in the OnInitDialog so in the class I can have OnCreate event that's fired and where I have those drawing lines code FillRect and Ellipse but it didn't draw anything... I thought it would be fired the OnPaint to draw whatever it has to be drawn on the CWnd but that event doesn't fire so it should be in the OnCreate but it doesn't show anything....so I don't know if I could have missed something in the OnCreate or I should fire different event for drawing... Suggests?....
From: Joseph M. Newcomer on 19 Oct 2009 21:22 On Mon, 19 Oct 2009 08:02:51 -0700 (PDT), Luigino <npuleio(a)rocketmail.com> wrote: >> CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); >> >> is not at all the way things should be done. There is no reason to >> create WndStatic. GetDlgItem should not be used in MFC. Check the >> source of your code snippets. Chances are very good they are either not >> written for MFC or written by someone who had not figured out what you >> get with MFC. >> >> Use c_MyStaticControl. instead of WndStatic-> >> >> This will not cirrect your problem. >> >> I suspect that the real problem is due to accessing the control before >> it is created (in OnInitDialog). >> >> Steve > >HI Steve, > >the fact is I created a CStatic in the resource window, and I created >a Control Variable for this CStatic so the variable created is a >CStatic type and the first parameter of SetWindowPos() which is a >const CWnd *pWndInsertAfter wouldn't cast from CStatic to const CWnd, >that's why I declared that variable. >Actually I can "create" my cwnd derived class which now fires an >OnCreate event and it executes those instructions: > >CPaintDC dc(this); >CDC MemDC; > >if (!MemDC.CreateCompatibleDC(&dc)) > return; > >CRect rect; >GetClientRect(rect); // because it is a client >MemDC.FillRect(rect, CBrush::FromHandle((HBRUSH) GetStockObject >(BLACK_BRUSH))); >MemDC.Ellipse(rect); > >but when I SetWindowPos with WndCStatic it appears the window without >showing the control... > >in the OnInitDialog I coded like here: > >CRect rect; >c_MyStaticControl.GetWindowRect(&rect); >ScreenToClient(&rect); >CCaChart c_MyControl; >CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); *** This is nonsense. Why? You *already have* c_MyStaticControl so doing a GetDlgItem is a nonsensical waste of time and effort! In any conceivable context in which you would use WndStatic-> you can use c_MyStaticControl. and get the effect you want! So there is no reason to have ever written this line of nonsense code! **** >c_MyControl.Create(_T("MFCCaChartCtrl"), _T(""), WS_CHILD|WS_VISIBLE| >WS_BORDER, rect, this, IDC_MYCONTROL); >c_MyControl.SetWindowPos(c_WndStatic, 0, 0, 0, 0, SWP_NOSIZE| >SWP_NOMOVE); *** The correct first argument is &c_yStaticControl; the error is obviously a complete misunderstanding of the C language. The first argument has to be a CWnd *. So why would you create a variable for that purpose *when you already have a variable that serves that purpose*???? You are not only using GetDlgItem in a context which should never occur, but your are *replicating* information *that already exists*! **** >c_MyStaticControl.DestroyWindow(); > >from an example at begin of this post I tried to put c_MyStaticControl >in the SetWindowPost but no cast available.. **** Of course not, because c_MyStaticControl is a CWnd, and SetWindowPos wants a CWnd*. It should be screamingly obvious to anyone who understands C or C++ that the & operator does this! I see this error all the time, and I have absolutely *no idea* why such an elementary concept such as the address-of (&) operator is uniformly missed! joe **** Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on 19 Oct 2009 21:28 It is (a) not clear why you want or should even consider an OnCreate handler and (b) why you think an OnCreate handler should duplicate the code that is in OnPaint. Both of these are probably serious design errors. I do lots of custom controls using DLLs and not once have I ever had to either use OnCreate or put one in for the purpose of duplicating code that correctly functions elsewhere. Another comment below... joe On Mon, 19 Oct 2009 15:22:05 -0700 (PDT), Luigino <npuleio(a)rocketmail.com> wrote: >On 19 Ott, 18:04, Stephen Myers <""StephenMyers >\"@discussi...(a)microsoft.com"> wrote: >> Luigino wrote: >> >> CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); >> >> >> is not at all the way things should be done. �There is no reason to >> >> create WndStatic. �GetDlgItem should not be used in MFC. �Check the >> >> source of your code snippets. �Chances are very good they are either not >> >> written for MFC or written by someone who had not figured out what you >> >> get with MFC. >> >> >> Use c_MyStaticControl. instead of WndStatic-> >> >> >> This will not cirrect your problem. >> >> >> I suspect that the real problem is due to accessing the control before >> >> it is created (in OnInitDialog). >> >> >> Steve >> >> > HI Steve, >> >> > the fact is I created a CStatic in the resource window, and I created >> > a Control Variable for this CStatic so the variable created is a >> > CStatic type and the first parameter of SetWindowPos() which is a >> > const CWnd *pWndInsertAfter wouldn't cast from CStatic to const CWnd, >> > that's why I declared that variable. >> > Actually I can "create" my cwnd derived class which now fires an >> > OnCreate event and it executes those instructions: >> >> > CPaintDC dc(this); >> > CDC MemDC; >> >> > if (!MemDC.CreateCompatibleDC(&dc)) >> > � �return; >> >> > CRect rect; >> > GetClientRect(rect); � � � // because it is a client >> > MemDC.FillRect(rect, CBrush::FromHandle((HBRUSH) GetStockObject >> > (BLACK_BRUSH))); >> > MemDC.Ellipse(rect); >> >> > but when I SetWindowPos with WndCStatic it appears the window without >> > showing the control... >> >> > in the OnInitDialog I coded like here: >> >> > CRect rect; >> > c_MyStaticControl.GetWindowRect(&rect); >> > ScreenToClient(&rect); >> > CCaChart c_MyControl; >> > CWnd* WndStatic = GetDlgItem(c_MyStaticControl.GetDlgCtrlID()); >> > c_MyControl.Create(_T("MFCCaChartCtrl"), _T(""), WS_CHILD|WS_VISIBLE| >> > WS_BORDER, rect, this, IDC_MYCONTROL); **** Also, why are you hardwiring IDC_MYCONTROL? I create the static control with the ID I want to use, then use c_MyStaticControl.GetDlgCtrlID() to retrieve that ID so I can use it. Since I destroy the window, the brief period in which two controls of the same ID exist is irrelevant, because during that time nobody accesses the control by ID. The second parameter to Create can be NULL. Unfortunately, this is not correctly documented. I note that the CCaChart variable appears to be a local variable; of course, what this means is that as soon as OnInitDialog completes, the window is destroyed. This variable *MUST* be a variable declared in your class! The reason you do not see the window paint is *there is no window there*. It has been destroyed. joe **** >> > c_MyControl.SetWindowPos(c_WndStatic, 0, 0, 0, 0, SWP_NOSIZE| >> > SWP_NOMOVE); >> > c_MyStaticControl.DestroyWindow(); >> >> > from an example at begin of this post I tried to put c_MyStaticControl >> > in the SetWindowPost but no cast available... >> >> Try >> >> ... SetWindowPos(&wndTop,...); >> >> or >> >> ... SetWindowPos(&c_MyStaticControl,...); >> >> Please remove CWnd* WndStatic ... ; �It is similar to the following >> >> double a; >> double *b = (double *)(&a); >> >> c = *b; >> >> Steve > >I tried to change putting &c_MyStaticControl and I put those three >lines of code in the OnInitDialog so in the class I can have OnCreate >event that's fired and where I have those drawing lines code FillRect >and Ellipse but it didn't draw anything... >I thought it would be fired the OnPaint to draw whatever it has to be >drawn on the CWnd but that event doesn't fire so it should be in the >OnCreate but it doesn't show anything....so I don't know if I could >have missed something in the OnCreate or I should fire different event >for drawing... >Suggests?.... Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Luigino on 22 Oct 2009 09:01
OK it looks like now I figured out about to draw a black background with a green grid inside an OnDraw() event. Since I want to initialize the control inside creation method, I supposed to start getting the DC since, as I did before in OnDraw(), I know calling "CPaintDC dc(this);" means performing a BeginPaint and EndPaint... So I did: CMemDC MemDC(this->GetDC()); CRect rect; GetClientRect(rect); then the rest of the code to fillrect a black rectangle as background, selecting a pen to draw all those lines with MoveTo and LineTo....and I commented the OnDraw() because this background has to be drawn in my own CCreate event. Obviously I called the Create method properly of CWnd and SetWindowPos() and the result is that it doesn't draw anything... (no errors and in debug it executes this code correctly)... Maybe I shall call some instruction to force drawing on the context as initialization?.... Thanks to everyone!!! Ciao Luigi |