Re: Can't copy the background of my simple control

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I would recommend using Regions. Create a region that is the same shape as
the icon you are trying to drag around, and set your window region to that
region. That way you don't have to worry about the background.

AliR.


"Maik Wiege" <mswiege-nospan-@xxxxxx> wrote in message
news:44565eea$0$22101$9b622d9e@xxxxxxxxxxxxxxxxxx
Hello!
I made a simple control that should simply paint a dragable circle on
the screen, but I cant get the surrounding of the circle to become the
correct background of the parent. Here is what I've done:
I crated a MainFrame, View project (for Windows Mobile, but that should
be the same for win32) with the wizard. In the ChildView derived from
CWnd:
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
svd = new CSimpleCircleDrawer();
svd->Create(NULL,_T(""),WS_VISIBLE | WS_CHILD,
CRect(10,10,30,30), this, 1234);
return 0;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect(rect);
CBrush brush(RGB(50, 50, 250));
dc.FillRect(rect, &brush);
}


And my CSimpleCircleDrawer class:

BEGIN_MESSAGE_MAP(CSimpleCircleDrawer, CWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

void CSimpleCircleDrawer::OnPaint()
{
CPaintDC dc(this); // device context for painting

//Get the parents background
CWnd* pParent = GetParent();
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
pParent->ScreenToClient(&rect);
CDC *pDC = pParent->GetDC();
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap m_Bmp;
CBitmap *pOldbmp = memdc.SelectObject(&m_Bmp);
memdc.BitBlt(0,0,rect.Width(),rect.Height(),pDC,rect.left,
rect.top,SRCCOPY);
pParent->ReleaseDC(pDC);

// copy the bitmap to the memdc
memdc.CreateCompatibleDC(&dc);
CBitmap memBmp;
memBmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
memdc.SelectObject(&memBmp);

//paint the background bitmap on the memory DC
dc.SelectObject(&m_Bmp);
memdc.BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
dc.SelectObject(pOldbmp);

// now draw a simple circle
dc.Ellipse(0,0,20,20);
}


void CSimpleCircleDrawer::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
m_bCaptured = TRUE;
m_nDragOffset = point;

CWnd::OnLButtonDown(nFlags, point);
}

void CSimpleCircleDrawer::OnLButtonUp(UINT nFlags, CPoint point)
{
ReleaseCapture();
m_bCaptured = FALSE;
CWnd::OnLButtonUp(nFlags, point);
}

void CSimpleCircleDrawer::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pParent = GetParent();

//Retrieve the old postion (in parent client coordinates)
CRect oldPos;
GetClientRect(&oldPos);
ClientToScreen(oldPos);
pParent->ScreenToClient(&oldPos);

//Work out the new position (in parent client coordinates)
CRect newPos(point.x - m_nDragOffset.x, point.y - m_nDragOffset.y,
point.x - m_nDragOffset.x + m_nWidth,
point.y - m_nDragOffset.y + m_nHeight);
ClientToScreen(&newPos);
pParent->ScreenToClient(&newPos);

//Only move the vertex if it is inside the client rect of the
parent
CRect clientRect;
pParent->GetClientRect(&clientRect);
if (newPos.left >= clientRect.left && newPos.top >=
clientRect.top && newPos.right < clientRect.right &&
newPos.bottom < clientRect.bottom)
{
// update the drawing
MoveWindow(newPos, FALSE);
pParent->InvalidateRect(oldPos, FALSE);
Invalidate(TRUE);
}
CWnd::OnMouseMove(nFlags, point);
}

So what is wrong here? The circle is painted fine and I can drag it
around, but the surrounding of the circle is not painted blue as it
should because the parent is blue.

Thanks for any help
Maik


.



Relevant Pages

  • Cant copy the background of my simple control
    ... I made a simple control that should simply paint a dragable circle on the screen, but I cant get the surrounding of the circle to become the correct background of the parent. ... CRect rect; ... //Work out the new position (in parent client coordinates) ...
    (microsoft.public.vc.mfc)
  • Re: Use a DC in a worker thread
    ... opportunities to update the progress bar, one for each little piece you did. ... typedef void ... CWnd * parent = NULL); ... static void SetRange; ...
    (microsoft.public.vc.mfc)
  • Re: Multiple Lines with a FlowLayout within a SOUTH region in a BorderLayout
    ... public static void main{ ... JPanel mainPanel = new JPanel; ... public void addLayoutComponent{ ... public Dimension preferredLayoutSize(Container parent) { ...
    (comp.lang.java.gui)
  • Re: [RFC][PATCH 3/3] sched: On-demand tg_shares_up()
    ... wide tree with a small number of active groups it should be a win. ... -static void update_shares(long cpu) ... If it has a parent, insert it immediately before the parent in the list. ...
    (Linux-Kernel)
  • Re: Threading Faux Pa
    ... the Parent class data from the database and loads all the objects. ... a TCPClient that is wrapped in a 'communicator' class. ... public class ObjectModelController ... private void LoadObjectModel() ...
    (microsoft.public.dotnet.languages.csharp)