MFC控件增加滚动条

以Listbox Control为例。

        首先,子类化CListBox,重载AddString和InsertString函数;并且根据加入的字符,判断行的宽度,实现RefushHorizontalScrollBar函数,如下:

//HorScrollListBox.h
#include <windows.h>
#include <afxwin.h>
// CHorScrollListBox CListBox

class CHorScrollListBox : public CListBox
{
	DECLARE_DYNAMIC(CHorScrollListBox)

public:
	CHorScrollListBox(CWnd* pParent = NULL);   // standard constructor
	virtual ~CHorScrollListBox();

	int AddString(LPCTSTR lpszItem);
	int InsertString(int nIndex, LPCTSTR lpszItem);

	// 计算水平滚动条宽度
	void RefushHorizontalScrollBar(void);

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
};


//
// HorScrollListBox.cpp : implementation file
//

#include "stdafx.h"
#include "HorScrollListBox.h"
#include "afxdialogex.h"


// CHorScrollListBox CListBox
IMPLEMENT_DYNAMIC(CHorScrollListBox, CListBox)

CHorScrollListBox::CHorScrollListBox(CWnd* pParent /*=NULL*/)
	: CListBox()
{

}

CHorScrollListBox::~CHorScrollListBox()
{
}

void CHorScrollListBox::DoDataExchange(CDataExchange* pDX)
{
	CListBox::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CHorScrollListBox, CListBox)
END_MESSAGE_MAP()


// CHorScrollListBox message handlers
int CHorScrollListBox::AddString(LPCTSTR lpszItem) {
	int nResult = CListBox::AddString(lpszItem);
	RefushHorizontalScrollBar();
	return nResult;
}

int CHorScrollListBox::InsertString(int nIndex, LPCTSTR lpszItem) {
	int nResult = CListBox::InsertString(nIndex, lpszItem);
	RefushHorizontalScrollBar();
	return nResult;
}

// 计算水平滚动条宽度
void CHorScrollListBox::RefushHorizontalScrollBar(void) {
	CDC *pDC = this->GetDC();
	if (NULL == pDC)
	{
		return;
	}

	int nCount = this->GetCount();
	if (nCount < 1)
	{
		this->SetHorizontalExtent(0);
		return;
	}

	int nMaxExtent = 0;
	CString szText;
	for (int i = 0; i < nCount; ++i)
	{
		this->GetText(i, szText);
		CSize &cs = pDC->GetTextExtent(szText);
		if (cs.cx > nMaxExtent)
		{
			nMaxExtent = cs.cx;
		}
	}
	this->SetHorizontalExtent(nMaxExtent);
}

         在ui界面给界面上的listbox control增加变量,选择刚才子类化CListBox的子类类型,如下:

           选择ui界面上的listbox control修改属性Horizontal Scroll为True,如下:

        添加完毕,运行可以看到效果。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC中,如果要显示大图片并且需要滚动条,可以使用CScrollView类来实现。 首先,在资源编辑器中创建一个对话框,然后添加一个静态图像控件(ID为IDC_STATIC_IMAGE)和一个滚动条控件(ID为IDC_SCROLLBAR)。在对话框类的头文件中添加两个成员变量: ```cpp CStatic m_StaticImage; CScrollBar m_ScrollBar; ``` 在OnInitDialog()函数中,进行控件的初始化设置,包括定义滚动条的范围和页面大小: ```cpp BOOL CMyDialog::OnInitDialog() { ... // 获取静态图像控件的客户区矩形 CRect rect; m_StaticImage.GetClientRect(rect); // 设置滚动条的范围和页面大小 m_ScrollBar.SetScrollRange(0, rect.Width()); m_ScrollBar.SetScrollPage(rect.Width() / 10); ... } ``` 接下来,在OnHScroll()事件中处理滚动条的滚动事件,以更新图像的显示区域: ```cpp void CMyDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { if(pScrollBar->GetDlgCtrlID() == IDC_SCROLLBAR) { // 获取当前滚动条的位置 int nScrollPos = m_ScrollBar.GetScrollPos(); // 显示图像的起始位置 int nImagePos = nScrollPos; // 设置静态图像控件的显示区域 m_StaticImage.SetWindowPos(NULL, -nImagePos, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE); } } ``` 最后,在OnInitDialog()函数中添加滚动条的消息映射: ```cpp BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx) ... ON_WM_HSCROLL() END_MESSAGE_MAP() ``` 这样,当滚动条滚动时,图像的显示位置也会随之改变,实现了大图片的显示和滚动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值