Drawn app with textured background

zhaozj2021-02-08  263

From http://www.microsoft.com/msj/0597/c0597.aspx This article may include a valid network connection when publishing, but now there is no website or web page that does not exist. To keep the integrity of the article, we keep the text of the connection, but disable connections.

May 1997

Code of this article: C 0597.exe (22KB)

Paul Dilascia is a free software consultant, specialty is C and Windows training and software development. He is a book by Windows : Writing Reusable Code In C (Addison-Wesley, 1992) author.

Q How do I write an app that draws a textured background like Microsoft® Encarta®, Bookshelf®, and Moneyhow? I tried to deal with WM_ EraseBkGnd but I can't work.

Robert H. MOWERY III

A

WM_RASEBKGND is a correct idea, but draws textured background - if you understand what I meant - may make you headache. I have written this application demonstration that is called PForm (Colored Forms or Beautiful Forms, how to see you). PFORM is a standard MFC application that displays a dialog as a form using CFormView. It allows you to display a variety of different backgrounds when you display the dialog: no format, lash, cyan stone carving background control, or all stone carving background (see Figure 1). I chose a form-based application because dialogs are in Windows® Drawing work control minimum; if you use your own main window and custom control, you can do anything you want to do; draw background is not a challenge. Figure 2 shows the code of the CMYFORMVIEW. The remainder of PForm is MFC's normal work, you can download it at the connection starting in this article.

Figure 1: PFORM- unformatted

The easiest and most reliable establishment of background texture is to use a pattern brush. Basic thinking is to deal with WM_CTLCOLOR messages so that you can tell Windows what brush is used in the background of various controls. When Windows is to draw controls, it sends WM_CTLCOLOR to the parent window of the control. This is a good opportunity you change the background of the control. For more information on setting up control colors, check out my columns in July 1996) A separate message, WM_CTLCOLOR, CTLColor_BTN, including subcodes, such as button background, and CTLColor_dlg using the entire dialog. In the new Win32® field, WM_CTLCOLOR is replaced with a separate WM_CTLCOLORXXX message. Figure 3 demonstrates all new messages. To be backward compatible, the MFC maps new messages to the old, technology over time, and is more convenient in some time. Special, PForm uses a simple ONCTLCOLOR handler to set the same background as all WM_CTLCOLORXXX messages. For the concave background, it looks like this: Hbrush CMYFORMVIEW :: ONCTLCOLOR

CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

PDC-> setbkmode (transparent);

PDC-> SetTextColor

Getsyscolor (color_windowtext));

