控件学习(三)

来源:互联网 发布:燃烧冰flamingo数据 编辑:程序博客网 时间:2024/06/06 16:33

一: CListBox的使用

列表框

CListBox

CListBox类提供Windows列表框的功能。列表框显示项的列表,如用户可以见到和选择的文件名称。
在单选列表框里,用户只可选择一个项。在多选列表框里,可选择许多项。当用户选择某项时,其高亮显示且列表框给父窗口发送一个通知消息。
可从对话模板或直接在你的代码中创建列表框。直接创建时,构造CListBox对象,再调用Create成员函数创建Windows列表框控件并将其附加给CListBox对象。要在对话模板中使用列表框,可在对话框类中声明一个CListbox 变量,再在对话框类的DoDataExchange中使用DDX_Control连接成员变量到此控件(当向对话框类中添加控件变量时,ClassWizard自动为你实现)。
构造函数可以是从CListBox派生的类的一个单步进程。为派生类写构造函数并从中调用Create。
如果要处理由列表框发送到其父亲(通常为从CDialog派生的类)的Windows通知消息,为每个消息添加消息映射入口和消息处理成员函数到父类。
每个消息映射入口有以下形式:
ON_Notification( id,memberFxn )
id 指定发送通知的列表框控件的子窗口,memberFxn 是编写处理通知的父成员函数名的地方。
父函数原型如下:
afx_msg void memberFxn( );
下面是可能的消息映射入口列表和描述它们可能被发送到父亲的情况:
ON_LBN_DBLCLK用户双击列表框中的字符串。只有LBS_NOTIFY风格的列表框才会发送此通知消息。ON_LBN_ERRSPACE列表框不能重新分配足够的内存来满足请求。ON_LBN_KILLFOCUS列表框正失去输入焦点。ON_LBN_SELCANCEL当前列表框选择被取消。此消息只有在列表框是LBS_NOTIFY风格时才发送。ON_LBN_SELCHANGE列表框中的选择可能改变。如果选择被CListBox::SetCurSel成员函数改变,则通知不发送。此通知只适用于LBS_NOTIFY风格的列表框。无论何时用户按下箭头键,即使选择未改变,LBN_SELCHANGE通知消息都被发送给多选列表框。ON_LBN_SETFOCUS列表框正在接收输入焦点。ON_WM_CHARTOITEM一个无字符串的自绘制列表接受WM_CHAR消息。ON_WM_VKEYTOITEMLBS_WANTKEYBOARDINPUT风格的列表框接受WM_KEYDOWN消息。 

  m_listbox.Create(WS_CHILD|WS_VISIBLE|LBS_NOTIFY,CRect(50,100,120,150),this,1012);m_listbox.AddString(_T("犀利哥"));m_listbox.AddString(_T("凤姐"));m_listbox.AddString(_T("小月月"));

在消息映射添加

ON_LBN_SELCHANGE(1012,OnChoice)

void C控件学习Dlg::OnChoice(void){int sel=m_listbox.GetCurSel();CString str;m_listbox.GetText(sel,str);MessageBox(str);}
如果该开始在创建的时候不加上LBS_NOTIFY,就没反映

查看MSDN可以看到

ON_LBN_SELCHANGE列表框中的选择可能改变。如果选择被CListBox::SetCurSel成员函数改变,则通知不发送。此通知只适用于LBS_NOTIFY风格的列表框。无论何时用户按下箭头键,即使选择未改变,LBN_SELCHANGE通知消息都被发送给多选列表框。
效果图如下:




二,静态文本控件CStatic

这个控件使用很简单,直接Create将所需要的文本显示在某一个区域

CStatic类提供了一个Windows静态控件的性能。一个静态控件用来显示一个文本字符串,框,矩形,图标,光标,位图,或增强的图元文件。它可以被用来作为标签,框,或用来分隔其它的控件。一个静态控件不接收输入,也不提供输出;但是,如果它是用SS_NOTIFY风格创建的,则它可以通知其父有关设备点击的消息。创建一个静态控件分两步。首先,调用构造函数来构造此CStatic对象,然后调用Create成员函数来创建此静态控件并将它与该CStatic对象连接。

加载位图的话和贴图差不多

m_Text.Create(_T("请选择你的对象"),WS_CHILD|WS_VISIBLE|SS_BITMAP,CRect(50,80,200,120),this,1013);m_bitmap.LoadBitmapW(IDB_BITMAP1);m_Text.SetBitmap((HBITMAP)m_bitmap.m_hObject);

注意属性里面添加SS_BITMAP属性



差了一下,控件Picture Control控件对应的类是什么尼?找了半天没找到,网上找到有人说是CStatic,上面效果图也证实了这一点


