Windows messages in child window - mfc

I have a container window (CFrameWnd) that contains child windows, if I set the spy++ on a child window in some cases I do not see any message passing. Why? what 's the system that regulates messages between windows? I checked that all windows are enabled.
The child window inherits from CDialog and has the WS_CHILD style
class PanelDialog : public CDialog
{
DECLARE_DYNAMIC(PanelDialog)
public:
PanelDialog(CWnd* pParent = NULL); // standard constructor
virtual ~PanelDialog();
// Dialog Data
enum { IDD = IDD_PANEL_DIALOG };
private:
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
};

I find this article by Paul DiLascia to be very good at explaining the relation between messages, windows and how they are routed.

Related

I am extending CTabCtrl but but cant insert any tabs

I am extending CTabCtrl but when I call InsertItem on my extended object none tab gets inserted. Who knows why is that. What do I do wrong?
class MyTabControl : public CTabCtrl
{
public:
MyListControl m_listCtrl;
void switchInterface(IDataProvider *provider);
public:
MyTabControl();
~MyTabControl();
afx_msg void OnGetDispInfo(NMHDR *pNMHDR, LRESULT *pResult);
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
If I remove ON_WM_CREATE() macro from message map then I can add tabs. Implementation of OnCreate function contains m_listCtrl.Create() function call and return 0 if list control is created successfully. What is wrong with this?
The CTabCtrl class is terribly old and poorly functional; you will have to do all the showing/hiding logic of controls when user is switching from one tab to another by your own hand. I recommend you to extend from CMFCTabCtrl instead.

Why child control doesn't appear in MFC derived CWnd

I want to add simple Cedit to my derived GUI class from CWnd. This class is simple container and treat the same as Panel in MFC. in constructor of class I add simple CEdit instance in the class.
but the when I instantiated the panel in client dialog, the panel shows but the button does not show.why it doesn't show.
The panel code
Panel header file
#pragma once
#include "afxwin.h"
class CPanel :
public CWnd
{
public:
CPanel(void);
~CPanel(void);
virtual void PreSubclassWindow();
virtual void DoDataExchange(CDataExchange* pDX);
CEdit *txt;
DECLARE_MESSAGE_MAP()
};
panel source file
#include "stdafx.h"
#include "Panel.h"
CPanel::CPanel(void)
{
WNDCLASS wndcls;
HINSTANCE hins=AfxGetInstanceHandle();
if(!(::GetClassInfo(hins,_T("CPanelCtrl"),&wndcls))){
wndcls.style=CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
wndcls.lpfnWndProc=::DefWindowProc;
wndcls.cbClsExtra=wndcls.cbWndExtra=0;
wndcls.hInstance=hins;
wndcls.hIcon=NULL;
wndcls.hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS );
wndcls.hbrBackground=(HBRUSH)(COLOR_3DFACE+13);
wndcls.lpszMenuName=NULL;
wndcls.lpszClassName=_T("CPanelCtrl");
txt=new CEdit();
txt->Create(ES_PASSWORD,CRect(10,10,25,35),this,1);
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return;
}
else{
return;
}
}
}
CPanel::~CPanel(void)
{
}
void CPanel::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CWnd::PreSubclassWindow();
}
void CPanel::DoDataExchange(CDataExchange* pDX)
{
// TODO: Add your specialized code here and/or call the base class
CWnd::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPanel, CWnd)
END_MESSAGE_MAP()
In dialog box in OninitDialog method I do like this
panel=new CPanel();
panel->Create(L"CPanelCtrl",L"Hello ", WS_VISIBLE , CRect(70, 70, 400, 200), this, 1);
Two hints:
first: you assign to your CEdit the same ID as to the CPanel: 1.
Second: you create CEdit inside CPanel constructor, I would try creating it inside CPanel WM_CREATE handler, because inside CPanel constructor HWND of CPanel is not yet assigned.
Your code to create the edit controls only runs once, because after the window class is registered the edit control is never be created.
You can't create a child window, when the parent window is created.
Create child windows on in the WM_CREATE handler of the parent.
If you always have an edit control, why do you use a pointer to it. Just create a simple member.
You should use WS_CHILD if the edit control is located inside the panel.

Changing static text in dialog box at runtime

I have created a dialog box and linked it to the menu item. In this case the menu item is Help -> Statistics. It all works. So when I run the program, click on the menu Help, then Statistics, a dialog box pops up.
I also have a static text box in the dialog box. How do you change the text of this static text box at runtime?
P.S: Though I have a dialog box up and running, I do not have the handle for the dialog box. If any of your solutions involve knowing the handle to the dialog box, please tell me how to retrieve it. Thanks.
EDIT:
class CStatisticsDlg : public CDialogEx
{
public:
CStatisticsDlg();
// Dialog Data
enum { IDD = IDD_STATISTICS };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
};
CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}
void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
In Class Wizard, create a CString member variable for the label. Note: by default, labels don't have a custom id so you have to give it one like IDC_MY_LABEL.
Somewhere before showing the dialog call m_strMyLabel.SetWindowText("blah");
If you need to do it while the dialog is open you have to call UpdateData(FALSE)
Edit: if you don't want to create a member variable you can
**corrected - typing from memory....
// Find the label
// if called from within CStatusDlg class
CWnd *label = GetDlgItem(IDC_MY_LABEL);
label->SetWindowText("blah");
// If called from elsewhere
CStatusDlg dlg..... // create the dialog
CWnd *label = dlg.GetDlgItem(IDC_MY_LABEL);
label->SetWindowText("blah");