Return M_Brden;

} I set up the background mode to transparent, so that all text is transparently drawn (for example, a radio box, a static control or text box, do not change the background.

Figure 4 Otherwise, Windows will use the current text color. Here you can also call PDC-> SetTextColor or SetBkcolor, if you need to modify the background color of this book. After the background mode is finished, OnCTLColor returns a handle M_BrDents for a brush. Note that the MFC conversion function CBRUSH :: Operator Hbrush converts CBRUSH to a HBrush. By returning a hbrush, you tell Windows with it to draw the background of the control. To create a pattern brush, you have to enter the position, then call - what is left? -Createpatternbrush. // in CMYFORMVIEW :: CMYFORMVIEWM_BMDENTS.LOADBITMAP (iDB_DENTS);

m_brdomts.createpatternbrush (& m_bmdens); IDB_DENTS determines bitmap resources, CMYFORMVIEW :: CMYFORMVIEW Loads CBITMAP M_BMDENTS, then call CBRush :: CreatePatternbrush Create a brush from this bitmap. Figure 4 demonstrates this bitmap, a small square of the 8 x 8 pixel of draws. When Windows uses a pattern to brush a zone, it repeats this pattern, providing the effect you see as 5.

Figure 5: Dark view

Finally, you have to set the text color to color_ windowtext, because you don't do this, text color is black, even when the user has customized his desktop color scheme. When you return to WM_ONCTLCOLOR, when you return a non-null value, Windows does not set the text color, you can only be self-reliant. It is not bad until there is currently. There is only one pattern brush: in Windows 95, they cannot be greater than 8 x 8 pixels (they can be your favorite size in Windows NT®.) If you want to use a larger bitmap in Windows 95 - For example, the stone carving pattern in Figure 6, you can't use the pattern brush and oncTlcolor, you must handle WM_RASEBKGND. One feature, two implementation methods - Why don't you just like Windows? Windows sent by Windows before sending WM_PAINT is WM_ERASEBKGND, if someone calls InvalidateWindow and the Frase parameter is TRUE. The default message handler (DEFWNDPROC) uses the system color getSysColor (Color_Window) redo window, however, you can handle WM_ EraseBkGnd to make your own heavy blow. CMYFORMVIEW :: OneRaseBkgn in PForm uses stone carving bitmap window. In other words, it repeats this bitmap - from left to right, from bottom to - until the entire window draws.

Figure 6: Stone carving pattern but wait! OneRaseBkGnd is only for the parent window instead of the control. How can you display a stone carving background in the option box, radio box, and static text box? You must be a little. In PFORM, I returned an empty brush for OnCTLColor. An empty or invalid brush is a brush that does not paint anything. i You can build a such object with CGDiobject :: CreateStockObject (Null_brush). The text is displayed in a transparent way with an empty brush: the control does not care yourself on the screen. So now the dialog uses the stone carving bitmail background (responding to WM_ERASEBKGND), and the control uses an empty brush to call yourself, resulting in the effect of keeping the stone carving background of the parent window. is it beautiful? This mechanism only does not work for two types of controls: text boxes and static icons. The problem with the text box is that they can be edited. If the user typed ABC and pressed the BackSpace to remove the C, the text box updates yourself by the starting AB starting, then uses the brush returned by ONCTLCOLOR to update the remaining blank. Usually, this will wipe C, but because I overdlcolor, use an empty brush, c is not wiped, oh. If you download and compile this program, select View | Marble, do not select View | Paint Edit Controls Too to use an empty brush, then type it in the text box and try to delete it, you can see this problematic behavior. The deleted character does not disappear - you must adjust the size of the edit control to get rid of them. Air brush technology has failed on the static icon applied to transparent color. The icon is 16 x 16 or 32 x 32 bitmap, allowing the following things to display using transparent color. This is the reason why the icon does not look a rectangle. If you return a colored or pattern brush, the icon works very well; if you return an empty brush for OnctLcolor, Windows does not properly draw icons. I personally think this is a bug, but it is easy to guess its reason: static control may allocate a memory device environment, draw it with a background brush, then draw it on it, finally copy it to the screen. If the background brush is an empty brush, the background retains the random memory data left when Windows allocates a memory device environment. So how do you solve these two questions? For text boxes, I simply kick out: Hbrush CmyformView :: oncTlcolor (...)

{

IF (nctlcolor == ctlcolor_edit)

// Normal Processing:

// don't do anything

Return CFormview :: ONCTLCOLOR (PDC,

PWND, NCTLCOLOR);

}

Figure 7 White text box

Now the text box is normal to display as white (actually Window_Color) (see Figure 7). I personally think that the box is the best. If you really want your text box with bitmap background, you will do a lot of things, because the text box has a lot of your own drawing process, this process exceeds a range of columns, but a good suggestion is to use and your bitmap The solid color of the fit is like I am using the cyan brush used in Figure 8.

Figure 8 cyan background

For static icons, you have two options. You can choose opaque colors when designing icons, or you use a self-drawing icon, new things for Windows95. For PForm, I choose the latter, I will finish it within a minute. First of all, I will point out some problems I have to consider when the texture background is completed. Special, you must consider the clear level under your background contrast. Like Figure 7, if your bitmap contains dark color, the text looks bad. The best solution is to use the solid color that cooperates with your bitmap, like Figure 8. Another option is to select a bitmap that uses the brightly. Of course - even if you never know why the text color of the zipper is yellow - you might want to use black text - but you start to deviate from the user interface - the most ideal thing is that you allow users to choose text colors. Finally, I have to explain how the button is mixed with the background. OnCTLColor allows you to control buttons background colors, but not it's look. This method is stupid, because the button is not like looks like it seems to have a background. In order to color the appearance, you must implement a self-painted button and process the WM_DRAWITEM message. PFORM demonstrates how to do; this is basically just a boring skill. CMYFORMVIEW :: OnDrawItem is doing this job. It draws a pure or stone carving button with color_btntext, if the button is pressed, offset down and right. The same function also considers the static icon of PForm using empty brush painting stone carvings: After using stone carving icon background, it calls Drawicon to manually draw this icon. The packed guy will notice that I have mixed in both cases: I have no alien graph. Technically, I should calculate the location of the bitmap to set the origin of the drawing, but it is indasistent to my poor brain at the morning, so I started from (0, 0). If you pay attention to it. You will see the stone carving bitmap in Figure 7 is slightly biased in the button and static icon. But don't look at this close to the telescope, it works very well. There will be a border around Konga, so I will leave the static icon. For the pattern brush (such as a dent example), you must use the setbrushorgex to set the original point of the brush, this, I said, too much for excessive fatigue. I keep the button in the deck view, which is pure gray. My onDrawItem for the button is not all completed - I didn't draw a focus rectangle - left to you at night, one person is bored, dry (skill: using drawfocusRect.) I left the raw material of the sandpaper brush as a practice, giving those senior Or alone, a person, no more other things. One of the wonderful differences for Windows 95 and Windows NT is that Windows NT automatically aligns the pattern brush for you, so you don't all need to call Setbrushorgex. Now, you get it - a lot of messy lives. Like usually in Windows, the appearance seems to be beautiful inside. It is actually horrible. If you want to be like Encarta and other company's imaginative user interface, there is no shortcut to go. You can only grind your teeth, then write some code - if you want to display the text box, maybe more than I have seen more. At least, if you write the code for the C class collection, you can reuse it in more than one project. Do you jump into this trap depends on your user. The last warning: If you use a bitmap, choose a color of only 20 standard system, otherwise you will face another nightmare: Implement the palette. Problems about C or C programming? Send it to askPD@pobox.com

From May 1997, Microsoft Systems Journals. You can get it from the magazine booth, or a better way is to subscribe.

转载请注明原文地址:https://www.9cbs.com/read-1875.html

New Post(0)