I overrided WM_CLOSE, how to came out from application now - mfc

I am a new bie to windows programming, I written an applicatoin with single dialog. In that I override the CWnd::OnClose to do some stuff before exiting from application.after that I need to get out from application. But It will also be called if I post or send a message with WM_CLOSE event from overridden method, so how to get out from application now.
class MyDlg : public CDialogEx
{
public:
afx_msg void OnClose();
};
BEGIN_MESSAGE_MAP(MyDlg , CDialogEx)
ON_WM_CLOSE ()
END_MESSAGE_MAP()
void MyDlg:: OnClose()
{
//what code I should write here to exit from application.
}

after you do your stuff write this:
__super::OnClose();

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.

MFC TVN_ITEMEXPANDING doesn't call the handler function

I have a CTreeCtrl and I like to use its TVN_ITEMEXPANDING message, but the handler function never calls.
CsetkliensDlg.h
afx_msg void OnItemExpanding(NMHDR* pNmhdr,LRESULT *lResult);
CsetkliensDlg.cpp
BEGIN_MESSAGE_MAP(CCsetkliensDlg, CDialogEx)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, &CCsetkliensDlg::OnItemExpanding)
END_MESSAGE_MAP()
...
void CCsetkliensDlg::OnItemExpanding(NMHDR* pNmhdr,LRESULT *lResult)
{
AfxMessageBox("almafa");
}
The items have childs.
You are trying to catch a notification in the parent dialog so you should use ON_NOTIFY instead of ON_NOTIFY_REFLECT.
Of course then your message map will be something lik:
ON_NOTIFY( TVN_ITEMEXPANDING, CTREECTRL_RESOURCES_ID, ONHandlerFunction)
You can use the reflection mechanism but then the handler should be in a CTreeCtrl derived class.

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.

C++ How to make a GUI in directx?

I have few questions on this Subjects.
I created a class for buttons, the class has problem.
Problem is:
1. I wanna create function to be called if button is clicked, the problem is that every single button gonna do different thing if it was clicked. So i don't know how i can create function that will do different thing for every button.
I have no idea how i should design my interface.
If you can give me an idea on how i should design my GUI that would be great.
This is my button class
class GUIButtons
{
public:
GUIButtons(void);
~GUIButtons(void);
void LoadMesh(string fileName, int startAnimation, LPDIRECT3DDEVICE9 d3ddev);
void Render(float timeElapsed, D3DXMATRIX *matWorld);
void EventProc(HWND hWnd, UINT msg, LPDIRECT3DDEVICE9 d3ddev);
void Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev);
private:
CXFileEntity *Button;
};
EDIT 2:
Guys is this possible?
I create two functions and than ill point one function to another.
Something like this
void a()
{
....
}
void b() = a;
EDIT 3:
Ok should i use this way for the onClick() function.
void Onclick( void(*fun) )
{
fun();
}
i pass a function to OnClick than it calls the function.
should i use this way?
Use inheritance/polymorphism: The base class is GUIButtons, and every new individual button derives from that base clase:
class MyButton : public GUIButtons { ... }
And then the functionality for a click comes in a virtual method onClick() or whatever.
More detail:
class GUIButtons
{
... \\ lots of stuff
virtual void onClick() { };
};
class CloseButton : public GUIButtons
{
...
virtual void onClick()
{
//code to close window
}
};
class SettingsButton : public GUIButtons
{
...
virtual void onClick()
{
//stuff to open settings menu
}
};
Button click is an action/event. You can encapsulate the click action as a class. This action is interpreted and used differently by the application that uses the button widget. The gui library has to notify that a button has been clicked with the relevant information. You can refer to popular GUI libraries like Qt or Gtk+ as examples to know how they implement GUI events and much more.
I recommend you the book "DirectX 9 User Interfaces: Design and Implementation". I have it and i may say that you'll find there everything what you need!!!;)

Catching when user selects an item from a CComboBox

This is as basic as it gets.
I want to catch when the user selects an item from a CComboBox (actually, a subclass of CComboBox).
Tried lots of combinations of OnCblSelChange, OnCommand. Guess I haven't hit the right combo yet (no pun intended).
OS is Vista but I'm forcing an XP-style dialog (That shouldn't matter, should it?)
I'm able to catch events for classes derived from CEdit and CFileDialog.
I am at my wits end here. Any assistance would be ever-so appreciated.
Any source code would, of course, be more than ever-so appreciated.
Unfortunately, it seems that all messages (even SELEND_OK) for combo box changing are sent before the text has actually changed, so DoDataExchange will give you the previous text in the CComboBox. I have used the following method, as suggested by MSDN:
void MyDialog::DoDataExchange(CDataExchange* pDX)
{
DDX_Text(pDX, IDC_COMBO_LOCATION, m_sLocation);
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(MyDialog, CDialog)
ON_CBN_SELENDOK(IDC_COMBO1, &MyDialog::OnComboChanged)
ON_CBN_EDITUPDATE(IDC_COMBO1, &MyDialog::OnComboEdited) // This one updates immediately
END_MESSAGE_MAP()
...
void MyDialog::OnComboChanged()
{
m_myCombo.GetLBText(m_myCombo.GetCurSel(), m_sSomeString);
}
void MyDialog::OnComboEdited()
{
UpdateData();
}
It seems to work quite nicely.
CBN_SELENDOK should be the message that you're looking for. It's sent after the user selection is finalized but before the combo box closes up (if it does). CBN_SELCHANGE is sent before the selection is actually saved to the combo box control.
This MSDN link has more information (you've likely seen it already...)
Here is the code I promised you. One thing I noticed when gathering this up is that it is possible to have this message suppressed if you are using an ON_CONTROL_REFLECT handler within the class derived from CComboBox. This would cause the control itself to handle the message and not pass it on to the parent. You can get around that problem by using ON_CONTROL_REFLECT_EX with the proper return code, which will make both the box itself and the parent receive the message.
Anyway, here's the code snippet:
class SPC_DOCK_CLASS ProcessingExceptionDockDlg : public CSPCDockDialog
{
SPC_DOCK_DECLARE_SERIAL(ProcessingExceptionDockDlg);
public:
// ... redacted ...
//{{AFX_DATA(ProcessingExceptionDockDlg)
CComboBox m_comboFilter;
//}}AFX_DATA
//{{AFX_VIRTUAL(ProcessingExceptionDockDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);
//}}AFX_VIRTUAL
protected:
//{{AFX_MSG(ProcessingExceptionDockDlg)
afx_msg void OnSelendokComboTreeFilter();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/****************/
// ProcessingExceptionDockDlg.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "ProcessingExceptionDockDlg.h"
// ... much code redacted ...
void ProcessingExceptionDockDlg::DoDataExchange(CDataExchange* pDX)
{
CSPCDockDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(ProcessingExceptionDockDlg)
DDX_Control(pDX, IDC_COMBO_TREE_FILTER, m_comboFilter);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(ProcessingExceptionDockDlg, CSPCDockDialog)
//{{AFX_MSG_MAP(ProcessingExceptionDockDlg)
ON_CBN_SELENDOK(IDC_COMBO_TREE_FILTER, OnSelendokComboTreeFilter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void ProcessingExceptionDockDlg::OnSelendokComboTreeFilter()
{
// ... code redacted ...
}