三:CScrollBar的使用

CScrollBar使用也和前面的控件一样,先声明一个CScrollBar滚动条的对象,然后调用Create函数来创建滚动条

   通过CScrollBar::SetScrollPos可以设置滚动条的位置

 对于更改滚动条的位置怎么更改,那么就会显示不同的位置,这里有一个很重要的消息WM_HSCROLL函数来更改,响应该函数

CWnd::OnHScroll

afx_msg void OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar );

参数:nSBCode指定了滚动条代码,指明了用户的滚动请求。这个参数可以是下列值之一:·SB_LEFT滚动到最左边。·SB_ENDSCROLL结束滚动。·SB_LINELEFT向左滚动。·SB_LINERIGHT向右滚动。·SB_PAGELEFT向左滚动一页。·SB_PAGERIGHT向右滚动一页。·SB_RIGHT滚动到最右边。·SB_THUMBPOSITION滚动到绝对位置。当前的位置由nPos参数指定。·SB_THUMBTRACK将滚动块拖动到指定的位置。当前的位置由nPos参数指定。nPos如果滚动条代码为SB_THUMBPOSITION或者SB_THUMBTRACK,则指定了滚动块的位置;否则没有使用。取决于初始的滚动范围,nPos可能是负的,如有必要应该被强制转换为整数pScrollBar如果滚动消息来自一个滚动条控件,则包含了控件的指针。如果用户点击了窗口的滚动条,则这个参数为NULL。这个参数可能时临时的,不能被保存以供将来使用。
说明:
当用户点击窗口的水平滚动条时,框架调用这个成员函数。
通常在滚动块要被拖动的时候,滚动条代码SB_THUMBTRACK被应用程序用来给出一些反馈。
如果应用程序滚动了滚动条所控制的内容,它必须用SetScrollPos成员函数复位滚动块的位置。
注意 框架调用这个成员函数以允许你的应用程序处理一个Windows消息。传递给你的成员函数的参数反映了接收到消息时框架接收到的参数。如果你调用了这个函数的基类实现,则该实现将使用最初传递给消息的参数(而不是你提供给这个函数的参数)。

m_scrollbar.Create(SBS_HORZ|WS_CHILD|WS_VISIBLE,CRect(150,200,350,220),this,1014);m_scrollbar.SetScrollRange(0,100,0);m_scrollbar.SetScrollPos(20,true);
这样就可以把位置设置在20%的地方

接着响应WM_HSCROLL

void C控件学习Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){// TODO: 在此添加消息处理程序代码和/或调用默认值    switch(nSBCode){case SB_LINELEFT: m_scrollpos--; break;case SB_LINERIGHT:m_scrollpos++;break;case SB_PAGELEFT:m_scrollpos-=5;break;case SB_PAGERIGHT:m_scrollpos+=5;break;case SB_THUMBPOSITION:m_scrollpos=nPos;break;case SB_THUMBTRACK:m_scrollpos=nPos;break;default: break;}if(pScrollBar!=NULL&&pScrollBar==&m_scrollbar){m_scrollbar.SetScrollPos(m_scrollpos,true);}CDialog::OnHScroll(nSBCode, nPos, pScrollBar);}
当滚动条发出如下消息的时候就去改变显示的位置

第二个参数只有在

SB_THUMBPOSITION:  
SB_THUMBTRACK:
两个消息的时候值才是有效的

第三个参数是用来判断是那一个滚动条发出的,所以最后还有用一个指针来判断是否为我们需要的控件


滚动条控件的风格如下

  • SBS_BOTTOMALIGN   Used with the SBS_HORZ style. The bottom edge of the scroll bar is aligned with the bottom edge of the rectangle specified in theCreate member function. The scroll bar has the default height for system scroll bars.

  • SBS_HORZ   Designates a horizontal scroll bar. If neither theSBS_BOTTOMALIGN norSBS_TOPALIGN style is specified, the scroll bar has the height, width, and position given in theCreate member function.

  • SBS_LEFTALIGN   Used with the SBS_VERT style. The left edge of the scroll bar is aligned with the left edge of the rectangle specified in theCreate member function. The scroll bar has the default width for system scroll bars.

  • SBS_RIGHTALIGN   Used with the SBS_VERT style. The right edge of the scroll bar is aligned with the right edge of the rectangle specified in theCreate member function. The scroll bar has the default width for system scroll bars.

  • SBS_SIZEBOX   Designates a size box. If neither the SBS_SIZEBOXBOTTOMRIGHTALIGN norSBS_SIZEBOXTOPLEFTALIGN style is specified, the size box has the height, width, and position given in theCreate member function.

  • SBS_SIZEBOXBOTTOMRIGHTALIGN   Used with the SBS_SIZEBOX style. The lower-right corner of the size box is aligned with the lower-right corner of the rectangle specified in theCreate member function. The size box has the default size for system size boxes.

  • SBS_SIZEBOXTOPLEFTALIGN   Used with the SBS_SIZEBOX style. The upper-left corner of the size box is aligned with the upper-left corner of the rectangle specified in theCreate member function. The size box has the default size for system size boxes.

  • SBS_SIZEGRIP   Same as SBS_SIZEBOX, but with a raised edge.

  • SBS_TOPALIGN   Used with the SBS_HORZ style. The top edge of the scroll bar is aligned with the top edge of the rectangle specified in theCreate member function. The scroll bar has the default height for system scroll bars.

  • SBS_VERT   Designates a vertical scroll bar. If neither the SBS_RIGHTALIGN nor SBS_LEFTALIGN style is specified, the scroll bar has the height, width, and position given in theCreate member function.

垂直滚动条在属性里设置为SBS_VERT,对应的滚动条消息是WM_VSCROLL

四:CSliderCtrl控件的使用

CSliderCtrl的消息响应函数是和CScrollBar的消息响应函数是一样的WM_HSCROLL和WM_HSCROLL函数,区分是用第三个参数 

void C控件学习Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
如果你使用CSliderCtrl的函数,那么你就要将指针pScrollBar转换成指针CSliderCtrl

实例:

m_slider.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ|TBS_AUTOTICKS|TBS_BOTH,CRect(150,300,300,400),this,1015);//自动显示刻度线
m_slider.SetRange(0,10,true);//在上面和下面都会显示11个刻度

