|
a slider is a windows control equipped with a small bar, also called a thumb, that slides along a visible line. there are two types of sliders: horizontal and vertical:
to use the slider control, the user can drag the thumb in one of two directions. if the slider is horizontal, the user can drag the thumb left or right. the thumb of a vertical slider can be dragged up or down. this changes the position of the thumb. the user can also click the desired position along the line to place the thumb at the desired location. alternatively, when the slider has focus, the user can also use the arrow keys of the keyboard to move the thumb.
a slider is configured with a set of values from a minimum to a maximum. therefore, the user can make a selection included in that range. optionally, a slider can be equipped with small indicators called ticks:
the ticks can visually guide the user for the available positions of the thumb mark. a slider can be used to let the user specify a value that conforms to a range. when equipped with ticks, a slider can be used to control exact values that the user can select in a range, preventing the user from setting just any desired value.
-
for this application, we will help the user display different pictures from a series of 10. if you want, you can start a new dialog-based application named carinventory with no about box and set the dialog title to car inventory delete the todo line and the ok button
-
save the following pictures to your hard drive:
-
on the main menu, click insert -> resource or project -> add resource…
-
on the add resource dialog box, click import… and import the above pictures. if you are using msvc 6, you may receive a message box when you import each picture, simply click ok
-
change the id of the new bitmap to idb_civic, idb_elantra, idb_escape, idb_escort, idb_focus, idb_marquis, idb_mystique, idb_navigator, idb_sentra, and idb_sephia respectively
-
add a picture control to the top side of the dialog box and change its id to idc_preview
-
type to bitmap and set its bitmap to idb_civic
-
add a control variable for the picture control and name it m_preview
-
to store the values for the controls on the dialog box, in the header file of the dialog box, declare a private uint array named carpicture[10]
-
to initialize the array, type the following in the oninitdialog event:
bool cslider1dlg::oninitdialog()
{
cdialog::oninitdialog();
seticon(m_hicon, true); // set big icon
seticon(m_hicon, false); // set small icon
// todo: add extra initialization here
carpicture[0] = idb_civic;
carpicture[1] = idb_elantra;
carpicture[2] = idb_escape;
carpicture[3] = idb_escort;
carpicture[4] = idb_marquis;
carpicture[5] = idb_mystique;
carpicture[6] = idb_navigator;
carpicture[7] = idb_sentra;
carpicture[8] = idb_focus;
carpicture[9] = idb_sephia;
return true; // return true unless you set the focus to a control
} |
-
to display default values in the dialog box, on top of its onpaint() event, type the following:
void cslider1dlg::onpaint()
{
cbitmap bmp;
bmp.loadbitmap(carpicture[0]);
m_preview.setbitmap(bmp);
updatedata(false);
if (isiconic())
{
cpaintdc dc(this); // device context for painting
sendmessage(wm_iconerasebkgnd, (wparam) dc.getsafehdc(), 0);
// center icon in client rectangle
int cxicon = getsystemmetrics(sm_cxicon);
int cyicon = getsystemmetrics(sm_cyicon);
crect rect;
getclientrect(&rect);
int x = (rect.width() - cxicon + 1) / 2;
int y = (rect.height() - cyicon + 1) / 2;
// draw the icon
dc.drawicon(x, y, m_hicon);
}
else
{
cdialog::onpaint();
}
} |
-
test the application and return to msvc
to provide a slider to an application, from the controls toolbox, click the slider button and click the desired area on the dialog box or the form.
to programmatically create a slider, declare a pointer to csliderctrl using the new operator. to initialize the control, call its create() method. here is an example: bool cdlgslide::oninitdialog()
{
cdialog::oninitdialog();
// todo: add extra initialization here
csliderctrl *trackbar = new csliderctrl;
trackbar->create(ws_child | ws_visible,
crect(15, 20, 222, 50), this, 0x14);
return true; // return true unless you set the focus to a control
// exception: ocx property pages should return false
}
- on the controls toolbox, click the slider button
and click in lower-left section of the dialog box
- save all
本文关键:The Slider Control
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)
|