で、本来の sample008.cpp の修正
WndProc の、switch case 追加(修正)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
static int xChar; // horizontal scrolling unit
static int yChar; // vertical scrolling unit
switch (message) {
case WM_CREATE:
proc_wm_create(hWnd, &xChar, &yChar);
return 0;
case WM_SIZE:
proc_wm_size(hWnd, lParam, xChar, yChar);
return 0;
case WM_HSCROLL: // 水平スクロール バー
proc_wm_hscroll(hWnd, wParam, xChar);
return 0;
case WM_VSCROLL: // 垂直スクロール バー
proc_wm_vscroll(hWnd, wParam, yChar);
return 0;
case WM_PAINT:
int yPos, xPos;
SCROLLINFO si;
// Get vertical scroll bar position.
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hWnd, SB_VERT, &si);
yPos = si.nPos;
// Get horizontal scroll bar position.
GetScrollInfo(hWnd, SB_HORZ, &si);
xPos = si.nPos;
sample_text_print(hWnd,-xPos * xChar,-yPos * yChar);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
として、各関数は、次の通り
void proc_wm_create(HWND hwnd,
int* ptr_xChar,
int* ptr_yChar) {
TEXTMETRIC tm;
static int xUpper; // average width of uppercase letters
HDC hdc;
// Get the handle to the client area's device context.
hdc = GetDC(hwnd);
// Extract font dimensions from the text metrics.
GetTextMetrics(hdc, &tm);
*ptr_xChar = tm.tmAveCharWidth;
xUpper = (tm.tmPitchAndFamily & 1 ? 3 : 2) * (*ptr_xChar) / 2;
*ptr_yChar = tm.tmHeight + tm.tmExternalLeading;
// Free the device context.
ReleaseDC(hwnd, hdc);
}
void proc_wm_size(HWND hwnd, LPARAM lParam , int xChar, int yChar) {
static int xClient; // width of client area
static int yClient; // height of client area
// Retrieve the dimensions of the client area.
yClient = HIWORD(lParam);
xClient = LOWORD(lParam);
SCROLLINFO si;
// Set the vertical scrolling range and page size
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = 1024; // サンプルのため適当なMAX値
si.nPage = yClient / yChar;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
// Set the horizontal scrolling range and page size.
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = 2 + 500; // サンプルのため適当なMAX値
si.nPage = xClient / xChar;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
}
int proc_wm_scroll(HWND hwnd, WPARAM wParam, int nBar) {
SCROLLINFO si;
// Get all the vertial scroll bar information.
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
// Save the position for comparison later on.
GetScrollInfo(hwnd, nBar, &si);
int mmPos; // current horizontal scrolling position
mmPos = si.nPos;
switch (LOWORD(wParam)) {
// User clicked the left arrow.
case SB_LINELEFT:
// User clicked the top arrow.
// case SB_LINEUP:
si.nPos -= 1;
break;
// User clicked the right arrow.
case SB_LINERIGHT:
// User clicked the bottom arrow.
// case SB_LINEDOWN:
si.nPos += 1;
break;
// User clicked the scroll bar shaft left of the scroll box.
case SB_PAGELEFT:
// User clicked the scroll bar shaft above the scroll box.
// case SB_PAGEUP:
si.nPos -= si.nPage;
break;
// User clicked the scroll bar shaft right of the scroll box.
case SB_PAGERIGHT:
// User clicked the scroll bar shaft below the scroll box.
// case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
// User dragged the scroll box.
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, nBar, &si, TRUE);
return mmPos;
}
//
void proc_wm_hscroll(HWND hwnd, WPARAM wParam, int xChar) {
static int xPos; // current horizontal scrolling position
xPos=proc_wm_scroll(hwnd, wParam, SB_HORZ);
SCROLLINFO si;
GetScrollInfo(hwnd, SB_HORZ, &si);
// If the position has changed, scroll the window.
if (si.nPos != xPos) {
ScrollWindow(hwnd, xChar * (xPos - si.nPos), 0, NULL, NULL);
}
}
//
void proc_wm_vscroll(HWND hwnd, WPARAM wParam, int yChar) {
static int yPos; //
yPos = proc_wm_scroll(hwnd, wParam, SB_VERT);
SCROLLINFO si;
GetScrollInfo(hwnd, SB_VERT, &si);
// If the position has changed, scroll window and update it.
if (si.nPos != yPos) {
ScrollWindow(hwnd, 0, yChar * (yPos - si.nPos), NULL, NULL);
}
}