Introduction
My application needs to decrease 4 edges (left, top, right, bottom) of a rectangle object, maximum decreasing amount for all edges is 100%.
The following picture shows 3 cases allowed for decreasing top edge.
It is not easy to implement the task for user except that I offer a suitable control, so I created a slider with 2 buttons, which can move inward but cannot be overlapped or cross each other.
The following interface is a screen shot of the sample program which contains 2 horizontal and 2 vertical sliders.
Files, Classes, Functions and Message
- Files and Classes
The directory "lib" inside the sample program contains 4 files: SliderBoth and SliderBothBtn (H and CPP).
Each file has a class with the same name as the file.
The 2 classes are inherited from CWnd
- so the slider belongs to MFC issues.
SliderBoth
class is the main control, SliderBothBtn
is the button class of SliderBoth
.
You only need to include SliderBoth
in the file in which you use the slider.
- Functions of class SliderBoth
-
BOOL Create(CWnd*pMum,CRect rcBd,BOOL bHorz,int iID)
bHorz
: if it is a horizontal or vertical slider
iID
: control ID of the slider, which must be unique for parent window (similar to button or menu ID)
rcBd
: rectangle of the slider, but its width or height is fixed without being affected by the rectangle (see function MoveWindow(...)
below).
-
BOOL SetRange(int iMin,int iMax,int iTick)
Minimum, maximum and tick values of the slider, which must be:
iMax>iMin
, iTick>0
and (iMax-iMin)%2==0
, (iMax-iMin)% iTick==0
Because the slider draws a longer tick sign in the middle for my program.
You can remove the last 2 restrictions easily by modifying the source code.
-
GetRange(int&iMin,int&iMax,int&iTick)
Get range and tick values of the slider
-
GetPosition(int&iL,int&iR)
SetPosition(int iL,int iR)
Get
or Set
button positions
iL
means left or bottom button position for horizontal or vertical slider respectively.
-
void MoveWindow(CRect*pRect,BOOL bRepaint=TRUE)
void MoveWindow(int iX,int iY,int iW,int iH,BOOL bRepaint=TRUE)
The two functions override CWnd
related functions, which move height of horizontal slider or width of vertical slider to 27 pixels always.
Normally you only need to call Create()
and SetRange()
functions.
- Message
If mouse is released (WM_LBUTTONUP
) after moving slider's buttons, a message is posted by PostMessage()
from the slider to its parent window in the format:
message: WM_COMMAND
, wParam
: slider's ID, lParam
: zero (not used)
(The slider uses only this message).
The parent window calls function GetPosition(...)
on the message to obtain button positions of the slider.
No message is sent:
If button positions are unchanged after moving - E.g. moved to left then right to previous position.
Note: It is very easy to modify the source code if you need message while mouse is moving.
Conclusion
All of my articles, including this one, are purely from my test applications, I don't have any extra time to write a real "article" as many guys do in the place - currently the slider works perfectly in my program, that is all I can do at the stage.
If you find bugs or need more features in your cases, please post a reply, I may modify the slider if I have time some day.
History
- 9th August, 2006: Initial post