Question about painting and scrolling in wxWidgets!!
-
So this isn't really Visual C++ but still I hope it's ok I "pollute" the board :) I need some help with some paint-related issues. I'm trying to build a simple image viewer/editor - just for fun. I have some problems when painting the bitmap though. If the bitmap is smaller than the client area it works nice; it gets painted in the center of the area as I want. If the bitmap is bigger than the client area both horizontally and vertically it also works beautifully. I can use the scrollbars in both directions and the image scrolls correctly. The problem arises when the image is larger than the client area in only one of the two dimensions. In this case when I scroll I only get to see the background; in other words the newly exposed image area doesn't repaint when scrolling. My pain handler is as follows:
void MyScrolledWindow::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);// Only paint if we have a valid bitmap if(m\_theBitmap && m\_theBitmap->Ok()) { // Get the outer bounds of the region of the window that has been damaged. const wxRect rectUpdate = GetUpdateRegion().GetBox(); // Translate the device coordinates of the upper left corner of the invalid region to logical coordinates. const wxPoint ptVirt = CalcUnscrolledPosition(rectUpdate.GetTopLeft()); // The memory device context we are using for double buffering. wxMemoryDC mdc; // Select the bitmap into the device context so we are ready for double buffering. mdc.SelectObject(\*m\_theBitmap); // Get the size of the client area. const wxRect rcClient = GetClientSize(); // This will be the point of where to draw the upper left corner of our image. wxPoint upperLeft; // If the client area is bigger than the bitmap in either width or height... if(rcClient.GetWidth() > m\_theBitmap->GetWidth() || rcClient.GetHeight() > m\_theBitmap->GetHeight()) { // ...draw the background... PaintBackground(dc); // ... and get the width and height of the image... wxCoord w = m\_theBitmap->GetWidth(); wxCoord h = m\_theBitmap->GetHeight(); // ...so we can use that for calculating the position of the upper left corner of the image. int x = wxMax(0, (rcClient.width-w)/2); int y = wxMax(0, (rcClient.height-h)/2); //
-
So this isn't really Visual C++ but still I hope it's ok I "pollute" the board :) I need some help with some paint-related issues. I'm trying to build a simple image viewer/editor - just for fun. I have some problems when painting the bitmap though. If the bitmap is smaller than the client area it works nice; it gets painted in the center of the area as I want. If the bitmap is bigger than the client area both horizontally and vertically it also works beautifully. I can use the scrollbars in both directions and the image scrolls correctly. The problem arises when the image is larger than the client area in only one of the two dimensions. In this case when I scroll I only get to see the background; in other words the newly exposed image area doesn't repaint when scrolling. My pain handler is as follows:
void MyScrolledWindow::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);// Only paint if we have a valid bitmap if(m\_theBitmap && m\_theBitmap->Ok()) { // Get the outer bounds of the region of the window that has been damaged. const wxRect rectUpdate = GetUpdateRegion().GetBox(); // Translate the device coordinates of the upper left corner of the invalid region to logical coordinates. const wxPoint ptVirt = CalcUnscrolledPosition(rectUpdate.GetTopLeft()); // The memory device context we are using for double buffering. wxMemoryDC mdc; // Select the bitmap into the device context so we are ready for double buffering. mdc.SelectObject(\*m\_theBitmap); // Get the size of the client area. const wxRect rcClient = GetClientSize(); // This will be the point of where to draw the upper left corner of our image. wxPoint upperLeft; // If the client area is bigger than the bitmap in either width or height... if(rcClient.GetWidth() > m\_theBitmap->GetWidth() || rcClient.GetHeight() > m\_theBitmap->GetHeight()) { // ...draw the background... PaintBackground(dc); // ... and get the width and height of the image... wxCoord w = m\_theBitmap->GetWidth(); wxCoord h = m\_theBitmap->GetHeight(); // ...so we can use that for calculating the position of the upper left corner of the image. int x = wxMax(0, (rcClient.width-w)/2); int y = wxMax(0, (rcClient.height-h)/2); //
Nevermind, I just solved it. That's how good you guys are - I can sense the solution just thinking about you :):)
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand