msnkd's profilePoor programmer's BlogPhotosBlogListsMore Tools Help

Blog


    July 31

    RichEdit Common Mistake MFC Win32

     
    If you ever tried RichEdit control in Vc++ , may be at first use you will notice that your application may crash , or the dialog is not coming.  Smile The problem is , the RichEdit module is not loaded to memory initially. We need to load it explicitly. IF you are using MFC there are functions like
     
    AfxInitRichEdit2(); // RichEdit Version 2
    and
    AfxInitRichEdit();  // RichEdit version 1.
     
    If you are doing pure win32 , just by loading the richedit2.dll to meory will solve the problems , like
    LoadLibrary("c:\\WINDOWS\\system32\\riched20.dll"); // this is richedit version 2.
    also remember to unload the module with FreeLibrary function.
     
    You need to call the approrpiate function (AfxInitRichEdit or LoadLibrary) before creating richedit. The best way is to call is  in the InitInstance in MFC , or winmain Win32.
     
    July 29

    Model Dialog in Win32 c++

     
    Creating model dialog is very easy in MFC. It has neat class for dialog , CDialog. The DoModel functions of CDialog provides a way to appear model dialog.
     
    In Win32 this is another story. Many people doesn't know what is a model dialog. Some may thought it is a special type of dialog, like the different window classes(EDIT,BUTTON etc) . Actually model dialog is just a modeless dialog with some additions. Creating a model dialog requires to create modeless dialog ( using CreateDialog ,or even CreateWindow(wnd) wnd = dialog class name).
     
    Following is a sample class , which will show a modle dialog in win32, (Just like the CDialog::DoModal() in MFC).
     
    class name CBaseDlg, In this class i wrote a function DoModel, see below.
    The MyDlgProc is the dialog procedure function . In this procedure you can see that it calls the CBaseDialog::DialogProc function (blue color). So the derived class of CBaseDlg can override this function and add their command handlers.
     
    LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT nMsg,WPARAM wParam, LPARAM lParam)
    {
         CBaseDlg* pWnd = NULL;
         pWnd = CBaseDlg::FromHandle(hDlg);
         if(pWnd)
                return pWnd->DialogProc(hDlg,nMsg,wParam,lParam);
         return FALSE;
    }
     
    void
    CBaseDlg::DoModel(HINSTANCE hInst,HWND hParent,UINT nResourceID)
    {
         HWND hwnd = CreateDialog(hInst,(LPCTSTR)nResourceID, hParent, (DLGPROC)MyDlgProc);
         HwndMaps.insert(std::pair<HWND,CBaseDlg*>(hwnd,this) ); //Add this hwnd for using in MyDlgProc.
        
         ShowWindow(hwnd,SW_SHOWNORMAL); //show dialog.
         SendMessage(hwnd,WM_INITDIALOG,0,0); ///send INIT_DIALOG message as usual.
         EnableWindow(hParent,FALSE);//Disable parent window
     
         MSG msg;
         while(GetMessage(&msg,0,0,0))
         {
             TranslateMessage(&msg);
            DispatchMessage(&msg);
          }
     
          EnableWindow(hParent,TRUE); 
          DestroyWindow(hwnd); //Destroy me
          HwndMaps.erase(HwndMaps.begin(),HwndMaps.end()); //Clear the window map , this is just like the MFC way in win32.
    }
     
    Here in DoModel Passing the parent window handle and resource id , It will show a model dialog. The inside message loop will prevent the function from returning . The parent window should be disable , otherwise still user can do on parent window. So by disabling the window , the only active window is the dialog.
     
    Thats all for now.
    July 26

    Empty class size c++

    People may thinks like the size is zero for an empty class (Me also thought in the beginning). But it is not true. The size of an empty class will be never zero. It is nonzero. There are two reasons for that.

    1) the sizeof operator in c++ never returns zero.

    2) IF the size becomes zero it may cause to have some invalid address arithmetic operations.

    so the size of an empty class is never zero , it is always nonzero (and this value depends on compiler).

     

    July 19

    Connecting Two classes c++ : Some thoughts.

    How we can connect two classes may be many ways , useful and some complicated ones. I just thought about some useful ones and I am going to mentioning about that here( There are many other ways exists. like using pointers etc ).

    For this example I am creating two classes namely , Teacher and the other Student. I can connect Teacher and Student by following ways ( Also I know some complicated ways , thats next time).


    Direct connecting

    Look the following code definitions. It is just calling Student's function by Teacher.
    class Teacher
    {
    public :
    void TakeExam()
    {
    //Do some stuffs like PrepareDifficultQuestions() etc
    Student st;
    st.StartExam();
    }
    };

    class Student
    {
    public:
    void StartExam()
    {
    // Start copying from the other person ;)
    }
    };

    Using Virtual

    Here Teacher doesn't call StartExam , it handled by some other lecturers.

    class Teacher
    {
    public :
    virtual void TakeExam()
    {
    //Do some stuffs like PrepareDifficultQuestions() etc

    }
    };

    class Examiner : public Teacher
    {
    public :
    void TakeExam()
    {
    Teacher::TakeExam(); // prepare questions .
    Student st;
    st.StartExam();
    }
    }

    and
    The next one is using templates

    Here teacher is a template class , So here we can initialize Teacher like

    Teacher<Student> examiner;
    examiner.TakeExam() ;

    template <typename T>
    class Teacher
    {
    public :
    void TakeExam()
    {
    //Do some stuffs like PrepareDifficultQuestions() etc
    T st;
    st.StartExam();
    }
    };

    All these uses direct connection. Which connection need to use is depending on the design .
    Hopes it may give somebody some good !dea..
    July 06

    Placement New

    This is not a new subject. Many of us knows this. Many not knows.

    Placement new means we can ask the new operator to allocate from a particular memory area. This can be useful when implementing memory pools , to avoid fragmentation of memory.

    This is the way to do this.

    // Our mem pool of size 1000 bytes.
    unsigend char *pMemPool = new unsigned char [1000]; // 1000 bytes is a sample ,use large numbers here.

    Now i can ask new to allocate to pMemPool like this
    Someclass *p = new (pMemPool) new Someclass;

    When comparing the p and (Someclass *) pMemPool reveals they are equal. Don't free p, it will delete whole memory pool ( pMemPool). So allocate as much possible to this memory pool, later we can delete the whole pool . This helps to avoid memory fragmentation.