Showing posts with label MFC. Show all posts
Showing posts with label MFC. Show all posts

Tuesday, August 7, 2007

MFC Slider Control Message Handling

I worked with horizontal slider control in MFC.

in order to receive the slider control messages,
 
 we have to add the HSCROLL or VSCROLL message

I added the following message :

ON_WM_HSCROLL() added it within BEGIN_MESSAGE () macro.

next I added the fn like

afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) ;

while using Slider control, the SCROLL message is posted...

There are two main notification codes are available.

That are

          TB_THUMBTRACK

//Slider movement (the user dragged the slider)

          TB_THUMBPOSITION

//WM_LBUTTONUP following a TB_THUMBTRACK notification message

 

void MFCDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)

{

       CSliderCtrl * pControl = (CSliderCtrl*) pScrollBar);

        switch(nSBCode)

         {

             case TB_THUMBTRACK://Slider movement (the user dragged the slider)

              {

                break;

             }

               case TB_THUMBPOSITION:

                 {

                     //WM_LBUTTONUP following a TB_THUMBTRACK notification message

                      //StopTimer();

                        break;

                  }

         }

}

Friday, August 3, 2007

The real use of C++ class concept in MFC :

The real use of C++ class in MFC :

First windows developed the win32 programming model.

But it is so complicated.. It is being simplified by MFC. MFC is nothing but an object oriented framework around WIN32 API.

 

 

 

Take an Example :

 

GetWindowRect( LPRECT lpRect);

these two functions are APIs in windows.

LPRECT is a pointer to the RECT structure .

typedef RECT* LPRECT;

 

In MFC, there is a class CRect for handling rectangles.

in MFC code they may as follows :

CRect rc;

GetWindowRect(rc);

it will works well without any problem. Actually the GetWindowRect() API supports only LPRECT as argument...

Is it possible to develope our own Rectangle class in our application ?... yes...

 

How ?

it is the speciality of Operator conversion.

 

class CRect : public RECT

{

public :

operator LPRECT()

{

return this;

}

};

while executing the following line,

CRect rc;

GetWindowRect(rc);

rc Object is converted to the LPRECT, so the following code in a CRect class will be executed.

operator LPRECT()

{

return this;

}

So the pointer of the RECT derived class is passed as an argument to the GetWindowRect() fn.

 

if we are writing our own rectangle class

class CMyRect : public RECT

{

public:

operator LPRECT()

{

return this;

}

};

what is the purpose of providing wrapper class around RECT like structures in Windows ?

In order to simplify the operation, we are going for the wrapper class.

For Example,

RECT structure has following members

struct RECT

{

int left;

int top;

int bottom;

int right;

};

if we want to idenify the Width of the rectangle every time the user has to calculate it ...

Instead of it we develope the wrapper class method as follows that will be more easy...

LONG CRect :: Width()

{

return right - left;

}

Actually RECT structure is simple ... Windows has so much complicated structures to be passed as an argument to the

Windows API function. At that time, it will be more helpful...