Skip to main content

28 posts tagged with "MFC"

View All Tags

Single Document Interface Class Diagram

· One min read

A CSDIApp MFC application will have a single CWinApp class object. This holds a CDocManager object which is used by the MFC to handle all the CDocTemplate objects that we registered with the framework.

The CWinApp object also creates a CMainFrame object which is the main window of CSDIApp application.

Every time if we open/create a document in CSDIApp application, a CDocument object of the right type will be created. A pointer to CDocument object will be stored in a list under the corresponding CDocTemplate object.

Console Application Event Handling

· One min read

Using SetConsoleCtrlHandler we can Add an application-defined HandlerRoutine function say myConsoleHandler.

Note: If the second parameter is TRUE, the handler is added; if it is FALSE, the handler is removed by SetConsoleCtrlHandler function.

Sample code is Shown Below

#include "stdafx.h"
#include "windows.h"

void myConsoleHandler(DWORD ctrlEvent)
{
switch(ctrlEvent)
{
case CTRL_CLOSE_EVENT:
MessageBox(NULL,"Program being closed!","CTRL_CLOSE_EVENT",MB_OK);
break;

case CTRL_C_EVENT:
MessageBox(NULL,"Copy Event!","CTRL_C_EVENT",MB_OK);
break;
}

}

int main(int argc, _TCHAR* argv[])
{
SetConsoleTitle("Console Event Handler Demo");

if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)myConsoleHandler, TRUE))
{
printf( "nThe Control Handler is installed.n" );
printf( "n -- Now try pressing Ctrl+C or closing the console..." );
while(1)
{
//Do Nothing
}
}
else
{
printf( "nERROR: Could not set control handler");
return 1;
}
return 0;
}

Obtain a pointer to various objects in MFC

· One min read

The Table is very Handy when writing MFC SDI or MDI Applications

Note: To access only the current view, the document class

  • For SDI can call AfxGetMainWnd()->GetActiveView()

  • For MDI can call AfxGetMainWnd()->MDIGetActive()->GetActiveView()

MFC:Fill Background color of the Dialog

· One min read

MFC Tip: To fill the Background color of the Dialog and get the background of the controls to look correct

  1. Handle the ON_WM_ERASEBKGND Message and write the following code

     BOOL CTNV_MFCDialogDemoDlg::OnEraseBkgnd(CDC\* pDC)
    {
    CRect r;
    //GetClientRect gets the width & height of the client area of the Dialog
    GetClientRect(&r);
    CBrush br(RGB(0,255,0));
    pDC->SelectObject(br);
    pDC->FillRect(r,&br);

    //Make sure to return TRUE;
    //return CDialogEx::OnEraseBkgnd(pDC);
    return TRUE;
    }
  2. To  get the background of the controls to look correct Handle the ON_WM_CTLCOLOR Message and write the following code - Make sure to return the Brush Handle which was created same as a color of Dialog Background.

    HBRUSH CTNV_MFCDialogDemoDlg::OnCtlColor(CDC\* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    //HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
    //return hbr;


    //Make sure to return the Brush color should be same as Dialog Background color
    CBrush br(RGB(0,255,0));
    return (HBRUSH)br;
    }

Converting from CString

· 2 min read

This post demonstrates how to convert from a CString to the other string types

CString to LPTSTR

CString origCString = "HelloWorld";

// Get the internal buffer pointer of CString.
LPTSTR pString = origCString.GetBuffer(0);

//Release it.
origCString.ReleaseBuffer();

LPTSTR to CString

LPTSTR origLPTSTR = "HelloWorld";
CString sCString = CString(origLPTSTR);

CString to char*

CString origCString = "HelloWorld";

// Get the internal buffer pointer of CString.
char* pString = origCString.GetBuffer();

//Release it.
origCString.ReleaseBuffer();

char to* CString

char* origchar = "HelloWorld";
CString cstring = CString(origchar);

CString to CComBSTR

CString origCString = "HelloWorld";
CComBSTR ccombstr = CComBSTR(origCString);

CComBSTR to CString

CComBSTR origCComBSTR = "HelloWorld";
CString cstring = CString(origCComBSTR);

CString to _bstr_t

CString origCString = "HelloWorld";
_bstr_t bstrt = _bstr_t(origCString);

_bstr_t to CString

_bstr_t origbstrt  = "HelloWorld";
CString cstring = CString((char\*)origbstrt);

CString to BSTR

CString origCString = "HelloWorld";
BSTR bstr = origCString.AllocSysString();
::SysFreeString(bstr); //Free the string.

BSTR to CString

BSTR origbstr  = "HelloWorld";
CString cstring = CString(origbstr);

CString to CComVariant

CString origCString = "HelloWorld";
CComVariant comvariant = CComVariant(origCString);

CComVariant to CString

CComVariant origcomvariant  = "HelloWorld";
CString cstring = CString(origcomvariant);

CString to COleVariant

CString origCString = "HelloWorld";
COleVariant olevariant = CComVariant(origCString);

COleVariant to CString

COleVariant origolevariant  = "HelloWorld";
CString cstring = CString(origolevariant);

Change the name of the Debug Version of a DLL or Exe

· One min read

Its very useful to name the Debug version of the exe or DLL with letter "D" appended in-order to avoid confusion between release and debug version of binaries

Steps:

  1. Create a console application with name "MyProject"

  2. To change the output file name in debug configuration

place D as shown in below figure

Project Project Properties -> Linker -> General -> Output File

Release Version: MyProject Application Name: c:MyProjectReleaseMyProject.exe Debug Version: MyProject Application Name: c:MyProjectDebugMyProjectD.exe

MFC: Fill the Client area with a bitmap

· One min read

In MFC to fill the client area with a bitmap use the following code.

BOOL CMySDIView::OnEraseBkgnd(CDC\* pDC)
{
CDC mymem;
//Create a dc to represent display surface
mymem.CreateCompatibleDC(pDC);

CBitmap bmp;
bmp.LoadBitmap(IDB\_MYBITMAP);
mymem.SelectObject(&bmp);

//BitBlt Copy bitbamp from source (mymem) to destination (pDC)
pDC->BitBlt(0,0,740, 520, &mymem,0,0, SRCCOPY);

return TRUE; // CView::OnEraseBkgnd(pDC);
}

MFC: Fill Client area with a desired color

· One min read

MFC Tip: To fill the Background of the client area with a desired color keeping the text in the client area unchanged

BOOL CMySDIView::OnEraseBkgnd(CDC\* pDC)
{
CRect r;
//GetClientRect gets the width & height of the client area of
//the window
GetClientRect(&r);
CBrush br(RGB(255,250,100));
pDC->SelectObject(br);
pDC->FillRect(r,&br);

return TRUE;
}