以下是创建窗口和滚动条的代码:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, "Name", WS_OVERLAPPEDWINDOW/*WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU*/,
CW_USEDEFAULT, 0, 1014, 585, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
WNDCLASSEX wcs;
wcs.cbSize = sizeof(wcs);
wcs.lpszClassName = szClassName;
wcs.hInstance = GetModuleHandle(0);
wcs.lpfnWndProc = CustWndProc;
wcs.hCursor = LoadCursor(NULL, IDC_ARROW);
wcs.hIcon = 0;
wcs.lpszMenuName = 0;
wcs.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcs.style = 0;
wcs.cbClsExtra = 0;
wcs.cbWndExtra = 0;
wcs.hIconSm = 0;
if(!RegisterClassEx(&wcs))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwndCtrl = CreateWindowEx(
0L, // give it a standard border
szClassName,
_T("A custom control"),
WS_VISIBLE|WS_CHILD|WS_BORDER,
0, 0, 0, 0,
hWnd,
NULL, GetModuleHandle(0), CustWndProc
);
ShowWindow (hwndCtrl, SW_SHOW);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
以下是处理其消息的代码:
LRESULT CALLBACK CustWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
RECT rc = {};
GetClientRect(hwnd, &rc);
const SIZE sz = { rc.right - rc.left, rc.bottom - rc.top };
SCROLLINFO si;
switch(msg)
{
case WM_MOUSEHOVER:
::MessageBox(hwnd, "Enter", "Info", MB_OK);
return 0;
case WM_CREATE:
int w , h;
w = 10;
h = 10;
HWND buttons;
for(h=10;h<500; h+=35){
buttons = CreateWindow("BUTTON", "How", WS_VISIBLE|WS_CHILD, w, h, 50, 30, hwnd, (HMENU)1231,NULL, NULL);
}
int width, height;
width = LOWORD(lParam); // Width Size of hWnd
height = HIWORD(lParam);
Scrollbar = CreateWindowEx(0L,
"SCROLLBAR",
NULL, // There is no text to display
WS_CHILD | WS_VISIBLE | SBS_VERT,
980,
47,
18,
405,
hWnd,
NULL,
hInst,
NULL
);
return 0;
case WM_INITDIALOG:
ZeroMemory(&si, sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = 0;
si.nMax = 1000;
si.nPage = 10;
si.nPos = 54;
SetScrollInfo(Scrollbar, SB_CTL, &si, TRUE);
return TRUE;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return FALSE;
}
正如你所看到的,有一些按钮是通过for循环自动创建的,之后滚动条本身被创建,现在我不知道如何使它向下或向上滚动等等。欢迎各种回复。