-1

As part of learning MFC i came across a situation where i create a rectangle dynamically in the OnPaint() of a dialog class something like this.

CPaintDC dc(this);
dc.Rectangle(10,10,208,92);

I want to get the device id of this rectangle from another function. The other function is the BtClick event function in the same dialog class.

void ThreadDialog::OnBnClickedButton3()
{
    CWnd* pWnd = FromHandle(dlg.m_hWnd);
    CDC* pDC = pWnd->GetDC  ();
  /* Here i wanted to get the device context of the rectangle drawn in OnPaint() */

}

So first i need the control id of the rectangle which was dynamically created so that after that i will be able to get device context to that rectangle.

Please help how to do it.

  • 3
    How did you create the rectangle? You'll need to show some code as a "rectangle" typically does not have a control ID. – rrirower Aug 06 '15 at 12:06
  • Edited the question with the code used to create rectangle. Now wont it have a control ID? if not how do i get the device context of it to draw inside it? – Deepak Selvakumar Aug 06 '15 at 12:15
  • Where does the other function "live"? And, will it have the same coordinates as what you've shown above? – rrirower Aug 06 '15 at 12:29
  • For most applications you would just use the same dc that you used to draw the rectangle, and you would draw inside the rectangle in OnPaint. To make it draw from another function that function should call Invalidate() on the window. That will cause a new WM_PAINT to be generated. – ScottMcP-MVP Aug 06 '15 at 14:33
  • @Scott..So i should draw inside the rectangle with the same dc used to draw the rectangle ? but i should draw within the dimensions of the rectangle . is it ? – Deepak Selvakumar Aug 07 '15 at 05:37

1 Answers1

3

CDC::Rectangle() doesn't create anything, it just draws a rectangle on that DC. There is no ID or device context associated with that drawing.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27