Home > Software engineering >  The ScrollBar CView automatically hidden
The ScrollBar CView automatically hidden

Time:10-01

Everyone a great god, and I recently in the realization of the CView ScrollBar automatic hidden, when the mouse in the View area (including the scroll bar area), show the scroll bar; When the mouse leaves the View area (including the scroll bar area), automatically hide the scroll bar,

I implemented by the following way:
1, in two ScrollBar variables declared in a CTextView
 
The class CTextView: public CView {
Protected:
//class NSbStyle inherited from CScrollBar class
NSbStyle m_sbH m_sbV;//defines two variables ScrollBar
Virtual void OnInitialUpdate ();
}

2, in CTextView OnInitialUpdate () created in m_sbH, m_sbV and initialize the two position of the ScrollBar
 
Void CTextView: : the OnInitialUpdate ()
{
SCROLLINFO info;
Info. CbSize=sizeof (SCROLLINFO);
Info. FMask=SIF_ALL;
The info. The nMin=0;
Info. NMax=100;
Info. NPage=10;
The info. The nPos=0;
Info. NTrackPos=0;

//set the position of the two ScrollBar
CRect rcSbH, rcSbV rcClient;
GetClientRect (rcClient);
RcSbH. Left=rcClient. Left;
RcSbH. Right=rcClient. Right;
RcSbH. Top=rcClient. Bottom - H_SCROLLBAR_WIDTH;
RcSbH. Bottom=rcClient. Bottom;

RcSbV. Left=rcClient. Right - V_SCROLLBAR_WIDTH;
RcSbV. Right=rcClient. Right;
RcSbV. Top=rcClient. Top;
RcSbV. Bottom=rcClient. Bottom - V_SCROLLBAR_WIDTH;//VScrollBar no bottom, empty space to HScrollBar
//the ScrollBar Create still according to the given width to calculate the size of the
M_sbH. Create (WS_CHILD | WS_VISIBLE | SBS_HORZ, rcSbH, this, NULL);
M_sbV. Create (WS_CHILD | WS_VISIBLE | SBS_VERT, rcSbV, this, NULL);
//Set the range, the page and position parameters in scroll bars.
M_sbH. SetScrollInfo (& amp; Info);
M_sbV. SetScrollInfo (& amp; Info);
}

3, in CTextView onm ouseMove () and onm ouseLeave () function to add a message response function
//XSB_EDRAWELEM is an enumeration variable, the enumeration of the Scrollbar state
 
Typedef enum tagXSB_EDRAWELEM
{
ENotDrawn=0,//UI element is not visible. (UI the Rect empty).
ENormal,//Element should be drawn in normal state.
EHover//Element should be drawn in hover state.
} XSB_EDRAWELEM;
Void CTextView: onm ouseMove (UINT, CPoint point)
{

TRACKMOUSEEVENT tme.
Tme. CbSize=sizeof (TRACKMOUSEEVENT);
Tme. DwFlags=TME_HOVER | TME_LEAVE;
Tme. DwHoverTime=10;
Tme. HwndTrack=m_hWnd;
if (! _TrackMouseEvent (& amp; Tme))
_cprintf (" Track Error! \n");

XSB_EDRAWELEM temp_sbHStatus=eNotDrawn;
XSB_EDRAWELEM temp_sbVStatus=eNotDrawn;

//m_sbHStatus CTextView according to state of the horizontal scroll bar
//the system response onm ouseMove (), which indicates that the mouse has entered into the area of the View, so show m_sbH
If (m_sbHStatus==eNotDrawn) {
M_sbH. ShowWindow (TRUE);
}
//m_sbVStatus CTextView vertical scroll bar in the display status
//the system response onm ouseMove (), which indicates that the mouse has entered into the area of the View, so show m_sbV
If (m_sbVStatus==eNotDrawn) {
M_sbV. ShowWindow (TRUE);
}

CRect rt, rt_vS rt_hS;
GetClientRect (& amp; Rt);//gets the current View of ClientRect
M_sbV. GetWindowRect (& amp; Rt_vS);//get the View of some Wnd in the Window of the Rect
The ScreenToClient (& amp; Rt_vS); The Window//the Rect into the coordinates of the parent Window
M_sbH. GetWindowRect (& amp; Rt_hS);//get the View of some Wnd in the Window of the Rect
The ScreenToClient (& amp; Rt_hS); The Window//the Rect into the coordinates of the parent Window

//point in the View area, but is not in the horizontal scroll bar area
If (rt) PtInRect (point) & amp; & ! Rt_hS. PtInRect (point)) {
Temp_sbHStatus=eNormal;
}
//point in the View area, but not in the vertical scroll bar area
If (rt) PtInRect (point) & amp; & ! Rt_vS. PtInRect (point)) {
Temp_sbVStatus=eNormal;
}
//point in the View area, at the same time also in a horizontal scroll bar area
If (rt) PtInRect (point) & amp; & Rt_hS. PtInRect (point)) {
Temp_sbHStatus=eHover;
}
//point in the View area, but also in the region of the vertical scroll bar
If (rt) PtInRect (point) & amp; & Rt_vS. PtInRect (point)) {
Temp_sbVStatus=eHover;
}
//if the ScrollBar state changes, you will need to redraw ScrollBar; But if the state remains the same, do not need to redraw
If ((temp_sbHStatus!=eNotDrawn) & amp; & (temp_sbHStatus!={m_sbHStatus))
M_sbHStatus=temp_sbHStatus;
//DrawScrollBar (status) is a function of a state according to the drawing ScrollBar
M_sbH. DrawScrollBar (m_sbHStatus);
}
//if the ScrollBar state changes, you will need to redraw ScrollBar; But if the state remains the same, do not need to redraw
If ((temp_sbVStatus!=eNotDrawn) & amp; & (temp_sbVStatus!={m_sbVStatus))
M_sbVStatus=temp_sbVStatus;
M_sbV. DrawScrollBar (m_sbVStatus);
}
return;
}
//mouse away, hide the ScrollBar, and update all state variables
Void CTextView: : onm ouseLeave () {
M_sbHStatus=eNotDrawn;
M_sbVStatus=eNotDrawn;
M_sbV. ShowWindow (FALSE);
M_sbH. ShowWindow (FALSE);
return;
}

4, now the question is: 1) when the horizontal Scrollbar display, the mouse in a horizontal scroll bar area, not automatic response onm ouseLeave (), and wait until the mouse left the View area response onm ouseLeave (); 2) whether there is other way to capture the mouse can reference?




CodePudding user response:

In MFC framework, the ScrollBar CTextView ClientRect automatically out of area, is there any way to remove the default Settings? Thank you Daniel!
  • Related