Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C++

Address Book

Rate me:
Please Sign up or sign in to vote.
4.48/5 (21 votes)
6 Aug 2000CPOL3 min read 287.2K   5.5K   93  
Address Book application
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Address Book</TITLE>
</HEAD>
<BODY>
<b><P ALIGN="CENTER">Address Book</P></b> 

<P>Address Book demonstrates the use of GfxListCtrl control, CHyperlink class ,  CSystemTray class into one useful application. It also demonstrates dropdown tool button and well as using the template class CArray.</P>
<img src="address.gif">
<P>Even thought lot of programs at Codeguru 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.</P>
<P>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.</P>
<B>
<P>How do I use the Textcall back function ?</P>
</B><P>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.</P>
<P>An array is created to hold the CPerson objects as in CAddressDoc as</P>
<P>CArray&lt;CPerson,CPerson&amp;&gt; m_PersonArray;</P>
<P>This makes it easy to use the TextCallBack function. Your TextCallBack function for the GfxListCtrl is declared as</P>
<P>void GetTextCallback(int iIndex, int iSubItem, long lCode, CString &amp;cs)</P>
<P>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</P>

<pre>
void CAddressView::GetTextCallback(int iIndex, int iSubItem, long lCode, CString &amp;cs)
{
   int rc = wndList.GetColumnIndex(iSubItem);          // wndList is the GfxListControl
   CPerson person;
   cs = "";
   if (GetDocument()-&gt;m_PersonArray.GetSize() == 0 || 
   GetDocument()-&gt;m_PersonArray.GetSize() &lt; (iIndex + 1)) {
     return;
   }
   person = GetDocument()-&gt;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);
   }
}
</pre>

<P>For the AutoPreview alone you have to draw the text you want on to the screen in the function</P>

<P> long CAddressView::GetExInfoCallback(LXHDREX * pLx)</P>

<P>as</P>

<pre>
   :
   :
   case NTEX_AUTOPREVIEW: {
      LXHDREX_DIV * pLxEx = (LXHDREX_DIV *) pLx;
      COLORREF ocr = pLxEx-&gt;pDC-&gt;SetTextColor(RGB(0,0,255));
      pLxEx-&gt;pDC-&gt;DrawText(GetDocument()-&gt;m_PersonArray[pLx-&gt;dwItemData - 1].m_strNotes , 
             pLxEx-&gt;rcItem, DT_END_ELLIPSIS|DT_WORDBREAK);
      pLxEx-&gt;pDC-&gt;SetTextColor(ocr);
      return 1;
}

</pre>
<P> where m_PersonArray[Index].strNotes is the text I want it to display.</P>

<B><P>How to solve the double click problem of GfxListCtrl when tooltip is displayed ?</P>
</B><P>To solve the double click problem just make the following changes to the class CGfxListTip.</P>
<P>Add the style CS_DBLCLKS to the WNDCLASS as</P>

<pre>
CGfxListTip::CGfxListTip()
{
   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();
   if(!(::GetClassInfo(hInst, GFXLISTTIP_CLASSNAME, &amp;wndcls)))
   {
     wndcls.style = CS_DBLCLKS | CS_SAVEBITS ; // Xavier added CS_DBLCLKS 
     wndcls.lpfnWndProc = AfxWndProc;          // Xavier changed from ::DefWindowProc;
     wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
     :
     :
</pre>
<P>and add the line </P>
case WM_LBUTTONDBLCLK: 
<P>to the function </P>

BOOL CGfxListTip::PreTranslateMessage(MSG* pMsg)
<br>as

<Pre>
BOOL CGfxListTip::PreTranslateMessage(MSG* pMsg)
{
   CWnd *pWnd;
   int hittest;

   switch(pMsg-&gt;message)
   {
     case WM_LBUTTONDBLCLK:         // Xavier added
     case WM_LBUTTONDOWN:
     case WM_RBUTTONDOWN:
     case WM_MBUTTONDOWN:
     POINTS pts = MAKEPOINTS(pMsg-&gt;lParam);
     POINT  point;
     point.x = pts.x;
     :
     :
</pre>

<P>To use double click features in your program derive a class from GfxListCtrl. In my program that class is CAddressCtrl.</P>
<P>You have to override the OnLButtonDblClk(UINT nFlags, CPoint point) member function.
Pass the point to CGfxListCtrl::HitTestEx(CPoint &amp; 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 </P>
<P>
column = pManager-&gt;FindColumnById(GetColumnIndex(column));
</P>
<P>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</P>

<pre>
void CAddressCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
   if( GetFocus() != this ) SetFocus();
   int index, column;
   if ((index = HitTestEx(point, &amp;column)) != -1) {
   if (column &gt; 0 )  {
      CString cs;
      CString strURL;
      CString strName;
      CString strComment;
      GetItemTextEx(index,column,cs);
      if (pManager) {
         column = pManager-&gt;FindColumnById(GetColumnIndex(column));
      }
      switch (column) {
        case ADDR_EMAIL: 
          if (!cs.IsEmpty()) {
            strURL.Format("mailto:%s",(LPCTSTR)cs);
            CHyperLink::GotoURL(strURL,SW_SHOWNORMAL);
            break;
          }
      case ADDR_PERSONAL_WEB_PAGE:
      case ADDR_BUSINESS_WEB_PAGE:
          if (!cs.IsEmpty()) {
            if (strncmp((LPCTSTR) cs, "http://",7)==0)
              strURL=cs;
            else {
              strURL.Format("http://%s",(LPCTSTR)cs);
            }
            CHyperLink::GotoURL(strURL,SW_SHOWNORMAL);
            break;
          }
     case ADDR_HOME_PHONE:
     case ADDR_BUSINESS_PHONE:
         if (!cs.IsEmpty()) {
            if ( column == ADDR_BUSINESS_PHONE)
               strComment = "Business Number";
            else
               strComment = "Home Number";
            GetItemTextEx(index,ADDR_NAME,strName);
            if (tapiRequestMakeCall(cs,"Address",strName, strComment)!=0) {
              AfxMessageBox("Unable to dial the number");
            }
            break;
         }
    default: 
            DisplayProperties();
     }
   }
   }
   CGfxListCtrl::OnLButtonDblClk(nFlags, point);
}
</pre>

<P>I modified the Chris Maunder 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 &lt;tapi.h&gt; and TAPI32.LIB into your project.</P>

<B><P>Serializations of  the Template class CArray</P>
</B><P>To load and save my address array I serialized the CArray derived class as follows</P>

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

<pre>
void CAddressDoc::Serialize(CArchive&amp; ar)
{
   m_PersonArray.Serialize(ar);
}
</pre>

<br>
<p> You can start the address book program with <b>/tray</b> as a command line parameter to tray it as it starts up.</p>
<p>
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. </P>
Wishing you all the best with Visual C++
<br><br>
<a href="mailto:xavier_john@yahoo.com">Xavier John</a>

</BODY>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions