• 0

Docking windows in Visual C++ .NET


Question

6 answers to this question

Recommended Posts

  • 0

1. In you .H file add in the following line

<pre>afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndPos);</pre>

after the Generated message map functions part.

2. In your BEGIN_MESSAGE_MAP section in you .CPP file add the line

<pre>ON_WM_WINDOWPOSCHANGING()</pre>

3. Then do the following

<pre>

void CMyTestDlg:: OnWindowPosChanging(WINDOWPOS* lpwndPos)

{

	//Get Desktop Size

	CRect rectScreen;

	SystemParametersInfo(SPI_GETWORKAREA, 0, &rectScreen, 0);



	//Screen Offsets for Snap

	int m_nXOffset = 20;

	int m_nYOffset = 20;



	//Min & Max X Position

	if (lpwndPos->x < 0)

  lpwndPos->x = 0;

	else if (lpwndPos->x + lpwndPos->cx > rectScreen.right)

  lpwndPos->x = rectScreen.right - lpwndPos->cx;



	//Min & Max Y Position

	if (lpwndPos->y < 0)

  lpwndPos->y = 0;

	else if (lpwndPos->y + lpwndPos->cy > rectScreen.bottom)

  lpwndPos->y = rectScreen.bottom - lpwndPos->cy;



	int nGapX = 0;



	//X Snap

	if (abs(lpwndPos->x - rectScreen.left) <= m_nXOffset)

	{

  nGapX = lpwndPos->x - rectScreen.left;

  lpwndPos->cx+= nGapX;

  lpwndPos->x = rectScreen.left;

	}

	else if (abs(lpwndPos->x + lpwndPos->cx - rectScreen.right) <= m_nXOffset)

	{

  nGapX = rectScreen.right - (lpwndPos->x + lpwndPos->cx);

  lpwndPos->cx+= nGapX;

  lpwndPos->x = rectScreen.right - lpwndPos->cx;

	}



	//Y Snap

	if (abs(lpwndPos->y - rectScreen.top) <= m_nYOffset)

  lpwndPos->y = rectScreen.top;

	else if (abs(lpwndPos->y + lpwndPos->cy - rectScreen.bottom) <= m_nYOffset)

  lpwndPos->y = rectScreen.bottom - lpwndPos->cy;

}

</pre>

  • 0

Ok I copied the above code into a new project, and it worked OK. I have put a link to the zip file onto my site: Test Project.

One thing I did notice, was when the dialog gets too close to the left or right side of the screen, the dialog increases in size (this was needed for the client, but I forgot to remove it here).

The code should be as follows:

<pre>

	//X Snap

	if (abs(lpwndPos->x - rectScreen.left) <= m_nXOffset)

	{

//  nGapX = lpwndPos->x - rectScreen.left;

//  lpwndPos->cx+= nGapX;

  lpwndPos->x = rectScreen.left;

	}

	else if (abs(lpwndPos->x + lpwndPos->cx - rectScreen.right) <= m_nXOffset)

	{

//  nGapX = rectScreen.right - (lpwndPos->x + lpwndPos->cx);

//  lpwndPos->cx+= nGapX;

  lpwndPos->x = rectScreen.right - lpwndPos->cx;

	}

</pre>

If it still does not work, or if you have any other questions drop me an email, or PM.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.