⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apb.htm

📁 Visual C++ 6.0 21天自学教程 一本适合初学和对c++有一定基础的人学习的好书
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE> 1: void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point) 2: { 3:     // TODO: Add your message handler code here and/or call default 4: 5:     /////////////////////// 6:     // MY CODE STARTS HERE 7:     /////////////////////// 8: 9:         // Check to see if the left mouse button is down10:     if ((nFlags &amp; MK_LBUTTON) == MK_LBUTTON)11:     {12:         // Get the Device Context13:         CClientDC dc(this);14:15:         // Create a new pen16:         CPen lpen(PS_SOLID, 16, RGB(255, 0, 0));17:18:         // Use the new pen19:         dc.SelectObject(&amp;lpen);20:21:         // Draw a line from the previous point to the current point22:         dc.MoveTo(m_iPrevX, m_iPrevY);23:         dc.LineTo(point.x, point.y);24: 25:         // Save the current point as the previous point26:         m_iPrevX = point.x;27:         m_iPrevY = point.y;28:     }29:30:     // Check to see if the right mouse button is down31:     if ((nFlags &amp; MK_RBUTTON) == MK_RBUTTON)32:     {33:         // Get the Device Context34:         CClientDC rdc(this);35:36:         // Create a new pen37:         CPen rpen(PS_SOLID, 16, RGB(0, 0, 255));38: 39:         // Use the new pen40:         rdc.SelectObject(&amp;rpen);41: 42:         // Draw a line from the previous point to the current point43:         rdc.MoveTo(m_iPrevX, m_iPrevY);44:         rdc.LineTo(point.x, point.y);45: 46:         // Save the current point as the previous point47:         m_iPrevX = point.x;48:         m_iPrevY = point.y;49:     }50:51:     ///////////////////////52:     // MY CODE ENDS HERE53:     ///////////////////////54:55:     CDialog::OnMouseMove(nFlags, point);</PRE><PRE>56: }</PRE><DL>	<DT></DT>	<DD><B>2. </B>Extend the OnKeyDown function to add some of the following standard	cursors:	<P></DL><UL>	<LI>IDC_CROSS	<P>	<LI>IDC_UPARROW	<P>	<LI>IDC_SIZEALL	<P>	<LI>IDC_SIZENWSE	<P>	<LI>IDC_SIZENESW	<P>	<LI>IDC_SIZEWE	<P>	<LI>IDC_SIZENS	<P>	<LI>IDC_NO	<P>	<LI>IDC_APPSTARTING	<P>	<LI>IDC_HELP</UL><DL>	<DT></DT>	<DD>Your modified OnKeyDown function can look something like the following:	<P></DL><PRE>void CMouseDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){    // TODO: Add your message handler code here and/or call default    ///////////////////////    // MY CODE STARTS HERE    ///////////////////////    char lsChar;        // The current character being pressed    HCURSOR lhCursor;    // The handle to the cursor to be displayed    // Convert the key pressed to a character    lsChar = char(nChar);    // Is the character &quot;A&quot;    if (lsChar == `A')    {        // Load the arrow cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_ARROW);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;B&quot;    if (lsChar == `B')    {        // Load the I beam cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_IBEAM);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;C&quot;    if (lsChar == `C')    {        // Load the hourglass cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_WAIT);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;D&quot;    if (lsChar == `D')    {        // Load the cross hair cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_CROSS);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;E&quot;    if (lsChar == `E')    {        // Load the up arrow cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_UPARROW);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;F&quot;    if (lsChar == `F')    {        // Load the size cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_SIZEALL);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;G&quot;    if (lsChar == `G')    {        // Load the up/right-down/left size cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_SIZENWSE);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;H&quot;    if (lsChar == `H')    {        // Load the up/left-down/right size cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_SIZENESW);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;I&quot;    if (lsChar == `I')    {        // Load the left-right size cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_SIZEWE);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;J&quot;    if (lsChar == `J')    {        // Load the up-down size cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_SIZENS);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    if (lsChar == `K')    {        // Load the no cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_NO);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    if (lsChar == `L')    {        // Load the app starting cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_APPSTARTING);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    if (lsChar == `M')    {        // Load the help cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_HELP);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);    }    // Is the character &quot;X&quot;    if (lsChar == `X')    {        // Load the arrow cursor        lhCursor = AfxGetApp()-&gt;LoadStandardCursor(IDC_ARROW);        // Set the cursor flag        m_bCursor = TRUE;        // Set the screen cursor        SetCursor(lhCursor);        // Exit the application        OnOK();    }    ///////////////////////    // MY CODE ENDS HERE    ///////////////////////    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);</PRE><PRE>}</PRE><H2><A NAME="Heading10"></A>Day 4</H2><H3>Quiz</H3><DL>	<DT></DT>	<DD><B>1. </B>What did you accomplish by adding the two timer IDs to the resource	symbols?	<P>	<DT></DT>	<DD>You defined the two IDs so that they were available as constants throughout the	application.	<P>	<DT></DT>	<DD><B>2. </B>What is another way to add these two IDs to the application?	<P>	<DT></DT>	<DD>Add them as #define constants in the class header file (Day2Dlg.h), as follows:	<P></DL><PRE>/////////////////////////////////////////////////////////////////////// CTimersDlg dialog#define ID_CLOCK_TIMER 1#define ID_COUNT_TIMER 2class CTimersDlg : public CDialog{..</PRE><PRE>.</PRE><DL>	<DT></DT>	<DD><B>3. </B>How can you tell two timers apart in the OnTimer function?	<P>	<DT></DT>	<DD>You use the timer ID to determine which timer triggered the event.	<P>	<DT></DT>	<DD><B>4. </B>How many timer events does your application receive if the timer is	set for one second and your application has been busy for one minute, preventing	it from receiving any timer event messages?	<P>	<DT></DT>	<DD>One.	<P></DL><H2>Exercise</H2><P>Update your application so that when the counter timer is started, the clock timeris reset to run at the same interval as the counter timer. When the counter timeris stopped, return the clock timer to a one-second interval.</P><P>To change the interval at which a timer is running, you need to first stop thetimer and then restart it, as in Listing B.6.</P><P><H4>LISTING B.6. THE REVISED OnStarttime AND OnStoptimer FUNCTIONS.</H4><PRE> 1: void CTimersDlg::OnStarttime() 2: { 3:     // TODO: Add your control notification handler code here 4:  5:     /////////////////////// 6:     // MY CODE STARTS HERE 7:     /////////////////////// 8:  9:     // Update the variables10:     UpdateData(TRUE);11: 12:     // Initialize the count13:     m_iCount = 0;14:     // Format the count for displaying15:     m_sCount.Format(&quot;%d&quot;, m_iCount);16: 17:     // Update the dialog18:     UpdateData(FALSE);19:     // Start the timer20:     SetTimer(ID_COUNT_TIMER, m_iInterval, NULL);21: 22:     // Stop the clock timer23:     KillTimer(ID_CLOCK_TIMER);24:     // Restart the clock timer with the counter interval25:     SetTimer(ID_CLOCK_TIMER, m_iInterval, NULL);26: 27:     // Enable the Stop Timer button28:     m_cStopTime.EnableWindow(TRUE);29:     // Disable the Start Timer button30:     m_cStartTime.EnableWindow(FALSE);31: 32:     ///////////////////////33:     // MY CODE ENDS HERE34:     ///////////////////////35: }36: 37: void CTimersDlg::OnStoptimer()38: {39:     // TODO: Add your control notification handler code here40: 41:     ///////////////////////42:     // MY CODE STARTS HERE43:     ///////////////////////44: 45:     // Stop the timer46:     KillTimer(ID_COUNT_TIMER);47: 48:     // Stop the clock timer49:     KillTimer(ID_CLOCK_TIMER);50:     // Restart the clock timer with 1 second interval51:     SetTimer(ID_CLOCK_TIMER, 1000, NULL);52: 53:     // Disable the Stop Timer button54:     m_cStopTime.EnableWindow(FALSE);55:     // Enable the Start Timer button56:     m_cStartTime.EnableWindow(TRUE);57: 58:     ///////////////////////59:     // MY CODE ENDS HERE60:     ///////////////////////</PRE><PRE>61: }</PRE><P><H2><A NAME="Heading13"></A>Day 5</H2><H3>Quiz</H3><DL>	<DT></DT>	<DD><B>1. </B>What are the possible return codes that your application might receive	from the MessageBox function call when you specify the MB_RETRYCANCEL button combination?	<P>	<DT></DT>	<DD>IDRETRY and IDCANCEL.	<P>	<DT></DT>	<DD><B>2. </B>What are the common dialogs that are built into the Windows operating	systems that are defined as MFC classes?	<P>	<DT></DT>	<DD>The common Windows dialogs that are defined as MFC classes are	<P></DL><UL>	<LI>File selection	<P>	<LI>Font selection	<P>	<LI>Color selection	<P>	<LI>Page setup for printing	<P>	<LI>Printing	<P>	<LI>Find and replace</UL><DL>	<DT></DT>	<DD><B>3. </B>What is the difference between a modal dialog and a modeless dialog?	<P>	<DT></DT>	<DD>A modal dialog stops all application processing until the user responds to the	dialog. A modeless dialog allows the user to continue working with the rest of the	application while the dialog is open for use.	<P>	<DD><B>4. </B>How can you display a File Save dialog for the user instead of the	File Open dialog that you did have in your application?	<DT></DT>	<DD>In the class instance variable declaration, pass FALSE instead of TRUE. This	makes the variable declaration look like this:	<P></DL><PRE>CFileDialog m_ldFile(FALSE);</PRE><DL>	<DT></DT>	<DD><B>5. </B>Why did you not need to create any functions and add any code to your	custom dialog?	<P>	<DT></DT>	<DD>The only functionality that was needed on the custom dialog was calling UpdateData	before closing the dialog. Because the OK and Cancel buttons were never deleted from	the dialog, the OK button automatically performed this functionality.	<P></DL><H3>Exercises</H3><DL>	<DT></DT>	<DD><B>1. </B>Modify your application so that it includes the directory with the	filename in the application. (Hint: The GetFileName function returns the path and	filename that was selected in the File Open dialog.)	<P>	<DT></DT>	<DD>Modify the OnFileopen function as follows:	<P></DL><PRE>void CDialogsDlg::OnFileopen() {    // TODO: Add your control notification handler code here    ///////////////////////    // MY CODE STARTS HERE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -