
案例2:可扩展的对话框【春苗教育】.ppt
26页案例2:可扩展的对话框1优讲借鉴VC++程序设计需求与目标n这个功能类似的显示聊天记录n当用户要显示一些过多的选择时,对话框自动扩展其大小,将隐藏的部分显示出来2优讲借鉴VC++程序设计效果3优讲借鉴VC++程序设计效果4优讲借鉴VC++程序设计手段n利用CDialog类的SetRect函数来设定对话框的大小5优讲借鉴VC++程序设计手段n利用CDialog类的SetRect函数来设定对话框的大小n调用MoveWindow函数来实现改变对话框的大小6优讲借鉴VC++程序设计编程步骤(1)n使用AppWizard创建一个基于对话框的应用程序7优讲借鉴VC++程序设计编程步骤(2)n为对话框添加一些控件8优讲借鉴VC++程序设计编程步骤(2)9优讲借鉴VC++程序设计编程步骤(2)10优讲借鉴VC++程序设计编程步骤(3)n为对话框添加3个成员变量:nBOOL m_bExpand;n// 记录是否已经扩展对话框nUINT m_nExpandedHeight;n// 扩展后对话框的高度nUINT m_nNormalHeight;n// 扩展前对话框的高度11优讲借鉴VC++程序设计BOOLntypedef int BOOL;ntypedef long BOOL;nA Boolean value.12优讲借鉴VC++程序设计UINT ntypedef unsigned int UINT;nA 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-bit unsigned integer on Win32.13优讲借鉴VC++程序设计编程步骤(4)n在对话框的OnInitDialog函数中加入以下代码:nm_bExpand = FALSE;nCRect rcDlg, rcMarker;nGetWindowRect(rcDlg);nm_nExpandedHeight = rcDlg.Height();nGetDlgItem(IDC_BORDER)->GetWindowRect(rcMarker);nm_nNormalHeight = (rcMarker.top - rcDlg.top);nrcDlg.SetRect(rcDlg.left, rcDlg.top, rcDlg.left + rcDlg.Width(), rcDlg.top + m_nNormalHeight);nMoveWindow(rcDlg, TRUE);14优讲借鉴VC++程序设计CWnd::GetWindowRect nvoid GetWindowRect( LPRECT lpRect ) const; nlpRectnPoints to a CRect object or a RECT structure that will receive the screen coordinates of the upper-left and lower-right corners.nRemarksnCopies the dimensions of the bounding rectangle of the CWnd object to the structure pointed to by lpRect. The dimensions are given in screen coordinates relative to the upper-left corner of the display screen. The dimensions of the caption, border, and scroll bars, if present, are included.15优讲借鉴VC++程序设计RECT、 LPRECTntypedef struct tagRECTn{n LONG left;n LONG top;n LONG right;n LONG bottom;n} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;16优讲借鉴VC++程序设计CRectnclass CRect : public tagRECTn{npublic:n……n// retrieves the widthnint Width() const;n// returns the heightnint Height() const;n……n}17优讲借鉴VC++程序设计CWnd::GetDlgItem nCWnd* GetDlgItem( int nID ) const;nnIDnSpecifies the identifier of the control or child window to be retrieved.nReturn ValuenA pointer to the given control or child window. If no control with the integer ID given by the nID parameter exists, the value is NULL. nThe returned pointer may be temporary and should not be stored for later use.nRemarksnRetrieves a pointer to the specified control or child window in a dialog box or other window. The pointer returned is usually cast to the type of control identified by nID.18优讲借鉴VC++程序设计控件IDnresource.hn#define IDC_BORDER 1000n#define IDC_EXPAND 100119优讲借鉴VC++程序设计CRect::SetRect nvoid SetRect( int x1, int y1, int x2, int y2 );nx1nSpecifies the x-coordinate of the upper-left corner.ny1nSpecifies the y-coordinate of the upper-left corner.nx2nSpecifies the x-coordinate of the lower-right corner.ny2nSpecifies the y-coordinate of the lower-right corner.nRemarksnSets the dimensions of CRect to the specified coordinates.20优讲借鉴VC++程序设计CWnd::MoveWindow nvoid MoveWindow( LPCRECT lpRect, BOOL bRepaint = TRUE );nlpRectnThe CRect object or RECT structure that specifies the new size and position.nbRepaintnSpecifies whether CWnd is to be repainted.nRemarksnChanges the position and dimensions. nFor a top-level CWnd object, the x and y parameters are relative to the upper-left corner of the screen. For a child CWnd object, they are relative to the upper-left corner of the parent window’s client area. 21优讲借鉴VC++程序设计编程步骤(5)n为“隐藏”按钮添加单击响应函数OnExpand:22优讲借鉴VC++程序设计编程步骤(5)n为“隐藏”按钮添加单击响应函数OnExpand:nvoid CExpandDlgDlg::OnExpand() n{nm_bExpand = !m_bExpand;n nCRect rcDlg;nGetWindowRect(rcDlg);23优讲借鉴VC++程序设计编程步骤(5)nif(m_bExpand)n{nrcDlg.SetRect(rcDlg.left, rcDlg.top, rcDlg.left + rcDlg.Width(), rcDlg.top + m_nExpandedHeight);nm_expand.SetWindowText("隐藏");n}nelsen{nrcDlg.SetRect(rcDlg.left, rcDlg.top, rcDlg.left + rcDlg.Width(), rcDlg.top + m_nNormalHeight);nm_expand.SetWindowText("显示");n}nMoveWindow(rcDlg, TRUE);n}24优讲借鉴VC++程序设计CWnd::SetWindowText nvoid SetWindowText( LPCTSTR lpszString );nlpszStringnPoints to a CString object or null-terminated string to be used as the new title or control text.nRemarksnSets the window’s title to the specified text. If the window is a control, the text within the control is set. 25优讲借鉴VC++程序设计LPCTSTR nA 32-bit pointer to a constant character string that is portable for Unicode and DBCS.26优讲借鉴。












