65.9K
CodeProject is changing. Read more.
Home

Button behavior - single button multiple behaviour

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (2 votes)

May 15, 2010

CPOL
viewsIcon

7351

You can use one single button for more than one behaviour.Here is how.If you have the coordinates of a point in device coordinates and want to find the corresponding position in logcal view - use CDC::DPtoLP to convert the device coordinates to logcal coordinates.Call on...

You can use one single button for more than one behaviour. Here is how. If you have the coordinates of a point in device coordinates and want to find the corresponding position in logcal view - use CDC::DPtoLP to convert the device coordinates to logcal coordinates. Call on OnPrepareDC first to set the mapping mode and factor. For example: Here is a WM_LEFTBUTTONDOWN handler that performs a simple hit test to determine whether the click point lies in the upper or lower half of the logcal view. CPoint objects passed to OnLButtonDown and other mouse message handlers always contain device coordinates so conversion is essetial.
void CMyView::OnLButtonDown(UINT nFlags, CPoint Point)
{
  CPoint pos=Point;
  CClientDC dc (this);
  OnPrepareDC(&dc);     
  dc.DPtoLP(&pos);
  CSize size=GetTotalSize();

  if(::abs( pos.y ) < (size.cy/2) )
  {
    //Upper half was clicked
  }
   else
  {
    //lower half was clicked
  }
 

}
...