关于《014滚动条3》的课后题
@小甲鱼@无符号整形首先是第4题:
滚动条的大小应该还与滚动条的范围有关吧,不仅仅是page吧~~
再是第3题,主要是这个题有疑问:
//设置横向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = xClientMax/cxChar;
si.nPage = cxClient/cxChar;
si.nPos = 0;
SetScrollInfo(hwnd,SB_HORZ,&si,FALSE);
关于这失效滚动条:
果一开始界面比较大,滚动条没出现,就不会看到“失效”这个效果。
而只有缩小界面滚动条出现一次之后,才有“失效”这个效果。
那么如何一开始就能看到实现效果呢? 你的wnd=CreateWinddows(szAppNeme,TEXT("鱼C工作室"),WS_OVERLAPPEDWINDOW,你 后面的 WS_VSCROLL和 WS_HSCROLL在哪去了{:5_109:}) SIF_DISABLENOSCROLL的意思是不允许使滚动条消失。一开始界面较大,那么一开始滚动条一样存在,只不过不能用。 hujianhang2996 发表于 2016-7-14 09:42
SIF_DISABLENOSCROLL的意思是不允许使滚动条消失。一开始界面较大,那么一开始滚动条一样存在,只不过不能 ...
我想本来应该是这样,但是测试一开始就是不存在的呢 我能说都忘记光光了吗?!{:9_240:}@小甲鱼
SIF_DISABLENOSCROLL的意思是不允许使滚动条消失 你是否在WM_CREATE里设置了si.fMask为SIF_DISABLENOSCROLL? hujianhang2996 发表于 2016-7-18 17:25
你是否在WM_CREATE里设置了si.fMask为SIF_DISABLENOSCROLL?
上面程序写了;
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL; 是在WM_CREATE消息里写的吗?要不然把完整代码发上来让大家看看吧 hujianhang2996 发表于 2016-7-19 12:27
是在WM_CREATE消息里写的吗?要不然把完整代码发上来让大家看看吧
/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#include <strsafe.h>
#include "SysMet.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
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("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szBuffer = TEXT("12345");
TEXTMETRIC tm;
SCROLLINFO si;
int pos = 0;
static int cxChar, cxCaps, cyChar, cxClient, cyClient, xClientMax;
size_t iTarget;
int i;
int xPos=0,yPos = 0;
int displayPos,displayxPos = 0;
int FirstLine,LastLine;
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);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//获取垂直滚动条的位置
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd,SB_VERT,&si);
yPos = si.nPos;
//获取纵向滚动条的位置
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd,SB_HORZ,&si);
xPos = si.nPos;
//计数需要重绘的行
FirstLine = max(0, yPos + ps.rcPaint.top / cyChar);
LastLine = min(NUMLINES - 1, yPos + ps.rcPaint.bottom / cyChar);
for (i = FirstLine; i <= LastLine; i++)
{
SetTextAlign(hdc, TA_LEFT | TA_TOP);
displayPos = (i-yPos) * cyChar; //计数重绘行的纵向位置
displayxPos=-xPos* cxChar;
StringCchLength(sysmetrics.szLabel, 1024, &iTarget);
TextOut(hdc, displayxPos,displayPos, sysmetrics.szLabel, iTarget);
displayxPos= (22 * cxCaps) - xPos* cxChar;
StringCchLength(sysmetrics.szDesc, 1024, &iTarget);
TextOut(hdc, displayxPos, displayPos, sysmetrics.szDesc, iTarget);
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
displayxPos= (25 * cxCaps + 40 * cxChar) - xPos* cxChar;
StringCchPrintf(szBuffer, 128, TEXT("%5d------%d"), GetSystemMetrics(sysmetrics.iIndex),i);
StringCchLength(szBuffer, 128, &iTarget);
TextOut(hdc, displayxPos, displayPos, szBuffer, iTarget);
}
//绘制完毕
EndPaint(hwnd, &ps);
xClientMax = 75*cxChar;//这里最好是测算出来,而不是写死,后面再说吧。
return 0;
case WM_SIZE:
hdc = GetDC(hwnd);
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
StringCchPrintf(szBuffer,128,TEXT("%d * %d"),cxClient,cyClient);
SetTextAlign(hdc, TA_TOP | TA_RIGHT);
TextOut(hdc, cxClient - 40, 30,szBuffer , lstrlen(szBuffer));
ReleaseDC(hwnd,hdc);
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = NUMLINES-1;
si.nPage = cyClient/cyChar;
si.nPos = 0;
SetScrollInfo(hwnd,SB_VERT,&si,FALSE);
//设置横向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = xClientMax/cxChar;
si.nPage = cxClient/cxChar;
si.nPos = 0;
SetScrollInfo(hwnd,SB_HORZ,&si,FALSE);
return 0;
case WM_LBUTTONDOWN:
return 0;
case WM_HSCROLL:
//获取横向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_HORZ,&si);
xPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINELEFT:
xPos = si.nPos;
break;
case SB_LINERIGHT:
si.nPos++;
break;
case SB_PAGELEFT:
si.nPos -= si.nPage;
if(si.nPos < 0) si.nPos = 0;
break;
case SB_PAGERIGHT:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
}
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
SetScrollInfo(hwnd,SB_HORZ,&si,TRUE);
GetScrollInfo(hwnd,SB_HORZ,&si);
if(xPos - si.nPos != 0)
{
ScrollWindow(hwnd,cxChar*(xPos - si.nPos),0,NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_VSCROLL:
//获取纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd,SB_VERT,&si);
yPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINEUP:
yPos = si.nPos;
si.nPos--;
break;
case SB_LINEDOWN:
si.nPos++;
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;
}
//设置纵向滚动条
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
//InvalidateRect(hwnd,NULL,TRUE);
GetScrollInfo(hwnd,SB_VERT,&si);
if(yPos - si.nPos != 0)
{
rect.left = 0;
rect.top = 0;
rect.right = (22 * cxCaps);
rect.bottom = cyClient;
ScrollWindow(hwnd, 0, cyChar*(yPos - si.nPos),&rect,NULL);
//ScrollWindow(hwnd, 0, cyChar*(yPos - si.nPos),NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
{:10_254:}
页:
[1]