Home   Notes   Contact Me

MFC

Local

External


Command line parsing

Handle command line arguments in a class derived from CCommandLineInfo. Example:

Make sure that InitInstance has lines like this in it:


Dialog, Resizing

Resizing Controls


ActiveX, embedding in SDI window

Junky Notes...

NON doc / view version

Doc / View version


Window, Disabling Clear


Window, desktop


// getting the window
CWnd *pCwnd = CWnd::GetDesktopWindow();

// getting a drawing context to draw to it
CWindowDC wdc( CWnd::GetDesktopWindow());

Timer

Associated with a window

Add a WM_TIMER event handler


class XxxDlg {
…
UINT m_nTimerId;
UINT m_nEventId;

static void CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

void OnStart();
void OnStop();
…
};

void XxxDlg::OnStart() 
{
	m_nEventId = 1;
	m_nTimerId = SetTimer(m_nEventId, m_nElapse, NULL);
}

void XxxDlg::OnStop() 
{
	if (m_nTimerId != 0) {
		// EventId is used instead of the TimerId 
		KillTimer(m_nEventId);
		m_nTimerId = 0;
	}
}

// Following is an IDE generated method:
void XxxDlg::OnTimer(UINT nIDEvent)
{
	CFrameWnd::OnTimer(nIDEvent);
	
	// do whatever here
}

Not assocated with a window


class XxxDlg {
…
UINT m_nTimerId;
UINT m_nEventId;

static void CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);

void OnStart();
void OnStop();
…
};

void CALLBACK XxxDlg::MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{   
  // Handle your timer here (timerID in idEvent)}

  // Do specific task
  Beep(3000, 100); 
}

void XxxDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	UINT nElapse; // Specifies the time-out value, in milliseconds
	
	nElapse = 500; 
	
	// EventId (2nd parameter) is ignored.
	m_nTimerId = ::SetTimer(NULL, 0, nElapse, MyTimerProc);
}

void XxxDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	
	if (m_nTimerId != 0) {
		::KillTimer(NULL, m_nTimerId);
		m_nTimerId = 0;
	}	
}

Window, setting the Initial Size


BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
{ 
    // Create a window without min/max buttons or sizable border 
    cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;

    // Size the window to 1/3 screen size and center it 
    cs.cy = ::GetSystemMetrics(SM_CYSCREEN) / 3; 
    cs.cx = ::GetSystemMetrics(SM_CXSCREEN) / 3; 
    cs.y = ((cs.cy * 3) - cs.cy) / 2; 
    cs.x = ((cs.cx * 3) - cs.cx) / 2;

    // Call the base-class version
    return CFrameWnd::PreCreateWindow(cs); 
}

Replacing The Default Icon

http://www.codeproject.com/gdi/replaceicon.asp#xx378xx

How to Set the Window Title


AfxGetApp()->GetMainWnd()->SetWindowText( "New Window Title");

Status Bar


// In CMainFrm
this->m_wndStatusBar.SetWindowText( "Show Me");

// From elsewhere
// add this to CMainFrm
void CMainFrm::SetStatusText( LPCTSTR str) { m_wndStatusBar.SetWindowText( str); }
// then use this to set it:
CMainFrame* pFrame =(CMainFrame*) AfxGetApp()->m_pMainWnd;
pFrame->SetStatusText( "Show Me Too");

SubWindow in an SDI MFC App

  1. Create an MFC sdi application
  2. Add an MFC class with base class CWnd
  3. Add an instance of your new class to ChildView.h
  4. To ChildView::OnCreate add this:
    	CRect rect( 0, 0, 10, 10);
    	if ( !m_wndGlx.Create( NULL, NULL, AFX_WS_DEFAULT_VIEW, rect, this, AFX_IDW_PANE_FIRST, NULL)) {
    		int y = 0;
    	}	
    
  5. To ChildView::OnSize add a call to {new window}.MoveWindow( 0, 0, cx, cy);
  6. Do whatever you want in the new windows OnPaint
  7. If you want to get Commands to your new window (like Menu Selections), Look at how the MainFrames OnCmdMsg handler chains to its ChildView and add a OnCmdMsg handler to the child view and make it chain to the sub window you added

Getting a HWND

From CDialog


dialog.GetSafeHWD();

From A Resource Identifier


HWND hWnd = GetDlgItem(IDC_CUSTOM1)->GetSafeHwnd();

Console


AllocConsole();
GetStdHandle( STD_OUTPUT_HANDLE);
WriteConsole(