简体   繁体   中英

Create rectangle in groupbox or picture control in MFC

I am trying to create a rectangle in a picture control box in the dialog. I can however draw rectangle on the dialog box after button click as of now, but I need to do the same in picture control.

Why do I need to do this? Actually the dimensions of the rectangle will be decided run time, so if the rectangle is on the dialog box, it may override the buttons and edit boxes below, I would want to keep picture control so that even if the dimension of rectangle changes, it does not come out of the picture box.

I am developing in VC++ with MFC Any help appreciated. Thanks! :)

The technique you need is called subclassing. You create a class derived from CStatic (I'll call it CYourDerivedStatic) and you add a message handler in that class for the WM_PAINT message. Then you can paint anything you want in the OnPaint message handler.

void CYourDerivedStatic::OnPaint()
{
 CPaintDC dc(this);
 dc.FillSolidRect(...);
}

To attach your custom class to the picture box control first you right-click on the control in the dialog editor and "Add Variable". This gives you a CStatic member variable in the dialog class. Then edit the "CStatic" to "CYourDerivedStatic" and #include "YourDerivedStatic.h".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM