Use of the VC picture control

Source: Internet
Author: User
Use of the VC picture control

The classification of the VC picture control is summarized,

(1) Non-dynamic display of images (that is, images are first loaded through the resource manager, with a fixed ID)

(2) Loading images dynamically (you only need to specify the image path in the Program)

For convenience, we have created a dialog box-based project named ttest.

The dialog box class is ctestdlg.

(1) The VC picture control does not dynamically load images.

Method 1. First, use the picture control to implement it in the simplest way.

Steps:

Import an image in the resource with the ID idb_bitmap2

Then, add a picture control in the dialog box and right-click to open properties,

Select bitmap from the type drop-down box, and an image drop-down box will appear below,

After the image is opened, all the loaded images are displayed,

Select the image you want and run the program.

Method 2vc picture control.

Similarly, load an image with the ID of idb_bitmap2.

Testdlg. h

Cbrush m_brbk; // defined in public

Testdlg. cpp

Add the following to the oninitdialog () initialization function:

Bool ctestdlg: oninitdialog ()

{

Cdialog: oninitdialog ();

Cbitmap BMP;

BMP. loadbitmap (idb_bitmap2 );

M_brbk.createpatternbrush (& BMP );

BMP. deleteobject ();

Return true; // return true unless you set the focus to a control

}

In the Open Class Wizard, find the wm_ctlcolor message, reload the corresponding function onctlcolor (), and add the following:

Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)

{

Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

If (pwnd = This)

{

Return m_brbk;

}

Return HBr;

}

(2) VC picture controls dynamically load images.

Method 3 image control (in this example, the Kodak image editing control is used)

1. Make sure that the system has this control. Note that it cannot be used independently and must be used together with several other controls (especially imgcen. dll. If not, copy it from another machine. These files are imgadmin. OCX, imgcen. DLL, imgedit. OCX, imgscan. OCX, imgshl. DLL, imgthumb. OCX, imgutil. DLL, copy them to the Windows \ systemdirectory, and then use regsvr32.exe to register them separately.

2. Open the project, enter the resource manager, right-click the dialog box, and click Insert ActiveX control... Select the Kodak image editing control.

3. Select the control in the dialog box and add the variable m_ctrlpicture to it ..

4. Add the following in bool ctestdlg: oninitdialog:

Bool ctestdlg: oninitdialog ()

{

Cdialog: oninitdialog ();

M_ctrlpicture.setimage ("aa.jpg"); // ensure that the image is in the project directory, Or you can write an absolute path.

M_ctrlpicture.display ();

Return true; // return true unless you set the focus to a control

// Exception: OCX property pages shold return false

}

Compile and run it. The advantage of this method is that it may be applicable to multiple image formats.

Method 4 the VC picture control uses cbitmap and hbitmap to draw data directly using onpaint ().

First, declare a variable in the ctestdlg class: cbitmap m_bmp;

Then we add a picture tag named idc_static1 to the dialog box.

Then:

Bool cdisplaypic: oninitdialog ()

{

Cdialog: oninitdialog ();

If (m_bmp .m_hobject! = NULL) // judge

M_bmp .deleteobject ();

///// // Load the image

Hbitmap hbmp = (hbitmap): LoadImage (AfxGetInstanceHandle (),

"C: \ aaa.bmp", image_bitmap, 0, 0, lr_createdibsection | lr_loadfromfile );

If (hbmp = NULL)

Return false;

/// // The disconnected program is used to obtain the information of the loaded BMP /////// /////////////////

M_bmp .attach (hbmp );

Dibsection Ds;

Bitmapinfoheader & bminfo = Ds. dsbmih;

M_bmp .getobject (sizeof (DS), & Ds );

Int Cx = bminfo. biwidth; // obtain the image width.

Int Cy = bminfo. biheight; // obtain the Image Height.

//////////////////////////////////////// ///////////

/////////// After obtaining the image width and height, we can adapt to the image size, that is, adjust the widget size, make it display an image ///////////////////////////

Crect rect;

Getdlgitem (idc_static1)-> getwindowrect (& rect );

Screentoclient (& rect );

Getdlgitem (idc_static1)-> movewindow (rect. Left, rect. Top, CX, Cy, true); // adjust the size

Return true; // return true unless you set the focus to a control

// Exception: OCX property pages shold return false

}

The image is loaded successfully, and the label size is adjusted. The following figure shows how to draw the image. Open the Class Wizard and reload the wm_paint message.

Void cdisplaypic: onpaint ()

{

//// // Either of the following three conditions may have different effects (only one can exist) ///////////

// Cpaintdc DC (this); // if this sentence is used, the DC of the dialog box is displayed, and the image is displayed in the dialog box.

Cpaintdc DC (getdlgitem (idc_static1); // use this sentence to obtain the DC of the picture control. The image will be drawn on the control.

// Cdc dc;

// Dc. m_hdc =: getdc (null); // if the two statements are used, the DC of the screen is obtained, the image will be drawn on the screen ///////////////////////////////// //////////////////////

Crect rcclient;

Getdlgitem (idc_static1)-> getclientrect (& rcclient );

CDC memdc;

Memdc. createcompatibledc (& DC );

Cbitmap bitmap;

Bitmap. createcompatiblebitmap (& DC, rcclient. Width (), rcclient. Height ());

Memdc. SelectObject (& Bitmap );

Cwnd: defwindowproc (wm_paint, (wparam) memdc. m_hdc, 0 );

CDC maskdc;

Maskdc. createcompatibledc (& DC );

Cbitmap maskbitmap;

Maskbitmap. createbitmap (rcclient. Width (), rcclient. Height (), 1, 1, null );

Maskdc. SelectObject (& maskbitmap );

Maskdc. bitblt (0, 0, rcclient. Width (), rcclient. Height (), & memdc,

Rcclient. Left, rcclient. Top, srccopy );

Cbrush brush;

Brush. createpatternbrush (& m_bmp );

DC. fillrect (rcclient, & brush );

DC. bitblt (rcclient. Left, rcclient. Top, rcclient. Width (), rcclient. Height (),

& Memdc, rcclient. Left, rcclient. Top, srcpaint );

Brush. deleteobject ();

// Do not call cdialog: onpaint () for painting messages

}

Only Kodak can support multiple images in the above four methods, while other methods only support BMP.

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.