3 Replies - 556 Views - Last Post: 05 October 2012 - 09:51 PM Rate Topic: -----

#1 Hooor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 26-May 11

problem with scroll bar "the window content get repeated"

Posted 05 October 2012 - 03:22 PM

hi I am learning C++ programming with windows and follow book to do that

I wrote program to draw rectangle by draw lines

the program was working correctly but when I add the scroll bar code I got a problem when I move
the scroll bar the rectangle get repeated in the whole window

Note: I took the scroll bar code from the book and I add it here to try it because I will need it in other program

could you tell me what is the reason for that ??


#include <windows.h>
#include <math.h>



LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)

{
static TCHAR szAppName[] = TEXT ("SineWave") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"), szAppName, MB_IConerror) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("My prog"),
WS_OVERLAPPEDWINDOW| WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, SW_SHOWMAXIMIZED) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}



LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, iVertPos, iHorzPos,cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
HDC hdc ;
PAINTSTRUCT ps ;
SCROLLINFO si ;
TEXTMETRIC tm ;
TCHAR szBuffer[10] ;

switch (message)
{
	case WM_CREATE:

hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
// Save the width of the three columns
iMaxWidth = 40 * cxChar + 22 * cxCaps ;
return 0 ;


case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
// Set vertical scroll bar range and page size
si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = 500  ;
si.nPage = cyClient / cyChar ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
// Set horizontal scroll bar range and page size
si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = 500 ;
si.nPage = cxClient / cxChar ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
return 0 ;

//....................................


case WM_VSCROLL:
// Get all the vertical scroll bar information
si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si) ;
// Save the position for comparison later on
iVertPos = si.nPos ;
switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ;
case SB_BOTTOM:
si.nPos = si.nMax ;
break ;
case SB_LINEUP:
si.nPos -= 1 ;
break ;
case SB_LINEDOWN:
si.nPos += 1 ;
break ;
case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;
case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;
default:
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.
si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hwnd, SB_VERT, &si) ;
// If the position has changed, scroll the window and update it
if (si.nPos != iVertPos)
{
ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos),
NULL, NULL) ;
UpdateWindow (hwnd) ;
}
return 0 ;

//............................................


case WM_HSCROLL:
// Get all the vertical scroll bar information
si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
// Save the position for comparison later on
GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ;
switch (LOWORD (wParam))
{
case SB_LINELEFT:
si.nPos -= 1 ;
break ;
case SB_LINERIGHT:
si.nPos += 1 ;
break ;
case SB_PAGELEFT:
si.nPos -= si.nPage ;
break ;
case SB_PAGERIGHT:
si.nPos += si.nPage ;
break ;
case SB_THUMBPOSITION:
si.nPos = si.nTrackPos ;
break ;
default :
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.
si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
GetScrollInfo (hwnd, SB_HORZ, &si) ;
// If the position has changed, scroll the window
if (si.nPos != iHorzPos)
{
ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0,
NULL, NULL) ;
}
return 0 ;


//........................


case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

//....................
// Get horizontal scroll bar position
//GetScrollInfo (hwnd, SB_HORZ, &si) ;
//iHorzPos = si.nPos ;
//...........................


//SelectObject (hdc,  GetStockObject (WHITE_PEN)) ;
//SetMapMode (hdc, MM_LOMETRIC) ;

MoveToEx (hdc, 0, 0, NULL) ;
LineTo (hdc, 290,0) ;

MoveToEx (hdc, 290, 0, NULL) ;
LineTo (hdc, 290,200) ;

MoveToEx (hdc, 290,200, NULL) ;
LineTo (hdc, 0,200) ;

MoveToEx (hdc, 0,200, NULL) ;
LineTo (hdc, 0,0) ;

//Rectangle (hdc, -100, -100, 0, 0) ;
EndPaint (hwnd, &ps) ;
return 0 ;


case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}


return DefWindowProc (hwnd, message, wParam, lParam) ;
}



I attatched print screen for it

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by Hooor: 05 October 2012 - 03:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with scroll bar "the window content get repeated"

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3164
  • View blog
  • Posts: 9,559
  • Joined: 05-May 12

Re: problem with scroll bar "the window content get repeated"

Posted 05 October 2012 - 03:44 PM

Since you are doing your own painting in your WM_PAINT, you are also responsible for erasing your own background area. So you'll have to do something like a FillRect() with the brush set to your background color.
Was This Post Helpful? 0
  • +
  • -

#3 Hooor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 26-May 11

Re: problem with scroll bar "the window content get repeated"

Posted 05 October 2012 - 04:56 PM

I did add this part in the WM_PAINT case
but also I have the same problem
when I minimize the window it does as it should do
the problem occur only if I use the vertical/horizontal scroll bar
RECT rect; // defined outside the case

SetRect (&rect, 0, 0, cxClient, cyClient) ;  
FillRect (hdc, &rect, (HBRUSH)BLACK_BRUSH) ; 



Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: problem with scroll bar "the window content get repeated"

Posted 05 October 2012 - 09:51 PM

View PostHooor, on 05 October 2012 - 11:56 PM, said:

I did add this part in the WM_PAINT case
but also I have the same problem
when I minimize the window it does as it should do
the problem occur only if I use the vertical/horizontal scroll bar
RECT rect; // defined outside the case

SetRect (&rect, 0, 0, cxClient, cyClient) ;  
FillRect (hdc, &rect, (HBRUSH)BLACK_BRUSH) ; 




Hi,

First of all I wouldn't use ScrollWindow for this better using ScrollWindowEx as ScrollWindow is primarily used
in scrolling text also UpdateWindow is a bit hit and miss better off using InvalidateRect
then you need to update WM_PAINT with the new rectangele coords everytime you scroll you should be updating the rectangle coords to a new postion and of course erasing either a portion of the screen or the entire screen.

Your going to want to set up a RECT to hold the co-ords of your rectangle.

then in your scroll routines your going to have to update them something like this

case SB_LINELEFT:
si.nPos -= 1 ;
r.left = r.left -1;
r.right = r.right -1;
break ;
case SB_LINERIGHT:
si.nPos += 1 ;
r.left = r.left + 1;
r.right = r.right + 1;
break ;



Regards

Snoopy.

This post has been edited by snoopy11: 05 October 2012 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1