VisualCpp.org Collection of Visual C++ Stuffs

18May/07Off

How to subclass MFC AfxMessageBox?

Answer hidden in the MFC framework itself. AfxMessageBox internally call CWinAPP::DoMessageBox function. So for overriding or providing your own custom message box in place of AfxMessageBox just handle above mentioned function.

int CTestApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
// TODO: Add your specialized code here and/or call the base class

return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}

here CTestApp is class derived from CWinAPP !

Filed under: MFC Comments Off
18May/07Off

How to add taskbar button in modeless windows?

Yesterday I have included a Model less dialog in my project, for some online data display. After initial Unit Testing , I required Task bar button for my model less dialog because when executed because it always go back in background, whenever i click on my main application window and i can't set always on top style as it can create problem. only solution i have to provide it with Taskbar button so that by clicking on Taskbar button, it get focus. for achieving it, you just have to set Desktop Window as parent of model less window. here CWnd::GetDesktopWindow() api come very handy!

CWnd *pDeskWnd= GetDesktopWindow();
CMyModellessDialog *pMyModellessDialog = new CMyModellessDialog ();
pMyModellessDialog ->Create(IDD_DIALOG_MODELLESS,pDeskWnd);
pMyModellessDialog ->ShowWindow(SW_SHOW);

PPS:- Don't forget to delete the pMyModellessDialog pointer afterward!

17May/07Off

How to create Dialog which is initially inactive?

Many situation come when you required your MFC dialog created initially in active or don't take keyboard focus from other window..humm don't get worryi will tll you the solution.... and here is solution :) , you just have to return FALSE instead of true from OnInitDialog function to create Dialog initially inactive..

BOOL CMyDialog::OnInitDialog()
{
return FALSE;
}

10May/07Off

How to get time elasped between any two instruction?

GetTickCount is the api/function, which could help you finding approximate time difference between two call or time taken by any function to execute or complete it's processing!

code example

DWORD dwTime,dwElaspedTime;
dwTime=GetTickCount();
//.....................
//Do some task for example like funtion call or lengthy operation
//.....................

dwElaspedTime=(GetTickCount()-dwTime)/1000;
// for getting result in second

CString strTimetaken;
strTimetaken.Format("Total Time Elasped %u in second",dwElaspedTime);

//display Total Time taken by Task
MessageBox(strTimetaken);

Filed under: General Comments Off
10May/07Off

How to dynamically load function from any DLL?

For Dynamically loading Function from the DLL you have to take services of two api :- LoadLibrary and GetProcAddress

Here A Small code that will demonstrate dynamically loading of function from DLL.here,
I will load mciSendString function defined in WINMM.DLL .

// first make Function pointer
typedef MCIERROR (WINAPI * MCISENDSTRING)(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);

/// In function where you want to use about api
MCISENDSTRING fnmciSendString=NULL;
HMODULE hLibrary;

// load the library
hLibrary=LoadLibrary(_T("winmm.dll"));

// check is library loaded
if(hLibrary)
{
// if yes try to get Function addressfnmciSendString=(MCISENDSTRING)::GetProcAddress(_T("mciSendString"));
}

// check is we got Function Pointer
if(fnmciSendString)
{
// if yes call function
(fnmciSendString)(......);
}

Filed under: DLL, General, Win32 Comments Off
6May/07Off

How to iterate through the subkeys in the registry location?

To enumerate the subkey of any given key you have RegEnumKey and RegEnumKeyEx function, the given use ATL::CRegKey which encapsulate basic registry window api.

This code that will enumerate all the key under HKEY_CURRENT_USER\Software, m_enumStrArray contain all enumerated keys

CRegKey m_hRegKey;
CStringArray m_enumStrArray;

if( m_hRegKey.Open(HKEY_CURRENT_USER,"Software")==ERROR_SUCCESS)
{
     DWORD dwSize=MAX_PATH,dwIndex=0;
     TCHAR tcsStringName[MAX_PATH];

    while(RegEnumKey( m_hRegKey.m_hKey,dwIndex,tcsStringName,dwSize)==ERROR_SUCCESS)
     {
       //sub key name
         m_enumStrArray.Add(tcsStringName);
         dwIndex++;
        dwSize=MAX_PATH;
      }
}

6May/07Off

What is last message posted when dialog is destroyed?

WM_NCDESTROY  message is posted last when the dialog is destroyed, this very handy message when come modeless dialog box, you can set notification from this  message to notify main window that your modeless dialog box is destroyed!

e.g.   let   CMainWnd is your parent window and CModlessDlg is  dialog box, m_IsModDlgOpen boolean variable declared in CMainWnd

DECLARE_MESSAGE_MAP(CMainWnd,CDialog)
.......
ON_MESSAGE(UWM_MODLESSDESTROYED,OnModLessDestroyed)
END_MESSAGE_MAP()

void CMainWnd::CreateModelessDlg()
{
if(m_IsModDlgOpen)
{
MessageBox("Modeless Dlg already open");
return;
}

CModlessDlg *pDlg = new CModlessDlg (IDC_DLGTEMPLATE,NULL);
pDlg->ShowWindow(SW_SHOW);
m_IsModDlgOpen = true;
}

LRESULT CMainWnd::OnModLessDestroyed(WPARAM wParam,LPARAM lParam)
{
m_IsModDlgOpen = true;
return 0;
}

// CModlessDlg class

void CModlessDlg ::OnNCDestory()
{
CDialog::OnNCDestory();
:: PostMessage(NULL,UWM_MODLESSDESTROYED,0,0)
delete this;
}

In brief when ever the modeless dialog get destoyed it post message to main window regarding it closure and and delete this will delete the memory associated with itself

Filed under: Dialog Based, MFC Comments Off