Re: Clickable images on a dialog
Right, the code is working up to a point. What it does is this:
depenending on the press of a radio button, the screen is segmented
into one of three modes: full screen, quarters or 1/16ths. The user can
then drill down further by selecting one of the zones on the screen,
which displays dashed red lines: the selected zone should then be
greyed out to denote selection.
The algorithm for determing key presses and finding out which zone the
user clicks on works fine. But when the user clicks on a cell, it
either ungreys all those cells before the clicked one, or it does
nothing - in fact, during a test, a completely grayed out screen (which
was in 1/16th mode, so only one cell should be greyed at all) suddenly
turned white - ie deselected!
My code is below. Can anyone see what is wrong?
Thanks!
void CStaticDraw::OnPaint()
{
CRect image_location;
GetClientRect( &image_location ); // find out the size of the window
CDC* pDC = GetDC();
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, image_location.Width(),
image_location.Height());
CBitmap* pOldBitmap = memDC.SelectObject((CBitmap*)&bitmap);
//
//--- At this point you can draw on your bitmap using memDC
//
CBrush brush;
brush.CreateSolidBrush(RGB(255, 255, 255));
memDC.SelectObject( &brush );
memDC.Rectangle(0, 0, image_location.Width(), image_location.Height()
);
// The screen is initialised to white
// this is also the default unselected view
// Now draw the selected area on screen - user will be able to click
on the ROI.
CPen pen( PS_DASH, 0, RGB(255,0,0)); // The dashed areas on the screen
// are red
memDC.SelectObject( &pen );
int x, y;
int row_counter = 0, column_counter = 0;
// We are now looking for m_Selected Zone: this is a zero indexed
number denoting
// the region the user is interested in: e.g. for the 1/16ths mode, the
indices can
// range from 0 to 15
switch( m_ZoomDetectionMode ) // Full screen, quarters or 1/16ths?
{
case Full_Screen :
if (m_Selected_Zone == 0)
{
brush.CreateSolidBrush( RGB(211,211,211));
memDC.SelectObject( &brush );
memDC.Rectangle(0 ,0 , image_location.Width(),
image_location.Height() );
} // If the screen is selected, color it in in grey
break;
case Sixteenth_Screen:
for( y = 0; y <= 3*image_location.Height()/4; y+=
image_location.Height()/4 )
{
column_counter = 0;
for( x = 0; x <= 3*image_location.Width()/4; x+=
image_location.Width()/4 )
{
memDC.Rectangle(x, y, x + image_location.Width()/4, y +
image_location.Height()/4);
// This code draws four rectangles in the main screen; the rectangles
are red and
// dashed
if ((row_counter*4) + column_counter == m_Selected_Zone ) // We
now determine, using the counters
// whether the zone we have looped "into" is the selected zone. If
so.....
{
brush.CreateSolidBrush( RGB(211,211,211));
memDC.SelectObject( &brush );
memDC.Rectangle(x, y, x + image_location.Width()/4, y +
image_location.Height()/4 );
// Alright, we have now greyed it
}
column_counter++;
}
row_counter++;
}
break;
case Quarter_Screen: // Do the same for the 1/4 screen segmentation
for( y = 0; y <= image_location.Height()/2; y+=
image_location.Height()/2 )
{
column_counter = 0;
for( x = 0; x <= image_location.Width()/2; x+=
image_location.Width()/2 )
{
memDC.Rectangle(x, y, x + image_location.Width()/2, y +
image_location.Height()/2);
if ((row_counter*2) + column_counter == m_Selected_Zone )
{
brush.CreateSolidBrush( RGB(211,211,211));
memDC.SelectObject( &brush );
memDC.Rectangle(x, y, x + image_location.Width()/2, y +
image_location.Height()/2 );
}
column_counter++;
}
row_counter++;
}
break;
case No_Mode_Selected:
// Do nothing - no mode selected
break;
default:
// Do nothing - no mode selected
break;
}
pDC->BitBlt(0, 0, image_location.Width(), image_location.Height(),
&memDC, 0, 0, SRCCOPY); // Blt the whole lot; white background, red
rectangles,
// and selected grey rectangle to the screen
memDC.SelectObject((CBitmap*)pOldBitmap);
ReleaseDC(pDC);
}