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

📄 ch06.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
red, green, and blue color components and converts them into a valid Windows color reference. The values for the red, green, and blue color components can be anything from 0 to 255—the higher the value, the brighter that color component. This code 
creates a bright blue pen. If all the color values were 0, the pen would be black; if the color values were all 255, the pen would be white.</P>

<P><I>Table 6.3&#151;Pen Styles</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Style</B></P>

<TD>

<P><B>Description</B></P>

<TR>

<TD>

<pre><font color="#008000">PS_DASH</font></pre>

<TD>

<P>Specifies a pen that draws dashed lines</P>

<TR>

<TD>

<pre><font color="#008000">PS_DASHDOT</font></pre>

<TD>

<P>Specifies a pen that draws dash-dot patterned lines</P>

<TR>

<TD>

<pre><font color="#008000">PS_DASHDOTDOT</font></pre>

<TD>

<P>Specifies a pen that draws dash-dot-dot patterned lines</P>

<TR>

<TD>

<pre><font color="#008000">PS_DOT</font></pre>

<TD>

<P>Specifies a pen that draws dotted lines</P>

<TR>

<TD>

<pre><font color="#008000">PS_INSIDEFRAME</font></pre>

<TD>

<P>Specifies a pen that's used with shapes, where the line's thickness must not extend outside of the shape's frame</P>

<TR>

<TD>

<pre><font color="#008000">PS_NULL</font></pre>

<TD>

<P>Specifies a pen that draws invisible lines</P>

<TR>

<TD>

<pre><font color="#008000">PS_SOLID</font></pre>

<TD>

<P>Specifies a pen that draws solid lines</P></TABLE>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>If you want to control the style of a line's end points or want to create your own custom patterns for pens, you can use the alternate <font color="#008000">CPen</font> constructor, which requires a few more arguments than the <font 
color="#008000">CPen</font> constructor described in this section. To learn how to use this alternate constructor, look up <font color="#008000">CPen</font> in your Visual C++ online documentation.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>After creating the new pen, <font color="#008000">ShowPens()</font> selects it into the DC, saving the pointer to the old pen. The <font color="#008000">MoveTo()</font> function moves the pen to an X,Y co-ordinate without drawing as it moves:; the 
<font color="#008000">LineTo()</font> function moves the pen while drawing. The style, thickness, and color of the pen are used. Finally, you select the old pen into the DC.</P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>There are a number of line drawing functions other than <font color="#008000">LineTo()</font>, including <font color="#008000">Arc()</font>, <font color="#008000">ArcTo()</font>, <font color="#008000">AngleArc()</font>, <font 
color="#008000">PolyDraw()</font> and more.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Build and run Paint1 again. When the font display appears, click the window. You should see a pen display like Figure 6.4.</P>

<A HREF="Hfig04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch06/Hfig04.gif"><b>Fig. 6.4</b></A>

<P><I>The Pen display shows the effect of setting line thickness.</I></P>

<P><B>Using Brushes</B></P>

<P>A pen draws a line, of a specified thickness, on the screen. A brush fills a shape on the screen. You can create both solid and patterned brushes, and even create brushes from bitmaps that contain your own custom fill patterns. Paint1 will display both 
patterned and solid rectangles in the <font color="#008000">ShowBrushes()</font> function, shown in Listing 6.8.</P>

<P><I>Listing 6.8&#151;CPaint1View::ShowBrushes()</I></P>

<pre><font color="#008000">void CPaint1View::ShowBrushes(CDC * pDC)</font></pre>

<pre><font color="#008000">     // Initialize the rectangle position.</font></pre>

<pre><font color="#008000">     UINT position = 0;</font></pre>

<pre><font color="#008000">     // Select pen to use for rectangle borders</font></pre>

<pre><font color="#008000">     CPen pen(PS_SOLID, 5, RGB(255, 0, 0));</font></pre>

<pre><font color="#008000">     CPen* oldPen = pDC-&gt;SelectObject(&amp;pen);</font></pre>

<pre><font color="#008000">     // Draw seven rectangles.</font></pre>

<pre><font color="#008000">     for (UINT x=0; x&lt;7; ++x)</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          CBrush* brush;</font></pre>

<pre><font color="#008000">          // Create a solid or hatched brush.</font></pre>

<pre><font color="#008000">          if (x == 6)</font></pre>

<pre><font color="#008000">               brush = new CBrush(RGB(0,255,0));</font></pre>

<pre><font color="#008000">          else</font></pre>

<pre><font color="#008000">              brush = new CBrush(x, RGB(0,160,0));</font></pre>

<pre><font color="#008000">          // Select the new brush into the DC.</font></pre>

<pre><font color="#008000">          CBrush* oldBrush = pDC-&gt;SelectObject(brush);</font></pre>

<pre><font color="#008000">          // Draw the rectangle.</font></pre>

<pre><font color="#008000">          position += 50;</font></pre>

<pre><font color="#008000">          pDC-&gt;Rectangle(20, position, 400, position + 40);</font></pre>

<pre><font color="#008000">          // Restore the DC and delete the brush.</font></pre>

<pre><font color="#008000">          pDC-&gt;SelectObject(oldBrush);</font></pre>

<pre><font color="#008000">          delete brush;</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     // Restore the old pen to the DC.</font></pre>

<pre><font color="#008000">     pDC-&gt;SelectObject(oldPen);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>The rectangles painted with the various brushes in this routine will all be drawn with a border. To arrange this, just create a pen (this one is solid, 5 pixels thick, and bright red) and select it into the DC. It will be used to border the rectangles 
without any further work on your part. Like <font color="#008000">ShowFonts()</font> and <font color="#008000">ShowPens()</font>, this routine creates its graphical objects within a <font color="#008000">for</font> loop. Unlike those two functions, <font 
color="#008000">ShowBrushes()</font> creates a graphical object (in this routine, a brush) with a call to <font color="#008000">new</font>. This allows you to call either the one-argument constructor, which creates a solid brush, or the two argument 
constructor, which creates a hatched brush.</P>

<P>In Listing 6.8, the first argument to the two argument constructor is just the loop variable, x. Usually you do not want to show all the hatch patterns, but want to select a specific one. Use one of these constants for the hatch style:</P>

<ul>

<li> HS_HORIZONTAL Horizontal </P>

<li> HS_VERTICAL Vertical </P>

<li> HS_CROSS Horizontal and vertical</P>

<li> HS_FDIAGONAL Forwards diagonal</P>

<li> HS_BDIAGONAL Backwards diagonal</P>

<li> HS_DIAGCROSS Diagonal in both directions</P>

</ul>

<P>In a pattern that should be familiar by now, <font color="#008000">ShowBrushes()</font> selects the brush into the DC, determines the position at which to work, uses the brush by calling <font color="#008000">Rectangle()</font>, and then restores the 
old brush. When the loop is complete, the old pen is restored as well.</P>

<p><font color="#008000">Rectangle()</font> is just one of the shape-drawing functions that you can call. <font color="#008000">Rectangle()</font> takes as arguments the coordinates of the rectangle's upper-left and lower-right corners. Some others of 
interest are <font color="#008000">Chord()</font>, <font color="#008000">DrawFocusRect()</font>, <font color="#008000">Ellipse()</font>, <font color="#008000">Pie()</font>, <font color="#008000">Polygon()</font>, <font color="#008000">PolyPolygon()</font>, 
<font color="#008000">Polyline()</font>, <font color="#008000">and RoundRect()</font>, which draws a rectangle with rounded corners.</P>

<P>Once again, build and run Paint1. Click twice, and you should see the demonstration of brushes, as shown in Figure 6.5.</P>

<A HREF="Hfig05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch06/Hfig05.gif"><b>Fig. 6.5</b></A> 

<P><I>The Brushes display shows a number of different patterns inside </I><I>thick-bordered rectangles.</I></P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>Remember the call to <font color="#008000">Invalidate()</font> in <font color="#008000">CPaint1View::OnLButtonDown()</font>? Invalidate() actually takes an argument, with a default value of <font color="#008000">TRUE</font>. This Boolean argument tells 
Windows whether to erase the window's background. If you use <font color="#008000">FALSE</font> for this argument, the background is not erased. In Figure 6.6, you can see what happens to the Paint1 application if <font color="#008000">Invalidate()</font> 
gets called with an argument of <font color="#008000">FALSE</font>.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<A HREF="Hfig06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch06/Hfig06.gif"><b>Fig. 6.6</b></A> 

<P><I>Without erasing the background, the Paint1 application's windows get a </I><I>bit messy.</I></P>

<H3><B>Scrolling Windows</B></H3>

<P>Those famous screen rectangles called <I>windows</I> were developed for two reasons. The first reason is to partition screen space between various applications and documents. The second reason is to enable the user to view portions of a document when 
the document is too large to completely fit into the window. The Windows operating system and MFC pretty much take care of the partitioning of screen space. However, if you want to enable the user to view portions of a large document, you must create 
scrolling windows.</P>

<P>Adding scroll bars to an application from scratch is a complicated task. Luckily for Visual C++ programmers, MFC handles many of the details involved in scrolling windows over documents. If you use the document/view architecture and derive your view 
window from MFC's <font color="#008000">CScrollView</font> class, you get scrolling capabilities almost for free. I say &quot;almost&quot; because there are still a few details that you must handle. You'll learn those details in the following sections.</P>


<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>If you create your application using AppWizard, you can specify that you want to use <font color="#008000">CScrollView</font> as the base class for your view class. To do this, in the Step 6 Of 6 dialog box displayed by AppWizard, select your view 
window in the class list and then select <font color="#008000">CScrollView</font> in the B<U>a</U>se Class dialog box, as shown in Figure 6.7.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<A HREF="Hfig07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch06/Hfig07.gif"><b>Fig. 6.7</b></A> 

<P><I>You can create a scrolling window from within AppWizard.</I></P>

<P><B>Building the Scroll Application</B></P>

<P>In this section, you will build a sample program called Scroll, to experiment with a scrolling window. When Scroll first runs, it displays five lines of text. Each time you click the window with your left mouse button, five lines of text are added to 
the display. When you get more lines of text than fit in the window, a vertical scroll bar appears enabling you to scroll to the parts of the documents that you can't see.</P>

<P>As usual, building the application starts with AppWizard. Choose File, New, and select the Projects tab. Fill in the project name as Scroll and fill in an appropriate directory for the project files. Make sure that <font color="#008000">MFC AppWizard 
(exe)</font> is selected. Click OK.</P>

<P>Complete the AppWizard steps, selecting the following options:</P>

<P>Step 1: Single document</P>

<P>Step 2: Default settings</P>

⌨️ 快捷键说明

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