Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC VC VC++
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?
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 2-May-12 1:08am
chaiein1.8K
Edited 2-May-12 19:08pm
v4
Comments
SAKryukov at 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 at 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 at 3-May-12 0:37am
   
My dialog is 539X394 and bitmap is 798*675
chaiein at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
chaiein at 3-May-12 5:18am
   
That is what exactly i need now but how to check?
Richard MacCutchan at 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 at 4-May-12 3:18am
   
which is the command to get size of the screen maximum?
Richard MacCutchan at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 853
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 166
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 3 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100