Solved

Drawing inside a control (picturebox) - VC++ 5.0

Posted on 1998-03-18
9
521 Views
Last Modified: 2012-05-05
The following code draws a bunch of solid boxes in a picturebox on a dialog, however the drawing goes outside the bounds of the picture box (and actually draws onto the dialog itself). Why is this? Eventually I want to attach scrollbars to the box to view any drawing that are larger than the box.
 
**************************
void ce05cDialog::OnPaint()
{
      CPaintDC dc(this); // device context for painting
      //Add your message handler code here
      int x1 = 0,y1 = 0,x2 = 7.5,y2 =7.5,i;
      
      CBrush brushsolid1(RGB(255,0,0));
      CBrush brushsolid2(RGB(0,0,255));
      
      CDC* pControlDC = m_picbox1.GetDC();
      
      m_picbox1.Invalidate();
      m_picbox1.UpdateWindow();
      pControlDC->SelectObject(brushsolid1);
      
      for (i = 1; i <=10; i++)
      {      
            pControlDC->Rectangle(x1,y1,x2,y2);
            x1 = x1 + 7.5;
            y1 = y1 + 7.5;
            x2 = x2 +7.5;            
            y2 = y2 +7.5;
      }

      pControlDC->SelectObject(brushsolid2);
      x1 = 0;
      y1 = 40;
      x2 = 7.5;
      y2 =47.5;

      for (i = 1; i <=10; i++)
      {      
            pControlDC->Rectangle(x1,y1,x2,y2);
            x1 = x1 + 7.5;
            y1 = y1 - 7.5;
            x2 = x2 + 7.5;            
            y2 = y2 - 7.5;
      }
      

      m_picbox1.ReleaseDC(pControlDC);
}
0
Comment
Question by:englm
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1183628
You created a device context for the dialog window, not the control (Picturebox) window.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183629
Opps I'm wrong!  You are creating a DC for the dialog, but then you create and use a DC for the control, which is correct.

Sorry.


Interesting.  I don't see what is wrong.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183630
Do you know for a fact that the drawing is occuring outside of the control window.  Is it possible that the control window is larger than you think?  (Assuming it doesn't have a visible border that would make this a stupid question?)
0
 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
ID: 1183631
You should try using double-buffering to draw first on a memory DC and flip it on using BitBlt or StretchBlt.  It will not only fix your problem but will also make the drawing much smoother, without flickery and visible drawing sequence.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Expert Comment

by:nietod
ID: 1183632
That will make the drawing appear without flickering, but there is no reason that it should prevent this problem, if fact there is no reason this problem should occur.  A DC created for a window should not be able to draw outside of the window.  If it does there is something wrong.  If you can use the DC to draw a rectangle outside of a window, then you can probably use it to BitBlt() outside of a window.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183633
Dialog's controls will have CS_PARENTDC as one of its class style's flag, therefore it will draw on parent's DC:
----------from MSVC HELP----------
CS_PARENTDC      Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the system's cache of device contexts. It does not give the child the parent's device context or device context settings.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183634
By the way, BitBlt() will not cause drawing on outside of its bouding rectangle.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183635
Or if you don't want to bother with double-buffering technique, be sure to set the clipping rectangle of the control window yourself to the control's client boundary.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183636
I see.  I do my own dialogs, so I wasn't aware that dialog controls use the CS_PARENTDC style.  That seems very unfriendly, I guess that must be faster.  However, that means the only reliable way to not draw on the parent is to change the DC's clipping rectangle/region.  Just like the other drawing procedures BitBlt() WILL anywhere inside the DC that you tell it so, so you must either draw only at the coordinates that are inside the control (which can be inconvenient) or you must set the clipping rectangle to the control's coordinates and then draw freely.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now