Display image data statically Based on the MFC Control

Source: Internet
Author: User

Display image data based on the MFC control static. add getdlgitem (idc_my_pic)-> modifystyle (ss_typemask, ss_ownerdraw) to oninitdialog; 2. add the following to the butto button to display the void cdlgpicdlg: ontest () {// todo: add your command handler code here cfiledialog filedlg (true, null, null, ofn_allowmultiselect, _ T ("picture files (*. BMP *. ICO *. JPG) | * BMP ;*. ICO; * JPG | all files (*. *) | *. * | "), afxgetmainwnd (); cstring pathname; If (filedlg. domod Al () = idok) {position MPOs = filedlg. getstartposition (); While (MPOs! = NULL) {pathname = filedlg. getnextpathname (MPOs); If (m_pict.loadpicture (pathname. getbuffer (pathname. getlength () {// prevents transparent images and eliminates the existing image information cpaintdc (this); // device context for painting crect RC; getdlgitem (idc_my_pic) -> getwindowrect (& rc); DC. fillsolidrect (RC, RGB (0, 0, 0); invalidate () ;}}} 3. add the notify message ing function to the parent window to draw the control void cdlgpicdlg: ondrawitem (INT nidctl, lpdrawitemstruct detail) in the ondrawitem (INT nidctl, lpdrawitemstruct detail) Message ing function in: add your message handler code here and/or call default if (nidctl = idc_my_pic) {If (m_pict.m_pic) {rect; trace ("-- hell -- Test ---"); hwnd =: getdlgitem (this-> getsafehwnd (), idc_my_pic); HDC =:: getdc (: getdlgitem (this-> getsafehwnd (), idc_my_pic )); // get the DC for the cpicture box: getclientrect (: getdlgitem (this-> getsafehwnd (), idc_my_pic), & rect ); // get dimensions of IT m_pict.drawpicture (HDC, 0, 0, rect. right-rect. left, rect. bottom-rect. top);: releasedc (: getdlgitem (this-> getsafehwnd (), idc_my_pic), HDC) ;}} cdialog: ondrawitem (nidctl, lpdrawitemstruct);} conclusion: if the image needs to be displayed in static mode and the painting in onpaint is ineffective, the painting should be as follows: 1) Add the ss_ownerdraw attribute getdlgitem (idc_my_pic)-> modifystyle (ss_typemask, ss_ownerdraw) to the static ); 2) Add the on_wm_drawitem message ing function to the parent window. 3) Draw controls in the ondrawitem (INT nidctl, lpdrawitemstruct) Message struing function, for example: the following is a blue background void cmydlg: ondrawitem (INT nidctl, lpdrawitemstruct) {If (nidctl = idc_static_draw) {CDC * PDC = CDC :: fromhandle (lpdrawitemstruct-> HDC); PDC-> fillsolidrect (10, 10, lpdrawitemstruct-> rcitem. right-20, lpdrawitemstruct-> rcitem. bottom-20, RGB (0, 0,255); return;} cdialog: ondrawitem (nidctl, lpdrawitemstruct);} You can also inherit a new class from cstatic, then add wm_paint message processing and draw the following image in onpaint: Class cmystatic: Public cstatic {... afx_msg void onpaint () ;}; void cmystatic: onpaint () {cpaintdc DC (this); // device context for painting crect RC; getclientrect (RC); DC. fillsolidrect (RC, RGB (0, 0,255 ));}

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.