Skip to main content

Center an MFC Dialog

· One min read

Use the CWnd::CenterWindow method to center the dialog. Write CenterWindow() method in the OnInitDialog() method.

BOOL CMyDialog::OnInitDialog() { 
CenterWindow();
return TRUE;
}

Creating a CDC from a HDC

· One min read

If we get handle to a DC and sometimes we might want to create a CDC from that. One example is owner-drawn lists, combos, and buttons. we will receive a draw item message with a hDC. The below code can be used to convert hdc into CDC.

 void MyList::DrawItem(LPDRAWITEMSTRUCT lpDrawItem) { 
CDC* pDC;
pDC = CDC:FromHandle(lpDrawItem->hDC);
}

NOTE: This technique for any of the other MFC class/ Windows handle pairs too.

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);
}

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