Center an MFC Dialog
Use the CWnd::CenterWindow method to center the dialog. Write CenterWindow() method in the OnInitDialog() method.
BOOL CMyDialog::OnInitDialog() {
CenterWindow();
return TRUE;
}
Use the CWnd::CenterWindow method to center the dialog. Write CenterWindow() method in the OnInitDialog() method.
BOOL CMyDialog::OnInitDialog() {
CenterWindow();
return TRUE;
}
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.
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.
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);
}
}
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.
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.
To limit the maximum and minimum size of a Frame Window.
Define Min and Mix size in pixels
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);
}
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;
}
We need at least two classes derived to construct an window
#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->ShowWindow(SW_SHOWNORMAL);
m_pMainWnd->UpdateWindow();
return TRUE;
}
};
// The one and only Application Object
CMyApp theApp;
In order to remove MAXIMIZE BOX and MINIMIZE BOX from an MFC Window see the below code snippet.
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
cs.dwExStyle &= ~WS\_EX\_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
//////////////////////////////////////////////////////////
// To remove MAXIMIZE BOX from window //
//////////////////////////////////////////////////////////
cs.style &= ~WS\_MAXIMIZEBOX;
//////////////////////////////////////////////////////////
// To remove MINIMIZE BOX from window //
//////////////////////////////////////////////////////////
cs.style &= ~WS\_MINIMIZEBOX;
return TRUE;
}
To make first MDI child window start out maximized
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);
}
//////////////////////////////////////////////////////////////////////////// }