Skip to main content

How do I maximize my MDI child ?

· One min read

In CMainFrame class ad the following code.

void CMainFrame::ActivateFrame(int nCmdShow)
{
if (!m_bActivated)
{
m_bActivated = TRUE;
nCmdShow = SW_SHOWMAXIMIZED;
}

CFrameWnd::ActivateFrame(nCmdShow);
}

where m_bActivated is a member variable of your frame object.

Programmatically terminate MFC application

· One min read

MFC does not provide a public function to exit an application. The following method shutdowns the Application. The method simply sends a WM_CLOSE message to application's mainframe window.

void CmfcApp::ExitApplication()
{
if(AfxGetApp()->m_pMainWnd != NULL))
{
AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);
}
}

Reading Books

· One min read

Every book we read makes us grow taller. Reading helps in expanding the thinking of the mind. Most successful people from history to the present are having the habit of reading books.

The best investment that we can make is buying one or two books and reading them in a month every rupee you spent is worth. Reading can be a relaxing hobby as well.

How to read​

Use pencil or marker to highlight the important points Use Book Mark (Card with thread) Take notes on the margins best thing is use notebook to write points that you like most To get started with what books to read are below.

Limiting the maximum and minimum sizing of a Frame Window

· One min read

To limit the maximum and minimum size of a Frame Window.

  1. Define Min and Mix size in pixels

  2. Handle the WM_GETMINMAXINFO message in a CFrameWnd derived class.  The MINMAXINFO struct sets limits on the entire window, we need to take into account scroll bars, toolbars,  etc

 // Min and Mix size in pixels
# define MINX 500
# define MINY 300
# define MAXX 600
# define MAXY 400

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI) {
CRect rectWnd; CRect rectClient;
GetWindowRect(&rectWnd);
GetClientRect(&rectClient);

// get offset of toolbars, scrollbars, etc.
int nWidthOffset = rectWnd.Width() - rectClient.Width();
int nHeightOffset = rectWnd.Height() - rectClient.Height();

lpMMI->ptMinTrackSize.x = MINX + nWidthOffset;
lpMMI->ptMinTrackSize.y = MINY + nHeightOffset;
lpMMI->ptMaxTrackSize.x = MAXX + nWidthOffset;
lpMMI->ptMaxTrackSize.y = MAXY + nHeightOffset;

CFrameWnd::OnGetMinMaxInfo(lpMMI);

}

Centering an Application Frame Window to desktop

· One min read

To center Application's Frame Window call CenterWindow() in MainFrame's OnCreate() function.

See the code snippet below

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;

//////////////////////////////////////////////////////////
// Center Application Frame Window //
/////////////////////////////////////////////////////////
CWnd* pWnd = GetDesktopWindow();
CenterWindow(pWnd);
//////////////////////////////////////////////////////////

return 0;
}

How to create a minimal MFC Window ?

· One min read

We need at least two classes derived to construct an window

  1. Window class / Frame class (CMainFrame) which is derived from CFrameWnd
  2. Application class (CMyApp) which is derived from CWinApp
#include <afxwin.h>;

class CMainFrame : public CFrameWnd
{
public:
CMainFrame()
{
Create(NULL, "HELLO MFC");
}
protected:
afx_msg void OnPaint()
{
CPaintDC dc(this);
dc.TextOut(100, 100, "This is the my MFC window");
}

afx_msg void OnLButtonDown(UINT nFlags, CPoint point)
{
AfxMessageBox("Mouse OnLButtonDown !!");
CFrameWnd::OnLButtonDown(nFlags, point);
}
// Declare the Message Map
DECLARE_MESSAGE_MAP()
};

// Define the Message Map out side the class Definition
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_PAINT() // Paint Message
ON_WM_LBUTTONDOWN() // Mouse LButton down message
END_MESSAGE_MAP()

class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance()
{
m_pMainWnd = new CMainFrame();
m_pMainWnd-&gt;ShowWindow(SW_SHOWNORMAL);
m_pMainWnd-&gt;UpdateWindow();
return TRUE;
}
};

// The one and only Application Object
CMyApp theApp;

Remove MAXIMIZE BOX and MINIMIZE BOX from an MFC Window

· One min read

In order to remove MAXIMIZE BOX and MINIMIZE BOX from an MFC Window see the below code snippet.

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT&amp; cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;

cs.dwExStyle &amp;= ~WS\_EX\_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);

//////////////////////////////////////////////////////////
// To remove MAXIMIZE BOX from window //
//////////////////////////////////////////////////////////
cs.style &amp;= ~WS\_MAXIMIZEBOX;

//////////////////////////////////////////////////////////
// To remove MINIMIZE BOX from window //
//////////////////////////////////////////////////////////
cs.style &amp;= ~WS\_MINIMIZEBOX;

return TRUE;
}

Making first MDI child window start out maximized

· One min read

To make first MDI child window start out maximized

  1. Add a ActivateFrame() which is a CChildFrame virtual function
  2. Modify the function with the below code snippet
void CChildFrame::ActivateFrame(int nCmdShow) { 
////////////////////////////////////////////////////////////////////////////
// To make first MDI child window start out maximized
// ////////////////////////////////////////////////////////////////////////////
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

// if another window is open, use default
if(GetMDIFrame()->MDIGetActive()) {
CMDIChildWnd::ActivateFrame(nCmdShow);
} else // else open the child window maximized.
{
CMDIChildWnd::ActivateFrame(SW_SHOWMAXIMIZED);
}

//////////////////////////////////////////////////////////////////////////// }

Check whether a given file exists in the given path

· One min read

Using Win32 API FindFirstFile we can find whether a given file exists in the given path (szFilePath) or not. BOOL IsFileExists( LPCTSTR szFilePath) method shown below serves our purpose.

BOOL IsFileExists( LPCTSTR szFilePath) // szFilePath in | file path 
{
BOOL bFileExists;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(szFilePath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
bFileExists = FALSE;
} else {
FindClose(hFind);
bFileExists = TRUE;
}

return bFileExists;
}

Hash Data using Win32 API through a CHashDataProvider class

· 3 min read

Cryptography is the use of codes to convert data so that only a specific recipient will be able to read it. Microsoft provide win32 API to Hash Data/string. The class CHashDataProvider developed to provides simple function HashData(ALG_ID algorithmID, LPCTSTR plainText, LPTSTR hashedText); which takes algorithmID and inputText to convert plainText to hashedText

ALG_ID algorithmID - Algorithm ID to be used LPCTSTR plainText - plainText that to be converted to hashedText LPTSTR hashedText - Hashed Test using the input algorithmID.

CHashDataProvider Code

HashDataProvider.h

#include <wincrypt.h>;
#define BUFFER_SIZE 256

class CHashDataProvider
{
public:
CHashDataProvider(void);
~CHashDataProvider(void);

private:
// Handle to a cryptographic service provider (CSP)
HCRYPTPROV m_hProv;

// Handle to the hash object needed to create a hash.
HCRYPTHASH m_hHash;

// Pointer to the hash.
PBYTE m_pbHash;

private:
void FreeResources();
void InitializeData();
LPTSTR FormatData(PBYTE pbHash, DWORD dwDataLen, LPTSTR hashedText);

public:
bool HashData(ALG_ID algorithmID, LPCTSTR plainText, LPTSTR hashedText);
};

HashDataProvider.cpp

#include "HashDataProvider.h"

CHashDataProvider::CHashDataProvider(void)
{
InitializeData();
}

CHashDataProvider::~CHashDataProvider(void)
{
}

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
// des: Hashes data based on the ALG_ID
// return: Hashing of the text is successful or not
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
bool
CHashDataProvider::HashData(ALG_ID algorithmID, // in | Algorithm ID (Sha1 or MD5 )
LPCTSTR plainText, // in | Original string to be hashed
LPTSTR hashedText) //out | string to store hashed text
{
InitializeData();

bool rc = true; // Default is success
DWORD dwDataLen = 0; // Length, in bytes, of the hash.

//--------------------------------------------------------------------
// Acquire a handle to the default RSA cryptographic service provider.
if (!CryptAcquireContext(
&amp;m_hProv, // handle of the CSP
NULL, // key container name
NULL, // CSP name
PROV_RSA_FULL, // provider type
CRYPT_VERIFYCONTEXT)) // no key access is requested
{
AfxMessageBox("Error in AcquireContext 0x%08x n", GetLastError());
rc = false;
FreeResources();
}

if (!CryptCreateHash(
m_hProv, // handle of the CSP
algorithmID, // hash algorithm to use
0, // hash key
0, // reserved
&amp;m_hHash)) // address of hash object handle
{
AfxMessageBox("Error in CryptCreateHash 0x%08x n", GetLastError());
rc = false;
FreeResources();
}

if (!CryptHashData(
m_hHash, // handle of the hash object
(const BYTE *) plainText, // text to be hash
_tcslen(plainText)*sizeof(TCHAR), // number of bytes of data
0)) // flags
{
AfxMessageBox("Error in CryptHashData 0x%08x n", GetLastError());
rc = false;
FreeResources();
}

if (!CryptGetHashParam(
m_hHash, // handle of the HMAC hash object
HP_HASHVAL, // query on the hash value
NULL, // pointer to the HMAC hash value
&amp;dwDataLen, // length,in bytes, of the hash
0))
{
AfxMessageBox("Error in CryptGetHashParam 0x%08x n", GetLastError());
rc = false;
FreeResources();
}

m_pbHash = (BYTE\*)malloc(dwDataLen);
if(NULL == m_pbHash)
{
AfxMessageBox("unable to allocate memoryn");
rc = false;
FreeResources();
}

if (!CryptGetHashParam(
m_hHash, // handle of the HMAC hash object
HP_HASHVAL, // query on the hash value
m_pbHash, // pointer to the HMAC hash value
&amp;dwDataLen, // length,in bytes, of the hash
0))
{
AfxMessageBox("Error in CryptGetHashParam 0x%08x n", GetLastError());
rc = false;
FreeResources();
}

hashedText = FormatData(m_pbHash, dwDataLen, hashedText);

FreeResources();

return rc;
}

//Helper functions
void CHashDataProvider::FreeResources()
{
if(m_hHash)
CryptDestroyHash(m_hHash);
if(m_hProv)
CryptReleaseContext(m_hProv, 0);
if(m_pbHash)
free(m_pbHash);
}

void CHashDataProvider::InitializeData()
{
m_hProv = NULL;
m_hHash = NULL;
m_pbHash = NULL;
}

LPTSTR CHashDataProvider::FormatData(PBYTE pbHash, DWORD dwDataLen, LPTSTR hashedText)
{
TCHAR rstData[BUFFER_SIZE]= {0}; // Buffer to receive hashed result
TCHAR tmpBuffer[3] = {0};
for (DWORD i = 0 ; i &lt; dwDataLen ; i++)
{
tmpBuffer[0] = 0;
tmpBuffer[1] = 0;
tmpBuffer[2] = 0;
_stprintf_s(tmpBuffer, _T("%2.2x"), pbHash\[i\]);
_tcscat_s(rstData, tmpBuffer);
}
_tcscpy(hashedText, rstData);

return hashedText;
}