📄 scrollbar.c
字号:
{ int scrollOffset=yPos-DesignationW-scrollbar->ScrollbarDragging;
int maxScroll_Track=(scrollbar->TrackHeight-scrollbar->ScrollHeight);
int maxScroll_Line=(scrollbar->TotalLine-scrollbar->PageKeepLines);
int newtopline=scrollOffset*maxScroll_Line/maxScroll_Track;
if(newtopline<0)newtopline=0;
else if(newtopline>maxScroll_Line)newtopline=maxScroll_Line;
if(newtopline!=scrollbar->TopLine)
{ SendMessage(hWnd,WM_VSCROLL,newtopline,newtopline-scrollbar->TopLine);
}
}
}
}
//---------------------------------------------------------------------------
void ScrollBar_MouseDown(HWND hWnd,LPARAM LParam)
{ TScrollBar *scrollbar= WndScrollBar(hWnd);
int xPos=LOWORD(LParam)-WNDPTR(hWnd)->WndRect.left;
int yPos=HIWORD(LParam)-WNDPTR(hWnd)->WndRect.top;
if(PointInRect(xPos, yPos, (TRECT *)&scrollbar->Left))
{ int topline;
if(scrollbar->TrackHeight>0)
{ if(yPos<scrollbar->Top+DesignationW)
{ ScrollBar_UpdateFlipflopButton(hWnd,scrollbar,1);
topline=(scrollbar->TopLine>0)?scrollbar->TopLine-1:scrollbar->TopLine;
SendMessage(hWnd,WM_VSCROLL,topline,-1);
}
else if(yPos>=scrollbar->Bottom-DesignationW)
{ ScrollBar_UpdateFlipflopButton(hWnd,scrollbar,2);
topline=(scrollbar->TopLine+scrollbar->PageKeepLines<scrollbar->TotalLine)?scrollbar->TopLine+1:scrollbar->TopLine;
SendMessage(hWnd,WM_VSCROLL,topline,1);
}
else if(scrollbar->ScrollHeight>0)
{ if(yPos<scrollbar->ScrollTop)
{ if(scrollbar->TopLine>0)
{ int x1=(scrollbar->ScrollTop-yPos)*(scrollbar->TotalLine-scrollbar->PageKeepLines);
int x2=(scrollbar->TrackHeight-scrollbar->ScrollHeight);
int step=(x1+x2-1)/x2;
if(step>scrollbar->TopLine)step=scrollbar->TopLine;
if(step>0)
{ SendMessage(hWnd,WM_VSCROLL,scrollbar->TopLine-step,-step);
}
}
}
else if(yPos>scrollbar->ScrollTop+scrollbar->ScrollHeight)
{ if(scrollbar->TopLine+scrollbar->PageKeepLines<scrollbar->TotalLine)
{ int x1=(yPos-scrollbar->ScrollTop-scrollbar->ScrollHeight)*(scrollbar->TotalLine-scrollbar->PageKeepLines);
int x2=(scrollbar->TrackHeight-scrollbar->ScrollHeight);
int step=(x1+x2-1)/x2;
if(scrollbar->TopLine+scrollbar->PageKeepLines+step>scrollbar->TotalLine)
{ step=scrollbar->TotalLine-scrollbar->TopLine-scrollbar->PageKeepLines;
}
if(step>0)
{ SendMessage(hWnd,WM_VSCROLL,scrollbar->TopLine+step,step);
}
}
}
else if( scrollbar->ScrollHeight< scrollbar->TrackHeight )
{ scrollbar->ScrollbarDragging=yPos-scrollbar->ScrollTop;
if(!scrollbar->ScrollbarDragging)scrollbar->ScrollbarDragging=1;
}
}
}
else
{ int topline=scrollbar->TopLine;
if( yPos < ((scrollbar->Top+scrollbar->Bottom)>>1) )
{ ScrollBar_UpdateFlipflopButton(hWnd,scrollbar,1);
if(topline>0) topline--;
SendMessage(hWnd,WM_VSCROLL,topline,-1);
}
else
{ ScrollBar_UpdateFlipflopButton(hWnd,scrollbar,2);
if(topline+scrollbar->PageKeepLines<scrollbar->TotalLine)topline++;
SendMessage(hWnd,WM_VSCROLL,topline,1);
}
}
}
}
//---------------------------------------------------------------------------
void ScrollBar_UpdateScroll(HWND hWnd,TScrollBar *scrollbar,BOOL bRepaint)
{ int ScrollTop,ScrollHeight;
if(bRepaint)
{ ScrollTop=scrollbar->ScrollTop;
ScrollHeight=scrollbar->ScrollHeight;
}
if(scrollbar->TrackHeight>0 && scrollbar->TotalLine>scrollbar->PageKeepLines)
{ /* 确定scroll height 和 positon*/
if(scrollbar->TotalLine<=scrollbar->LinePerPage)
{ scrollbar->ScrollHeight = scrollbar->TrackHeight - ((scrollbar->TrackHeight*scrollbar->TotalLine/scrollbar->LinePerPage)>>1);
}
else
{ /*scrollbar->ScrollHeight = (scrollbar->TrackHeight*scrollbar->LinePerPage/scrollbar->TotalLine)>>1;*/
scrollbar->ScrollHeight = (scrollbar->TrackHeight - scrollbar->TrackHeight*(scrollbar->TotalLine-scrollbar->LinePerPage)/(scrollbar->TotalLine+ScrollHeightAttenuationParam) )>>1;
}
if(scrollbar->ScrollHeight<6)scrollbar->ScrollHeight=6;
if(scrollbar->ScrollHeight<scrollbar->TrackHeight)
{ scrollbar->ScrollTop=scrollbar->Top+DesignationW+(scrollbar->TrackHeight-scrollbar->ScrollHeight)*scrollbar->TopLine/(scrollbar->TotalLine-scrollbar->PageKeepLines);
}
else
{ scrollbar->ScrollTop=scrollbar->Top+DesignationW;
scrollbar->ScrollHeight=scrollbar->TrackHeight;
}
}
else
{ scrollbar->ScrollHeight=0;
}
if(bRepaint && (ScrollTop!=scrollbar->ScrollTop || ScrollHeight!=scrollbar->ScrollHeight) )
{ HDC dc=GetDC(hWnd);
ScrollBar_PaintTrack(dc,scrollbar);
ReleaseDC(dc);
}
}
//---------------------------------------------------------------------------
void ScrollBar_PaintTrack(HDC dc,TScrollBar *scrollbar)
{ if(scrollbar->TrackHeight>0)
{ GroupOn(dc);
SetBackColor(dc,CL_BTNFACE);
EraseBackground(dc,scrollbar->Left,scrollbar->Top+DesignationW,DesignationW,scrollbar->TrackHeight);
if(scrollbar->ScrollHeight>0)
{ DrawRect(dc,scrollbar->Left,scrollbar->ScrollTop,DesignationW,scrollbar->ScrollHeight);
}
GroupOff(dc,scrollbar->Left,scrollbar->Top+DesignationW,DesignationW,scrollbar->TrackHeight);
}
}
//---------------------------------------------------------------------------
void ScrollBar_Repaint(HWND hWnd,HDC dc)
{ TScrollBar *scrollbar=WndScrollBar(hWnd);
if(scrollbar->TrackHeight>0)
{ int triangleW=DesignationW>>1;
GroupOn(dc);
SetBackColor(dc,CL_BTNFACE);
EraseBackground(dc,scrollbar->Left,scrollbar->Top,DesignationW,DesignationW);
EraseBackground(dc,scrollbar->Left,scrollbar->Bottom-DesignationW,DesignationW,DesignationW);
DrawRect(dc,scrollbar->Left,scrollbar->Top,DesignationW,DesignationW);
DrawLine(dc,scrollbar->Left+4,scrollbar->Top+triangleW+1,scrollbar->Left+triangleW,scrollbar->Top+5);
DrawLine(dc,scrollbar->Left+DesignationW-4,scrollbar->Top+triangleW+1,scrollbar->Left+triangleW,scrollbar->Top+5);
DrawLine(dc,scrollbar->Left+5,scrollbar->Top+triangleW+2,scrollbar->Left+triangleW,scrollbar->Top+7);
DrawLine(dc,scrollbar->Left+DesignationW-5,scrollbar->Top+triangleW+2,scrollbar->Left+triangleW,scrollbar->Top+7);
DrawRect(dc,scrollbar->Left,scrollbar->Bottom-DesignationW ,DesignationW,DesignationW);
DrawLine(dc,scrollbar->Left+4,scrollbar->Bottom-triangleW-1,scrollbar->Left+triangleW,scrollbar->Bottom-5);
DrawLine(dc,scrollbar->Left+DesignationW-4,scrollbar->Bottom-triangleW-1,scrollbar->Left+triangleW,scrollbar->Bottom-5);
DrawLine(dc,scrollbar->Left+5,scrollbar->Bottom-triangleW-2,scrollbar->Left+triangleW,scrollbar->Bottom-7);
DrawLine(dc,scrollbar->Left+DesignationW-5,scrollbar->Bottom-triangleW-2,scrollbar->Left+triangleW,scrollbar->Bottom-7);
ScrollBar_PaintTrack(dc,scrollbar);
GroupOff(dc,scrollbar->Left,scrollbar->Top,DesignationW,scrollbar->Bottom-scrollbar->Top);
}
else
{ int triangleW=DesignationW>>1;
int triangleH=DesignationW>>2;
int triangleOffsetX,i,designationY=(scrollbar->Top+scrollbar->Bottom)>>1;
int scrollbarHeight=scrollbar->Bottom-scrollbar->Top;
GroupOn(dc);
SetBackColor(dc,CL_BTNFACE);
ClearRect(dc,scrollbar->Left,scrollbar->Top,DesignationW,scrollbarHeight);
Draw3dOutset(dc,scrollbar->Left,scrollbar->Top,DesignationW,scrollbarHeight);
triangleOffsetX=scrollbar->Left+(DesignationW-triangleW)/2;
for(i=0;i<triangleH;i++)
{ DrawHorLine(dc,triangleOffsetX+i,designationY-2-i,triangleW-i-i);
DrawHorLine(dc,triangleOffsetX+i,designationY+2+i,triangleW-i-i);
}
GroupOff(dc,scrollbar->Left,scrollbar->Top,DesignationW,scrollbarHeight);
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -