Particle Editor in MFC

Started by
1 comment, last by dewhacker 16 years, 9 months ago
Ok, so I'm completely new to MFC and I'm trying to implement a particle editor in an application. My viewing window is in the FormView dialog, and so are all of my sliders to control selected attributes. I have CSliderCtrl's for each of them but I want to set the range and position for them when the application starts up because I already have a default emitter loaded in and rendering. I can't seem to find an event to initialize these values in, can anyone offer any advise, I may be doing this completely wrong also, I'm a total n00b @ MFC. Thanks for your time
Advertisement
You have a class whose instance represents window on which those sliders reside. It can be a CDialog or CFormView or whatever. Within resource editor you place a slider or two here and there and add in that class variables of type CSliderCtrl for each of yours sliders. For example, if you decide to house sliders in your CFormView derived class:

class CMyFormView : public CFormView
{
CSliderCtrl m_slider1;
CSliderCtrl m_slider2;


Next you need to connect those slider instances with onscreen sliders. This is done in DoDataExchange function. So with the above CMyFormView, you would have something like this:

void CMyFormView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER1, m_slider1);
DDX_Control(pDX, IDC_SLIDER2, m_slider2);
}

My assumption here is that your onscreen sliders have IDs named IDC_SLIDER1 and IDC_SLIDER2.

Now that connection is established, you can set slider default values. The function where this can be done is different for various types of classes which hold those slider instances. For CViewForm derived classes you could use OnInitialUpdate, for CDialog derived classes the usual place is OnInitDialog and so on.

void CMyFormView::OnInitialUpdate()
{
// these three come from MFC appwizard so just leave them as they are
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();

// do your own initialization
m_slider1.SetPos(0);
m_slider2.SetPos(100);
}


By default, MFC sliders are in the range from 0 to 100 but you can setup whatever range you like. Take a look at MSDN docs for CSliderCtrl.

And then your would be probably interested in reading slider values when user slides them. What I do is to add handlers for slider NM_CUSTOMDRAW message. So, again with the above example, in your CFormView derived class you add functions:

class CMyFormView : public CFormView
{
void OnSlider1(NMHDR *pNMHDR, LRESULT *pResult);
void OnSlider2(NMHDR *pNMHDR, LRESULT *pResult);

Note that the exact function signature depends on the type of message it handles. These are for slider NM_CUSTOMDRAW, other message types may require different signatures. Use MSDN for further info. Next, you need to connect these handlers with its message:

BEGIN_MESSAGE_MAP(CMyFormView, CFormView)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnSlider1)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnSlider2)
END_MESSAGE_MAP()

There... On NM_CUSTOMDRAW coming from IDC_SLIDER1, MFC should invoke your OnSlider1 function. The only thing left is to define those handlers:

void CMyFormView::OnSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
// MFC likes to put these but they are not necessary
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;

// update controls with new values
UpdateData( TRUE );

// current slider value
int pos = m_slider1.GetPos();

// do whatever you want with 'pos'
}


BTW, most of these things can be done without any typing, with just a few mouse clicks here and there in MSVS resource editor and it does all this setup for you. For some reasons I made a bad habit of typing everything manually (that damn Linux and its "visual" IDEs, see now what they have done to me) so really can't remember the "visual" way of doing it. Sorry.

Oh, and in the future, for any MFC questions you should look at CodeProject website. They have a humongous section devoted to MFC.

Thanks so much for the help! Fixed my problem!

This topic is closed to new replies.

Advertisement