Click here to Skip to main content
12,297,982 members (50,479 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


92 bookmarked

Address Book

, 6 Aug 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
Address Book application

Sample Image


Address Book demonstrates the use of GfxListCtrl control, CHyperlink class, CSystemTray class in one useful application. It also demonstrates dropdown tool button and well as using the template class CArray.

Even though a lot of programs at CodeProject come with samples, beginners find it difficult to learn how to actually use them in real application because the demos usually stick to just displaying the capabilities of that control. I decided to write a program which will not just demonstrate the functionality of the control but also how to make it work for you in your programs.

I will be focusing on the GfxListCtrl because my program is based around it. GfxListCtrl is written by Iuri Apollonio. CHyperLink and CSystemTray are written by Chris Maunder.

How do I use the Text callback function ?

Well, first of all I create a class called CPerson. This class holds all the details about the person and the Serialize function is overloaded to save and load the details.

An array is created to hold the CPerson objects as in CAddressDoc as

CArray<CPerson,CPerson&> m_PersonArray;

This makes it easy to use the TextCallBack function. Your TextCallBack function for the GfxListCtrl is declared as

void GetTextCallback(int iIndex, int iSubItem, long lCode, CString &cs)

where lCode gives which item in the array is required. iIndex and iSubItem gives you the row and column respectively on the screen. To get the actual column you will have to use GetColumnIndex() member function of the GfxListControl, which means in your view class you would write something like this

void CAddressView::GetTextCallback(int iIndex, 
   int iSubItem, long lCode, CString &cs)
   int rc = wndList.GetColumnIndex(
     iSubItem); // wndList is the GfxListControl
   CPerson person;
   cs = "";
   if (GetDocument()->m_PersonArray.GetSize() == 0 || 
   GetDocument()->m_PersonArray.GetSize() < (iIndex + 1)) {
   person = GetDocument()->m_PersonArray[lCode - 1 ];
   switch (rc) {
     case 0: cs.Format("%d", iIndex + 1 ); break;
     case ADDR_FIRST_NAME: cs = person.m_strFirstName;  break;
     case ADDR_LAST_NAME:  cs = person.m_strLastName;   break;
     case ADDR_MIDDLE_NAME:cs = person.m_strMiddleName; break;
     case ADDR_NAME:       cs = person.m_strName;       break;
     case ADDR_NICK_NAME:  cs = person.m_strNickName;   break;
     case ADDR_EMAIL:      cs = person.m_strEMail;      break;
     default:  cs.Format("%d, %d", lCode, rc);

For the AutoPreview alone you have to draw the text you want on to the screen in the function

long CAddressView::GetExInfoCallback(LXHDREX * pLx)


      LXHDREX_DIV * pLxEx = (LXHDREX_DIV *) pLx;
      COLORREF ocr = pLxEx->pDC->SetTextColor(RGB(0,0,255));
             GetDocument()->m_PersonArray[pLx->dwItemData - 1].m_strNotes , 
             pLxEx->rcItem, DT_END_ELLIPSIS|DT_WORDBREAK);
      return 1;

where m_PersonArray[Index].strNotes is the text I want it to display.

How to solve the double click problem of GfxListCtrl when the tooltip is displayed

To solve the double click problem just make the following changes to the class CGfxListTip.

Add the style CS_DBLCLKS to the WNDCLASS as

   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();
   if(!(::GetClassInfo(hInst, GFXLISTTIP_CLASSNAME, &wndcls)))
   { = CS_DBLCLKS | CS_SAVEBITS ; // Xavier added CS_DBLCLKS 
     wndcls.lpfnWndProc = AfxWndProc; // Xavier changed from ::DefWindowProc
     wndcls.cbClsExtra = wndcls.cbWndExtra = 0;

and add the line


to the function

BOOL CGfxListTip::PreTranslateMessage(MSG* pMsg)


BOOL CGfxListTip::PreTranslateMessage(MSG* pMsg)
   CWnd *pWnd;
   int hittest;

     case WM_LBUTTONDBLCLK:         // Xavier added
     POINTS pts = MAKEPOINTS(pMsg->lParam);
     POINT  point;
     point.x = pts.x;

To use double click features in your program derive a class from GfxListCtrl. In my program that class is CAddressCtrl.

You have to override the OnLButtonDblClk(UINT nFlags, CPoint point) member function. Pass the point to CGfxListCtrl::HitTestEx(CPoint & point, int * col) member function and it will return to you the physical row and column on the screen at which the double click occurred. You can use the GetItemTextEx(index,column,cs) function where cs is a CString object to get the text of that Item. If you need to know the actual column on which the double click occurred like I do then you will have to use

column = pManager->FindColumnById(GetColumnIndex(column));

Based on this you can take the required action. In my program if you double click on the phone number it dials that number or double clicking on the URL will take you there. Here is the code

void CAddressCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
   if( GetFocus() != this ) SetFocus();
   int index, column;
   if ((index = HitTestEx(point, &column)) != -1) {
   if (column > 0 )  {
      CString cs;
      CString strURL;
      CString strName;
      CString strComment;
      if (pManager) {
         column = pManager->FindColumnById(GetColumnIndex(column));
      switch (column) {
        case ADDR_EMAIL: 
          if (!cs.IsEmpty()) {
          if (!cs.IsEmpty()) {
            if (strncmp((LPCTSTR) cs, "http://",7)==0)
            else {
     case ADDR_HOME_PHONE:
         if (!cs.IsEmpty()) {
            if ( column == ADDR_BUSINESS_PHONE)
               strComment = "Business Number";
               strComment = "Home Number";
            if (tapiRequestMakeCall(cs,"Address",strName, strComment)!=0) {
              AfxMessageBox("Unable to dial the number");
   CGfxListCtrl::OnLButtonDblClk(nFlags, point);

I modified Chris Maunder's class (CHyperLink) and made the GotoURL and GetRegKey member functions as static so that they can be called directly without the need to create an instance of the class. tapiRequestMakeCall is the TAPI function to make a call. If you want to use it in your project just include <tapi.h> and TAPI32.LIB into your project.

Serializations of the Template class CArray

To load and save my address array I serialized the CArray derived class as follows

template <> 
void AFXAPI SerializeElements <CPerson> ( 
 CArchive& ar, CPerson* pNewPersons, int nCount ) {
   for ( int i = 0; i < nCount; i++, pNewPersons++ )    {
       // Serialize each CPerson object
       pNewPersons->Serialize( ar );
which makes the serialization of my CDocument class as simple as
void CAddressDoc::Serialize(CArchive& ar)

You can start the address book program with /tray as a command line parameter to tray it as it starts up.

If you like this program please let me know. If you want anymore details ask me I will it try to add it to this page.

Wishing you all the best with Visual C++


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Xavier John
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralAddress Book Pin
kiransrinivas28-Feb-11 4:46
memberkiransrinivas28-Feb-11 4:46 
General[Message Deleted] Pin
it.ragester28-Mar-09 5:38
memberit.ragester28-Mar-09 5:38 
QuestionHow to remove some of the fields in the list? Pin
reiner tolentino20-Nov-07 23:25
memberreiner tolentino20-Nov-07 23:25 
GeneralA Great Tutorial! Pin
goiania4-May-05 10:00
membergoiania4-May-05 10:00 
Generalhelp address book Pin
promego2-May-05 4:03
memberpromego2-May-05 4:03 
General.SetHotItem(); problem Pin
Anonymous31-Mar-05 4:30
sussAnonymous31-Mar-05 4:30 
GeneralCSV Pin
imajit29-Mar-05 19:06
memberimajit29-Mar-05 19:06 
GeneralRe: CSV Pin
Xavier John1-Apr-05 7:04
memberXavier John1-Apr-05 7:04 
Generalisdn sms facsimile Pin
awani green12-Feb-05 0:22
memberawani green12-Feb-05 0:22 
Generalplease help me Xavier:code for tapi in vc++ for accepting incoming calls Pin
abhiaruna26-Jan-05 18:26
memberabhiaruna26-Jan-05 18:26 
GeneralRe: please help me Xavier:code for tapi in vc++ for accepting incoming calls Pin
Xavier John28-Mar-05 14:39
memberXavier John28-Mar-05 14:39 
General(SOS) Please Help me Xavier John... Pin
vahid.k1LOO6-Jan-05 5:44
membervahid.k1LOO6-Jan-05 5:44 
D'Oh! | :doh:

Dear Sir,
First of all,I would like to express my appreciation.Thanks alot
for writing such a wonderful program.It really helps me alot in my study.
I am still a beginner in VC++ programming.Actually,I am engaged in a project (an answering machine).
In this project,I include "tapi.h" and "mmsystem.h" .
But I'm getting a problem in the link of code in Visual C++ 6.0 :
" error LNK2001 : Unresolved external Symbol mmioOpen... "
Please help me.
My E-mail :
I would be very happy if you could answer my questions.
Thanks again.Blush | :O

GeneralRe: (SOS) Please Help me Xavier John... Pin
Xavier John6-Jan-05 6:37
memberXavier John6-Jan-05 6:37 
QuestionHow To Solve it Pin
thienle5-Nov-03 14:55
memberthienle5-Nov-03 14:55 
GeneralSerializing CArray Pin
mellertson14-Aug-03 9:57
membermellertson14-Aug-03 9:57 
GeneralRe: Serializing CArray Pin
mellertson14-Aug-03 10:01
membermellertson14-Aug-03 10:01 2003 Problem! Pin
Yoyoboy31-Jul-03 23:55
memberYoyoboy31-Jul-03 23:55 
GeneralRepeatedly causing an ASSERT when converted to VC++ 7 Pin
pariann11-May-03 20:44
memberpariann11-May-03 20:44 
QuestionHow to use the address book with other project Pin
vvv_slava9-May-03 14:35
membervvv_slava9-May-03 14:35 
AnswerRe: How to use the address book with other project Pin
Xavier John10-May-03 4:19
memberXavier John10-May-03 4:19 
GeneralRe: How to use the address book with other project Pin
vvv_slava10-May-03 8:27
membervvv_slava10-May-03 8:27 
Questionhow Uses tapi.h in C++ builder 6.0 Pin
snake_plus_plus5-Mar-03 10:29
membersnake_plus_plus5-Mar-03 10:29 
GeneralProblem with tapirequestmakecall() as defined in tapi32.dll Pin
Rahul Rana7-Feb-03 19:38
sussRahul Rana7-Feb-03 19:38 
General2 convert x Dialog project Pin
kits20-Jan-03 3:11
memberkits20-Jan-03 3:11 
GeneralRe: 2 convert x Dialog project Pin
Xavier John20-Jan-03 3:43
memberXavier John20-Jan-03 3:43 
GeneralEquation editor Pin
dodinh20-Jan-03 2:29
memberdodinh20-Jan-03 2:29 
GeneralI can't change the lable in the columns Pin
Angel Gavrilov10-Aug-02 23:41
sussAngel Gavrilov10-Aug-02 23:41 
GeneralRe: I can't change the lable in the columns Pin
Angel Gavrilov11-Aug-02 0:54
sussAngel Gavrilov11-Aug-02 0:54 
Questioncan not adjust the wide of first column Pin
Fujun13-Jul-02 12:28
sussFujun13-Jul-02 12:28 
AnswerRe: can not adjust the wide of first column Pin
Angel Gavrilov11-Aug-02 1:07
sussAngel Gavrilov11-Aug-02 1:07 
QuestionMay I use your code? Pin
Fish23-Dec-01 20:49
memberFish23-Dec-01 20:49 
AnswerRe: May I use your code? Pin
Xavier John15-Jan-02 20:09
memberXavier John15-Jan-02 20:09 
GeneralRe: May I use your code? Pin
Arash2-Feb-02 22:05
memberArash2-Feb-02 22:05 
GeneralRe: May I use your code? Pin
Armyman12b16-Sep-04 10:01
memberArmyman12b16-Sep-04 10:01 
QuestionAdd New Contact??? Pin
Paras Shah30-Jul-01 1:07
memberParas Shah30-Jul-01 1:07 
AnswerRe: Add New Contact??? Pin
Xavier John30-Jul-01 4:49
memberXavier John30-Jul-01 4:49 
GeneralRe: Add New Contact??? Pin
Paras Shah2-Aug-01 23:06
memberParas Shah2-Aug-01 23:06 
GeneralRe: Add New Contact??? Pin
Xavier John3-Aug-01 8:20
memberXavier John3-Aug-01 8:20 
Generalsearch functions Pin
Taketo17-Jun-01 8:17
memberTaketo17-Jun-01 8:17 
GeneralNotes Edit Box scrolls off the border Pin
Peter Hopkins30-Aug-00 16:14
sussPeter Hopkins30-Aug-00 16:14 
GeneralRe: Notes Edit Box scrolls off the border Pin
Xavier John31-Aug-00 4:35
sussXavier John31-Aug-00 4:35 
GeneralRe: Notes Edit Box scrolls off the border Pin
Peter Hopkins31-Aug-00 10:37
sussPeter Hopkins31-Aug-00 10:37 
GeneralSorting in the Address Book Sample Pin
Alex Mitchell10-Jul-00 6:26
sussAlex Mitchell10-Jul-00 6:26 
GeneralRe: Sorting in the Address Book Sample Pin
Xavier John31-Aug-00 4:37
sussXavier John31-Aug-00 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 7 Aug 2000
Article Copyright 1999 by Xavier John
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid