Tuesday, March 18, 2008

给C++初学者的50个忠告

下面的是学C++时要注意的。绝对经典。!!

  1.把C++当成一门新的语言学习(和C没啥关系!真的。);

  2.看《Thinking In C++》,不要看《C++变成死相》;

  3.看《The C++ Programming Language》和《Inside The C++ Object Model》,不要因为他们很难而我们自己是初学者所以就不看;

  4.不要被VC、BCB、BC、MC、TC等词汇所迷惑——他们都是集成开发环境,而我们要学的是一门语言;

  5.不要放过任何一个看上去很简单的小编程问题——他们往往并不那么简单,或者可以引伸出很多知识点;

  6.会用Visual C++,并不说明你会C++;

  7.学class并不难,template、STL、generic programming也不过如此——难的是长期坚持实践和不遗余力的博览群书;

  8.如果不是天才的话,想学编程就不要想玩游戏——你以为你做到了,其实你的C++水平并没有和你通关的能力一起变高——其实可以时刻记住:学C++是为了编游戏的;

  9.看Visual C++的书,是学不了C++语言的;

  10.浮躁的人容易说:XX语言不行了,应该学YY;——是你自己不行了吧!?

  11.浮躁的人容易问:我到底该学什么;——别问,学就对了;

  12.浮躁的人容易问:XX有钱途吗;——建议你去抢银行;

  13.浮躁的人容易说:我要中文版!我英文不行!——不行?学呀!

  14.浮躁的人容易问:XX和YY哪个好;——告诉你吧,都好——只要你学就行;

  15.浮躁的人分两种:a)只观望而不学的人;b)只学而不坚持的人;

  16.把时髦的技术挂在嘴边,还不如把过时的技术记在心里;

  17.C++不仅仅是支持面向对象的程序设计语言;

  18.学习编程最好的方法之一就是阅读源代码;

  19.在任何时刻都不要认为自己手中的书已经足够了;

  20.请阅读《The Standard C++ Bible》(中文版:标准C++宝典),掌握C++标准;

  21.看得懂的书,请仔细看;看不懂的书,请硬着头皮看;

  22.别指望看第一遍书就能记住和掌握什么——请看第二遍、第三遍;

  23.请看《Effective C++》和《More Effective C++》以及《Exceptional C++》;

  24.不要停留在集成开发环境的摇篮上,要学会控制集成开发环境,还要学会用命令行方式处理程序;

  25.和别人一起讨论有意义的C++知识点,而不是争吵XX行不行或者YY与ZZ哪个好;

  26.请看《程序设计实践》,并严格的按照其要求去做;

  27.不要因为C和C++中有一些语法和关键字看上去相同,就认为它们的意义和作用完全一样;

  28.C++绝不是所谓的C的“扩充”——如果C++一开始就起名叫Z语言,你一定不会把C和Z语言联系得那么紧密;

  29.请不要认为学过XX语言再改学C++会有什么问题——你只不过又在学一门全新的语言而已;

  30.读完了《Inside The C++ Object Model》以后再来认定自己是不是已经学会了C++;

  31.学习编程的秘诀是:编程,编程,再编程;

  32.请留意下列书籍:《C++面向对象高效编程(C++ Effective Object-Oriented Software
Construction)》《面向对象软件构造(Object-Oriented Software
Construction)》《设计模式(Design Patterns)》《The Art of Computer
Programming》;

  33.记住:面向对象技术不只是C++专有的;

  34.请把书上的程序例子亲手输入到电脑上实践,即使配套光盘中有源代码;

  35.把在书中看到的有意义的例子扩充;

  36.请重视C++中的异常处理技术,并将其切实的运用到自己的程序中;

  37.经常回顾自己以前写过的程序,并尝试重写,把自己学到的新知识运用进去;

  38.不要漏掉书中任何一个练习题——请全部做完并记录下解题思路;

  39.C++语言和C++的集成开发环境要同时学习和掌握;

  40.既然决定了学C++,就请坚持学下去,因为学习程序设计语言的目的是掌握程序设计技术,而程序设计技术是跨语言的;

  41.就让C++语言的各种平台和开发环境去激烈的竞争吧,我们要以学习C++语言本身为主;

  42.当你写C++程序写到一半却发现自己用的方法很拙劣时,请不要马上停手;请尽快将余下的部分粗略的完成以保证这个设计的完整性,然后分析自己的错误并重新设计和编写(参见43);

  43.别心急,设计C++的class确实不容易;自己程序中的class和自己的class设计水平是在不断的编程实践中完善和发展的;

  44.决不要因为程序“很小”就不遵循某些你不熟练的规则——好习惯是培养出来的,而不是一次记住的;

  45.每学到一个C++难点的时候,尝试着对别人讲解这个知识点并让他理解——你能讲清楚才说明你真的理解了;

  46.记录下在和别人交流时发现的自己忽视或不理解的知识点;

  47.请不断的对自己写的程序提出更高的要求,哪怕你的程序版本号会变成Version 100.XX;

  48.保存好你写过的所有的程序——那是你最好的积累之一;

  49.请不要做浮躁的人;

  50.请热爱C++!

Wednesday, February 27, 2008

Tooltip

How to add tooltips for controls to an MFC modal dialog box

Article ID:141758
Last Review:November 21, 2006
Revision:3.1
This article was previously published under Q141758

SUMMARY

To make the CToolTipCtrl class work correctly, you must call the CToolTipCtrl::RelayEvent function. When you use this function, the mouse messages can be passed to the tooltip control.

For a non-modal dialog box window in an MFC application, use the window's CWnd::PreTranslateMessage function to call the CToolTipsCtrl::RelayEvent function. However, for a modal dialog box in MFC versions earlier than version 4.0, the CDialog::PreTranslateMessage function is not called because modal dialog boxes have their own message loops.

In MFC 4.0 and later versions, this is not a problem because of changes to the implementation of DoModal. Therefore, to use the CToolTipCtrl class in a modal dialog box, you must use a different approach for versions earlier than 4.0. This article gives you step-by-step examples that show you how to use the CToolTipCtrl class in an MFC modal dialog box for MFC 4.0 and earlier versions.

Back to the top

MORE INFORMATION

Step-by-step examples

The following procedures generate a default MFC skeleton application, add tooltips to the OK button in the About dialog box, and add tooltips to the dialog box itself.

For MFC 4.0 or later versions, follow these steps:
1.Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings.
2.Use ClassWizard to add a PreTranslateMessage override to CAboutDialog.
   CAboutDialog::PreTranslateMessage(MSG* pMsg)
{
if (NULL != m_pToolTip)
m_pToolTip->RelayEvent(pMsg);

return CDialog::PreTranslateMessage(pMsg);
}
3.Use ClassWizard to add a member variable for the OK button in the CAboutDlg class. Name the member variable m_btOK. Also, add an m_pToolTip pointer to a CToolTipCtrl object.
   class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CButton m_btOK;
//}}AFX_DATA

CToolTipCtrl* m_pToolTip;

//...
};
4.Add code to the CAboutDlg class constructor and destructor to initialize and release the tooltip object. You may first have to add a default destructor.
   CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
m_pToolTip = NULL;
}

CAboutDlg::~CAboutDlg()
{
delete m_pToolTip;
}
5.Override the OnInitDialog function of the CAboutDlg class to set up the tooltip control.
   BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();

//Set up the tooltip
m_pToolTip = new CToolTipCtrl;
if(!m_pToolTip->Create(this))
{
TRACE("Unable To create ToolTip\n");
return TRUE;
}

if(m_pToolTip->AddTool(this, "About Box"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}

if (m_pToolTip->AddTool(&m_btOK,"OK Button"))
{
TRACE("Unable to add OK button to the tooltip\n");
}

m_pToolTip->Activate(TRUE);

return TRUE;
}
6.Rebuild the application. Then, open the About dialog box. You will see the tooltips.
For versions of MFC earlier than version 4.0, follow these steps:
1.Use the Appwizard in Visual C++ to generate an MFC application. Name the application Tooltips. Use all the Appwizard default settings.
2.Include the header file in the Stdafx.h file.
3.Add the following member variables to the CTooltipsApp class in the Tooltips.h file.
   class CTooltipsApp : public CWinApp
{
//...
public:
HWND m_hwndDialog;
CToolTipCtrl* m_gpToolTip;

//...
};
4.Initialize the two variables in the application's constructor to NULL.
   CTooltipsApp::CTooltipsApp()
{
m_hwndDialog = NULL;
m_gpToolTip = NULL;
}
5.Override the CTooltipsApp::ProcessMessageFilter function.
   BOOL CTooltipsApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if (m_hwndDialog != NULL)
if (lpMsg->hwnd == m_hwndDialog ||
::IsChild(m_hwndDialog, lpMsg->hwnd))
{
if (NULL != m_gpToolTip)
m_gpToolTip->RelayEvent(lpMsg);
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
6.Use ClassWizard to add a member variable for the OK button in the CAboutDlg class. Name the member variable m_btOK. Also, add an m_pToolTip pointer to a CToolTipCtrl object.
   class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CButton m_btOK;
//}}AFX_DATA

CToolTipCtrl* m_pToolTip;

//...
};
7.Add code to the CAboutDlg class constructor and destructor to initialize and release the tooltip object. You may first have to add a default destructor.
   CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
m_pToolTip = NULL;
}

CAboutDlg::~CAboutDlg()
{
delete m_pToolTip;
}
8.Override the OnInitDialog function of the CAboutDlg class to pass the handle of the dialog box to the application.
   BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();

((CTooltipsApp*)AfxGetApp())->m_hwndDialog=m_hWnd;

if (!m_pToolTip)
{
m_pToolTip = new CToolTipCtrl;
if(!m_pToolTip->Create(this))
{
TRACE("Unable To create ToolTip\n");
return TRUE;
}

((CTooltipsApp*)AfxGetApp())->m_gpToolTip = m_pToolTip;

if(m_pToolTip->AddTool(this, "About Box"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}

if (m_pToolTip->AddTool(&m_btOK,"OK Button"))
{
TRACE("Unable to add OK button to the tooltip\n");
}

m_pToolTip->Activate(TRUE);
}

return TRUE;//return TRUE unless you set the focus to a control
//EXCEPTION: OCX Property Pages should return FALSE
}
9.Override the PostNcDestroy function of the CAboutDlg class to reset the variables in the application class.
   void CAboutDlg::PostNcDestroy( )
{
CDialog::PostNcDestroy();

((CToolTipsApp*)AfxGetApp())->m_hwndDialog= NULL;
((CToolTipsApp*)AfxGetApp())->m_gpToolTip= NULL;
}
10.Rebuild the application. Then, open the About dialog box. You will see the tooltips.