Thursday, October 12, 2006

List Control

List control provided by MFC is very useful. See here for simple usage of list control as report.

Create a dialog based application. Add list control to your dialog. Go to properties and ->Style->View->Select Report from Combo box.

Create a member variable for list control using class wizard available in Visual C++ 6.0.

lets say variable name m_List.

Creating column:
m_list.InsertColumn(0,"Heading",LVCFMT_LEFT,100);
m_list.InsertColumn(0,"Heading2",LVCFMT_LEFT,100);

Creating new item:
m_list. InsertItem(0,_T("c++"));
m_list.SetItemText(0,_T("Language"));

m_list. InsertItem(1,_T("vc++"));
m_list.SetItemText(0,_T("windows c++ compiler"));


List control can be used as a simple Table control

See my website for some intersting MFC projects:
http://mfc.neuralnetworks.in
http://neuralnetworks.in/windowsprogramming.html/

Saturday, September 16, 2006

Docking Bar on Dialog

Introduction: Normal Dialog doesn't support creating docking bars on it. This article explains how to create a docking bar on Dialog

Creating Docking bar: CFrameWnd is a MFC class supports docking bars. Creating Docking bars is easy with CFrameWnd. Generally CFrameWnd used to create SDI or MDI applications.
Follow These steps to create a Docking bar in dialog.

1) Create Dialog bases application using MFC App wizard
2) Derive a new class from CFrameWnd and declare a member variable for CDialogbar.
3) Create Dialog Bar on frame window's OnCreatec function.
4) Call enable docking functions CDialogBar and CFrameWnd
5) Create a member variable of type Frame in main Dialog class
6) On Dialog initialization create Frame with WS_CHILD style

More Details and source code can be found here: http://neuralnetworks.in/DockBarOnDialog.html

Friday, September 15, 2006

Customizing Windows Color Picker Dialog

Extending the color picker dialog is useful for so many applications.
MFC has class (CColorDialog) for color dialog.CColorDialog is derived from CDialog.Follow these steps to create a custom color picker dialog.

1. Create a new class and derive from CColorDialog
2. Overide the OnInitDialog and add your custom code like, Increasing the window size, adding new controls change the existing controls etc.
3. Override DoModal and OnOK for doing extra initialization.


See here more details and Sample Code:

http://neuralnetworks.in/CustomColorDlg.html

Wednesday, February 22, 2006

Change the Child Control Color in MFC

There is no SetTextColor function available for controls in MFC. If you want change the color of the Child control Pleasefollow these steps.It is aplicable to any CWnd derived Windows such asCFrameWnd,CDialog, CView,CComboBox etc.Using Class Wizard create this function.I am assuming we have CDialog Derived class

HBRUSH CDialog::OnCtlColor(CDC* pDC, CWnd* pWnd,UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
If you have a static text control IDC_STATIC_NAME.You can change the color by modifying the code as shown below.
HBRUSH CDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CMyDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_STATIC)
{
if(pWnd->GetDlgCtrlID() == IDC_STATIC_NAME)
{
pDC->SetTextColor(RGB(0,0,255)); //Blue Color
pDC->SetBkColor(RGB(255,0,0)); //Red Color
}
}
return hbr;
}
Above code will change the text color as Blue and back color as black. Same thing you can do for edit control using CTLCOLOR_EDIT

Saturday, February 04, 2006

MFC Application


Creating Simple MFC Application
without using wizards
I assume reader familiar with C++.
Follow these steps to create an simple

MFC application with a Frame Window.

Create a file with cpp extension.
Create a class which is derived from
CFrameWnd with Constructor and destructor


class CMyFrame
: public CFrameWnd
{
public:
CMyFrame()
{
}
virtual ~CMyFrame()
{
}

};


Create a class which derived from CWinApp and
override InitiInstance function we will create
Frame Window using the
CMyFrame.


class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance()
{
CMyFrame* myFrame = new CMyFrame();
m_pMainWnd = myFrame;
//Assign myFrame pointer to 'm_pMainWnd'
//which is member variable of CWinApp
myFrame->Create(NULL,"My Frame Window");
myFrame->ShowWindow(SW_SHOW);
myFrame->UpdateWindow();

return TRUE;

}

};



Last We will create CMyApp's instance


CMyApp myApp;

Include afxwin.h at top of
file which is MFC header file

#include

Put all code together

#include

class CMyFrame : public CFrameWnd
{
public:
CMyFrame()
{
}
virtual ~CMyFrame()
{
}

};

class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance()
{
CMyFrame* myFrame = new CMyFrame();
m_pMainWnd = myFrame;
//Assign myFrame pointer to 'm_pMainWnd'
//which is member variable of CWinApp
myFrame->Create(NULL,"My Frame Window");
myFrame->ShowWindow(SW_SHOW);
myFrame->UpdateWindow();

return TRUE;

}

};

CMyApp myApp;

Copy and paste this code to our cpp file and save.

Step1. Create an empty Win32 Application
and add this file to Project.
Step2. Enable MFC using project settings.
Step3. Build and Execute.

It will open Simple Frame window with System Menu.