ホームに戻る
 スクロールバーの使用
 
#include <windows.h>

static LPCTSTR lpszAppName = "MyApp";

HINSTANCE hInst;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID Scroll(HWND, int *, WORD, WORD);

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

  if(! hPrevInstance){
    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(hInstance, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = lpszAppName;
    wc.lpszClassName = lpszAppName;
  }

  if(RegisterClass(&wc) == 0){
    return FALSE;
  }

  hInst = hInstance;

  hWnd = CreateWindow(
    lpszAppName,
    "My_Application",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    0,
    CW_USEDEFAULT,
    0,
    NULL,
    NULL,
    hInstance,
    NULL);

  if(hWnd == NULL){
    return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);

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

  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  static int nCurPos;

  switch(uMsg){
    case WM_CREATE:
      ShowScrollBar(hWnd, SB_HORZ, FALSE);
      SetScrollRange(hWnd, SB_HORZ, 0, 100, FALSE);
      SetScrollPos(hWnd, SB_HORZ, 0, TRUE);
      break;
    case WM_HSCROLL:
      {
        Scroll(hWnd, &nCurPos, LOWORD(wParam), HIWORD(wParam));
        InvalidateRect(hWnd, NULL, TRUE);
      }
      break;
    case WM_PAINT:
      {
        RECT rect;
        SIZE size;
        PAINTSTRUCT ps;
        HDC hDC = BeginPaint(hWnd, &ps);

        GetClientRect(hWnd, &rect);

        GetTextExtentPoint32(hDC, "abc", 3, &size);

        TextOut(hDC, (rect.right - rect.left) * (50 - nCurPos) / 120 + ((rect.right - rect.left) - size.cx) / 2, ((rect.bottom - rect.top) - size.cy) / 2, "abc", 3);

        EndPaint(hWnd, &ps);
      }
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }

  return 0;
}

VOID Scroll(HWND hWnd, int *pnCurPos, WORD wScroll, WORD pos){
  int nMin, nMax;
  int nPos;

  GetScrollRange(hWnd, SB_HORZ, &nMin, &nMax);
  nPos = GetScrollPos(hWnd, SB_HORZ);

  switch(wScroll){
    case SB_LINERIGHT:
      if(*pnCurPos < nMax){
        *pnCurPos += 1;
      }
      break;
    case SB_LINELEFT:
      if(*pnCurPos > 0){
        *pnCurPos -= 1;
      }
      break;
    case SB_PAGERIGHT:
      if(*pnCurPos < nMax){
        *pnCurPos += 1;
      }
      break;
    case SB_PAGELEFT:
      if(*pnCurPos > 0){
        *pnCurPos -= 1;
      }
      break;
    case SB_THUMBTRACK:
    case SB_THUMBPOSITION:
      {
        *pnCurPos = pos;
      }
      break;
  }

  if(nPos != *pnCurPos){
    SetScrollPos(hWnd, SB_HORZ, *pnCurPos, TRUE);
  }
}

inserted by FC2 system