CDC: selectstockobject and CDC: SelectObject

Source: Internet
Author: User

Selectstockobject can only select a limited number of objects into the device environment, while SelectObject can select any GDI object into the device environment.

 
Check msdn. the above lists all the objects that can be selected by selectstockobject. The following is the original article.

Black_brush black brush.
Dkgray_brush dark gray brush.
Gray_brush gray brush.
Hollow_brush hollow brush.
Ltgray_brush light gray brush.
Null_brush null brush.
White_brush white brush.
Black_pen black pen.
Null_pen null pen.
White_pen white pen.
Ansi_fixed_font ANSI fixed system font.
Ansi_var_font ANSI variable system font.
Device_default_font device-dependent font.
Oem_fixed_font oem-dependent fixed font.
System_font the system font. by default, Windows uses the system font to draw menus, dialog-box controls, and other text. in Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.
System_fixed_font the fixed-width system font used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.
Default_palette default color palette. This palette consists of the 20 static colors in the system palette.

 

 

In GDI, DC (device context) is a very important concept.

In some books, DC is translated into a device description table (Charles Petzold, author of Windows Programming Fifth Edition ),

Some books translate DC into device context.

What is DC?

It may be easier to understand it using real-world examples.

If you like painting, you have to prepare canvas, paint, paint ......

After the painting environment is set up, you can draw.

The painting environment is DC.

In a graphic environment, everything is drawn. Therefore, you must prepare a DC to draw images on the screen. -- Writing is also a painting.

What are the objects in the painting environment?

Canvas-one of the GDI objects: Area

Paint Brush-one of the GDI objects: paint brush

Pigment box -- one of the GDI objects: palette

What kind of font should I write on a paint brush? Founder font? Xu Jinglei font? -- The font is also one of the GDI objects.

Some paint brushes are relatively thick and are used to paint a large area of background color. This is one of the brush-GDI objects: Brush

If you don't want to draw a picture, you just want to paste it on your canvas. -- One of the GDI objects: bitmap.

Therefore, there are six types of GDI objects available for DC.

Now you start painting. You picked up a pen. -- In windows, a paint brush object is selected: The SelectObject function is used. Of course, it doesn't matter if you don't have a pen. Windows has prepared several brushes for you. You can apply for the default paint brush provided by the system as follows: Hpen = getstockobject (white_pen );

If you are painting, you can change the pen if you feel uncomfortable. It doesn't matter. -- Returns SelectObject.

Of course, if you leave the studio, do not clear your paint brush. Otherwise, the room is full of pens. The paint brush is too messy. -- Deleteobject ()

 

 

Test example:

 

 

Void cex03aview: ondraw (CDC * PDC)
{
Cex03adoc * pdoc = getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native data here
 
 
PDC-> selectstockobject (gray_brush );
PDC-> ellipse (crect (300,320,400,420 ));

 
 
Cbrush brush1;
Brush1.createsolidbrush (RGB (0, 0, 255 ));
/* Same as above */cbrush brush (RGB (0,255, 0 ));
/* Function: This function creates a logic brush with the specified color.
Function Principle: hbrush createsolidbrush (colorref crcolor );
Parameter: crcolor: Specifies the paint brush color.
Return Value: if the function is successfully executed, the return value identifies a logical solid brush. If the function fails, the return value is null.
*/
 

Cbrush * ptempbrush = NULL;
Crect RC;
Getclientrect (& rc );
/* In Win32 SDK, the API function is prototype bool getclientrect (hwnd, // window handle lprect // client region coordinate );
In MFC, the prototype of this function is void getclientrect (lprect) const;

Getclientrect (& rc); obtains the region of the client relative to the client. Therefore, it is 0, 0 ,..

Getwindowrect (& rc); obtains the area of the window relative to the screen. the left and top of the window must be in the "outside" of the client, so the screentoclient is changed to-2,-2 .. (-2 and-2 are the difference between the client and window -- Border)

*/
Screentoclient (& rc );
Ptempbrush = (cbrush *) PDC-> SelectObject (& brush1); // save original brush.

// Paint upper left corner with blue brush.
PDC-> rectangle (0, 0, RC. Width ()/2, RC. Height ()/2 );

PDC-> SelectObject (& brush );
PDC-> ellipse (crect (100,120 ));

}

 

 

 

 

Void cmy10view: ondraw (CDC * PDC)
{
PDC-> moveTo (10, 10 );
PDC-> lineto (110,10 );
Cpen newpen (ps_dashdotdot, 10, (colorref) 192); // The width of the red pen is 10
Cpen * poldpen = PDC-> SelectObject (& newpen );
// When the new object is selected into the device environment, the pointer to the selected object is returned. Saves the original object so that it can be restored when the task is completed.
...
PDC-> SelectObject (poldpen); // restore the original object
}
Because the gdi c ++ Object Pointer returned by the SelectObject function is temporary, the application framework deletes the temporary C ++ object when the program is idle or controls the function to return, instead of simply saving this pointer in the data member of the class, we should use the getsafehdc function to convert it into a Windows handle, so as to persistently Save the GDI identifier.
Store a GDI object using a handle
Void cmy10view: ondraw (CDC * PDC)
{
Hpen m_hpen; // a pointer to the cpen object
PDC-> moveTo (10, 10 );
PDC-> lineto (110,10 );
Cpen newpen (ps_dashdotdot, 10, (colorref) 192); // The width of the red pen is 10
Cpen * poldpen = PDC-> SelectObject (& newpen); // returns the pointer to the previously selected object when the new object is selected into the device environment. Saves the original object so that it can be restored when the task is completed.
M_hpen = (Hpen) poldpen-> getsafehandle (); // get and save the handle of the original object
PDC-> moveTo (10, 20 );
PDC-> lineto (110,20 );
PDC-> SelectObject (cpen: fromhandle (m_hpen); // restores the original object. Unlike the previous example
PDC-> moveTo (10, 30 );
PDC-> lineto (110,30 );
}
Restore the GDI object handle to use the original GDI object for future operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.