消息响应函数

void C控件学习Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

    switch(nSBCode)
{
case SB_LINELEFT:
m_scrollpos--;
break;
case SB_LINERIGHT:
m_scrollpos++;
break;
case SB_PAGELEFT:
m_scrollpos-=5;
break;
case SB_PAGERIGHT:
m_scrollpos+=5;
break;
case SB_THUMBPOSITION:
m_scrollpos=nPos;
break;
case SB_THUMBTRACK:
m_scrollpos=nPos;
break;
default:
break;
}
if(pScrollBar!=NULL&&(CSliderCtrl*)pScrollBar==&m_slider)//这里判断指针如果是我们所需要的则进入
{
//m_scrollbar.SetScrollPos(m_scrollpos,true);
((CSliderCtrl*)pScrollBar)->SetPos(m_scrollpos);//设置滚动条的位置
int sliderpos=m_slider.GetPos();
CString str;
str.Format(_T("当前位置是:%d"),m_scrollpos);//这个函数最后不要加&,否则会出错,文字前面也要加上_T宏
MessageBox(str);
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

效果图:



A slider control notifies its parent window of user actions by sending the parentWM_HSCROLL or WM_VSCROLL messages, depending on the orientation of the slider control. To handle these messages, add handlers for theWM_HSCROLL and WM_VSCROLL messages to the parent window. The OnHScroll and OnVScroll member functions will be passed a notification code, the position of the slider, and a pointer to the CSliderCtrl object. Note that the pointer is of type CScrollBar * even though it points to a CSliderCtrl object. You may need to typecast this pointer if you need to manipulate the slider control.

Rather than using the scroll bar notification codes, slider controls send a different set of notification codes. A slider control sends theTB_BOTTOM, TB_LINEDOWN, TB_LINEUP, andTB_TOP notification codes only when the user interacts with a slider control by using the keyboard. TheTB_THUMBPOSITION and TB_THUMBTRACK notification messages are only sent when the user is using the mouse. TheTB_ENDTRACK, TB_PAGEDOWN, and TB_PAGEUP notification codes are sent in both cases.

The following table lists the slider control notification messages and the events (virtual key codes or mouse events) that cause the notifications to be sent. (For a list of standard virtual key codes, see Winuser.h.)

Notification message

Event causing notification to be sent

TB_BOTTOM

VK_END

TB_ENDTRACK

WM_KEYUP (the user released a key that sent a relevant virtual key code)

TB_LINEDOWN

VK_RIGHT or VK_DOWN

TB_LINEUP

VK_LEFT or VK_UP

TB_PAGEDOWN

VK_NEXT (the user clicked the channel below or to the right of the slider)

TB_PAGEUP

VK_PRIOR (the user clicked the channel above or to the left of the slider)

TB_THUMBPOSITION

WM_LBUTTONUP following a TB_THUMBTRACK notification message

TB_THUMBTRACK

Slider movement (the user dragged the slider)

TB_TOP

VK_HOME