webForumDet fria alternativet

Scroll bar

0 svar · 227 visningar · startad av nowb

nowbMedlem sedan juli 20053 inlägg
#1

Jag har gjort ett fönster med en scrollbar och två knappar, en "exit" och en "test". Exit knappen stänger fönstret och test knappen lägger till en rad med text, när man tryckt så pass många gånger på test så att fönstret är fullt aktiveras scrollen och man kan rulla upp och ner med scrollen, men det blir inte snyggt. När man scrollar överlappar texter och knappar varandra så det blir helt klöddigt. När man ändrar storlek på fönstret hoppar också knapparna till nya lägen.

här är koden:


#include <windows.h>  

HINSTANCE hInst;  

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application"; 

#define IDM_EXIT          100
#define IDM_TEST          200

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

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
  MSG      msg;
  HWND    hWnd; 
  WNDCLASS wc;

  wc.style        = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc  = (WNDPROC)WndProc;      
  wc.cbClsExtra    = 0;                      
  wc.cbWndExtra    = 0;                      
  wc.hInstance    = hInstance;              
  //wc.hIcon        = LoadIcon( hInstance, lpszAppName ); 
  //wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon        = NULL; 
  wc.hCursor      = NULL;
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName  = lpszAppName;              
  wc.lpszClassName = lpszAppName;              

  RegisterClass(&wc);

  hInst = hInstance; 

  hWnd = CreateWindow( lpszAppName, 
                        lpszTitle,    
                        WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
                        100, 100, 
                        300, 200,  
                        NULL,              
                        NULL,              
                        hInstance,        
                        NULL              
                      );

  ShowWindow( hWnd, nCmdShow ); 
  UpdateWindow( hWnd );        

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

  return( msg.wParam ); 
}

VOID Scroll( HWND hWnd, int* pnCurPos, WORD wScroll )
{
  SCROLLINFO si;

  si.cbSize = sizeof( SCROLLINFO );
  si.fMask  = SIF_PAGE | SIF_RANGE | SIF_POS;
  GetScrollInfo( hWnd, SB_VERT, &si );

  switch( wScroll )
  {
      case SB_LINEDOWN :
              if ( *pnCurPos <= (int)(si.nMax - si.nPage) )
                *pnCurPos += 1;
              break;

      case SB_LINEUP :
              if ( *pnCurPos > 0 )
                *pnCurPos -= 1;
              break;
  }

  if ( si.nPos != *pnCurPos )
  {
      RECT cltRect;

      GetClientRect( hWnd, &cltRect );

      ScrollWindowEx( hWnd, 0, (si.nPos-*pnCurPos)*20, NULL,
                    &cltRect, NULL, NULL, SW_INVALIDATE );

      si.fMask = SIF_POS;
      si.nPos  = *pnCurPos;
      SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  }
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static int  nDspLines;
static int  nNumItems = 0;
static int  nCurPos  = 0;
static char szBuf[10];

  switch( uMsg )
  {
      case WM_CREATE :
      {
            ShowScrollBar( hWnd, SB_VERT, TRUE );
      HWND button1, button2; 
      button1 = CreateWindow("Button","EXIT",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,150,10,80,25,hWnd,(HMENU)IDM_EXIT,hInst,0);
      button2 = CreateWindow("Button","TEST",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,150,50,80,25,hWnd,(HMENU)IDM_TEST,hInst,0);
      

      }

              break;

      case WM_SIZE :
              {
                RECT rect;
                GetClientRect( hWnd, &rect );

                nDspLines = rect.bottom / 20;

                if ( nDspLines < nNumItems ) 
                {
                    SCROLLINFO si;

                    si.cbSize = sizeof( SCROLLINFO );
                    si.fMask  = SIF_POS | SIF_RANGE | SIF_PAGE;
                    si.nMin  = 0;
                    si.nMax  = nNumItems-1;
                    si.nPage  = nDspLines;
                    si.nPos  = nCurPos;

                    EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );
                    SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
                }
                else
                    EnableScrollBar( hWnd, SB_VERT, ESB_DISABLE_BOTH );
              }
              break;
              
      case WM_PAINT :
              {
                PAINTSTRUCT ps;
                int        i,j;
                int        nNumPaint;

                nNumPaint = min( nCurPos+nDspLines, nNumItems );

                BeginPaint( hWnd, &ps );

                for ( j=0,i=nCurPos; i<nNumPaint; i++,j++ )
                {
                    itoa( i, szBuf, 10 );
                    TextOut( ps.hdc, 10, j*20, "Line of Text:", 13 );
                    TextOut( ps.hdc, 90, j*20, szBuf, strlen( szBuf ) );
                }
                  
                EndPaint( hWnd, &ps );
              }
              break;

      case WM_VSCROLL :
              Scroll( hWnd, &nCurPos, LOWORD( wParam ) );

              break;

      case WM_COMMAND :
              switch( LOWORD( wParam ) )
              {
                case IDM_TEST :
                        if ( nDspLines == nNumItems ) 
                          EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );

                        nNumItems++;
                        if ( nDspLines < nNumItems )
                        {
                          SCROLLINFO si;

                          si.cbSize = sizeof( SCROLLINFO );
                          si.fMask  = SIF_RANGE | SIF_PAGE;
                          si.nMin  = 0;
                          si.nMax  = nNumItems-1;
                          si.nPage  = nDspLines;

                          SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
                        }

                        InvalidateRect( hWnd, NULL, FALSE );
            WM_SIZE;
                        break;

                case IDM_EXIT :
                        DestroyWindow( hWnd );
                        break;
              }
              break;
      
      case WM_DESTROY :
              PostQuitMessage(0);
              break;

      default :
            return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  }

  return( 0L );
}

Skulle vara underbart om någon kan hjälpa mig, sliter snart av allt mitt hår!!

129 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
126 ms — hämta tråd, inlägg och bilagor (db)