Click here to Skip to main content
15,886,812 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have used the following codes to move window and paint the background window now when I move window to edges of the screen the background of my application which is a picture of bitmap type gets disturbed.How to solve this problem?
C++
void CDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	 CRect myRect;
	 GetWindowRect(&myRect);
              //save current cursor coordinate
		GetCursorPos(&point);
		//ScreenToClient(&point);
		SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
	__super::OnLButtonDown(nFlags, point);
}
 
void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CRect myRect;
	 GetWindowRect( &myRect);
	 CPoint curpoint;
	 GetCursorPos(&curpoint);
     {     
           MoveWindow(curpoint.x - point.x, curpoint.y - point.y, 
			myRect.right - myRect.left, myRect.bottom - myRect.top, TRUE);
     }
	
	 __super::OnMouseMove(nFlags, point);
}
void CDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		
		CPaintDC dc(this);
		CRect rect;
		GetClientRect(&rect);
		//ScreenToClient(rect);
		BITMAP bmp;
		HBITMAP hBmp = ::LoadBitmap(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_MAIN_BG));
		::GetObject(hBmp, sizeof(bmp), &bmp);
		HDC hDC = ::CreateCompatibleDC(NULL);
		SelectObject(hDC, hBmp);
		::BitBlt(dc.m_hDC, 0, 0, rect.Width(), rect.Height(), hDC, 0, 0, SRCCOPY);
		CDialog::OnPaint();
	}

}



Its causing problem when I move the app with mouse event out side the screen Limits. How can I stop the app edges crossing the screen limits.
Posted
Updated 2-May-12 19:08pm
v4
Comments
Sergey Alexandrovich Kryukov 2-May-12 10:50am    
What kind of disturbing? You might need optimized double buffering, a usual technique against flicker, but is it really flicker?
--SA
Code-o-mat 2-May-12 15:07pm    
Are you sure your window's client rect isn't wider or taller than the bitmap? I think if you specify a bigger width or height than the source bitmap's dimensions when using BitBlt like that it will fail.
On a sidenote: you are leaking GDI resources since you never call DeleteObject on the bitmap and DeleteDC on the compatible DC. You also don't save the original bitmap handle that was selected into the compatible DC when selecting the bitmap and don't reselect it (you'd do that before deleting the DC or the bitmap).
chaiein 3-May-12 0:37am    
My dialog is 539X394 and bitmap is 798*675
chaiein 3-May-12 1:05am    
Even if i increased the dialog size to 798*675 problem exist. Its causing problem when I move the app with mouse event out side the screen Limits. How can I stop the app edges crossing the screen limits.

1 solution

You need to add a check in your CDemoDlg::OnMouseMove() function to see if any of your windows edges go beyond the edge of the screen and adjust them back inside if necessary.
 
Share this answer
 
Comments
chaiein 3-May-12 5:18am    
That is what exactly i need now but how to check?
Richard MacCutchan 3-May-12 6:08am    
Given that you know the new size of your window and you can find the size of your screen you just need to check that the top and left values are not less than zero and the bottom and right are not greater than the screen maximum. It's simple mathematics.
chaiein 4-May-12 3:18am    
which is the command to get size of the screen maximum?
Richard MacCutchan 4-May-12 5:29am    
Use the GetSystemMetrics() call, with the SM_CXSCREEN and SM_CYSCREEN parameters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900