C++/MFC Error accessing control's variable

I created a control's variable for CEdit:
class CGateDlg : public CDialog
{
...
public:
// here is my control's variable
CEdit m_edit_a;
// here I map variable to control
virtual void DoDataExchange(CDataExchange* pDX);
}
And this is how I map my variable to the control:
void CGateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_A, m_edit_a);
}
This is how it works: user types some text into the edit box. Then he presses the "Reset" button which clears the edit box. This is a piece of code responsible for clearing edit box after clicking Reset button:
void CGateDlg::OnBnClickedReset()
{
// clear edit box
m_edit_a.SetWindowTextW(L"");
}
Application starts without any errors. I type some text into EditBox and hit "Reset" button. Then I get an error which leads me to winocc.cpp, line 245 (ENSURE(this)):
void CWnd::SetWindowText(LPCTSTR lpszString)
{
ENSURE(this);
ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
if (m_pCtrlSite == NULL)
::SetWindowText(m_hWnd, lpszString);
else
m_pCtrlSite->SetWindowText(lpszString);
}
I think the problem is with the hWnd:
this 0x0030fa54 {CEdit hWnd=0x00000000} CWnd * const
but how to fix it ?
Everything works fine when I access my control's value using this:
CEdit *m_edit_a;
m_edit_a = reinterpret_cast<CEdit *>(GetDlgItem(IDC_EDIT_A));
m_edit_a->SetWindowTextW(L"");
What am I doing wrong ?
I can see two possibilities:
The control does not exist when the dialog starts. The first thing that CDialog::OnInitDialog will do is call DoDataExchange, so if you're creating the control later in the initialization process it's too late.
Your own OnInitDialog is not calling CDialog::OnInitDialog so DoDataExchange is not being called.
I think you should no use directly the meber of your control (in this case m_edit_a). Instead you should use a memeber variable, let's say CStrimg m_edit_data, and you should link it to the control:
DDX_Text(pDX, IDC_EDIT_A, m_edit_data); // as you did it in DDC_Cotrol
Now you can use directy the variable, but in order the control to be updated you should use the following code before using it:
UpdateData(true); // unlocks the control in a sense
m_edit_data = "this is my test";
UpdateData(false); // locks the control again (in a sense)
This is normal procedure in MFC :), hope I helped...
ohh... you should also add the control to String Table ... (let me know if you do not know)
I can not find something wrong with you. I Create a new project using VC6.0,and associate a variable to the Edit,just link you do. the exe operates normally.
class CEditTestDlg : public CDialog
{
// Construction
public:
CEditTestDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CEditTestDlg)
enum { IDD = IDD_EDITTEST_DIALOG };
CEdit m_Edit;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditTestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
......
.cpp
void CEditTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEditTestDlg)
DDX_Control(pDX, IDC_EDIT1, m_Edit);
//}}AFX_DATA_MAP
}
void CEditTestDlg::OnBnClickedReset()
{
// TODO: Add your control notification handler code here
m_Edit.SetWindowText("tttt");
}
so,I think it is not a code problem.You had better try again.
If your dialog starts off calling CDialog::OnInitDialog() and your DoDataExchange starts off calling CDialog::DoDataExchange but still you have null hWnd pointers and get CNotSupportedException, make sure your resource (rc) file's dialog template includes all the controls (IDC_) and such you have in DoDataExchange.
Check for overriding definitions if using a DLL that also provides resources.

Handling WM_PAINT in a Subclassed CStatic Control

I created a custom control whose class has CStatic as base class. Currently I handle the drawing using WM_PAINT event. But there is a strange behavior. When I re-enable the window after disabling it using CWnd::EnableWindow function, it refuses to draw what I written in OnPaint function. It draws the static control instead.
I agree that there is this standard method of overriding DrawItem and using SS_OWNERDRAW style. But what's wrong with WM_PAINT?
void XXControl::OnPaint()
{
CPaintDC PaintDC( this );
// ** draw the control to PaintDC**
}
Here is exactly what I have written:
class CMyStatic : public CStatic
{
DECLARE_MESSAGE_MAP()
public:
void OnPaint(void);
};
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyStatic::OnPaint(void)
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(120,255,0));
}
And subclassed:
class CMyDlg : public CDialog
{
// Construction
CMyStatic my_static;
...
};
BOOL CCMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
my_static.SubclassDlgItem(IDC_DRAW, this);
return true;
}
Where IDC_DRAW is static control on resource for this dialog. I wrote two button handlers:
void CMyDlg::OnBnClickedOk()
{
my_static.EnableWindow(FALSE);
my_static.Invalidate();
}
void CMyDlg::OnBnClickedOk2()
{
my_static.EnableWindow();
my_static.Invalidate();
}
And it works flawlessly! Remove Invalidate call and it would fail.
Try turning off Aero. I'm having a similiar problem where I'm drawing a static control and when it goes from disabled to enabled the WM_PAINT message is never received, but if I turn Aero off it works fine.