Search code examples
visual-c++mfcdynamic-controls

MFC - Get id of dynamically created rectangle


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.


Solution

  • 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.