简体   繁体   English

在WTL中实现自定义绘制控件中的滚动

[英]Implementing scroll in custom drawn control in WTL

我已经创建了我自己的自定义绘制列表与复选框WTL,我想让它现在可滚动,事情是,我正在子类化我绘制的静态文本控件..我不知道静态控件是否支持任何滚动方式..无论如何我的问题是如何使我的自定义控件可滚动,我是否必须自己强制机制?

Yes, you'll have to implement it entirely by hand. 是的,你必须完全手工实施。 That's the drawback of not using a built-in control. 这是不使用内置控件的缺点。 It probably would have been a better idea to start off with a ListBox and then customize that to your desire. 从ListBox开始然后根据您的需求自定义它可能是一个更好的主意。 That way, you would get all of the scrolling, selection, and other logic for free. 这样,您就可以免费获得所有滚动,选择和其他逻辑。

The steps are roughly as follows (there are probably ATL/WTL idioms for some or all of these, but any ATL/WTL programmer can convert back and forth from raw Win32): 步骤大致如下(部分或全部可能有ATL / WTL惯用法,但任何ATL / WTL程序员都可以从原始Win32来回转换):

  1. Add the WS_HSCROLL and/or WS_VSCROLL window styles to your custom static control, depending on if you want a horizontal, vertical, or both scroll bars. WS_HSCROLL和/或WS_VSCROLL窗口样式添加到自定义静态控件,具体取决于您是否需要水平,垂直或两个滚动条。 You would add these to list of window styles passed in to the CreateWindow/CreateWindowEx function. 您可以将这些添加到传递给CreateWindow / CreateWindowEx函数的窗口样式列表中。

  2. By default, those scroll bars won't do anything at all. 默认情况下,这些滚动条根本不会执行任何操作。 You need to tell them what to do using the SetScrollInfo function. 您需要告诉他们使用SetScrollInfo函数做什么。 In your case: 在你的情况下:

    • The first parameter ( hwnd ) would be the handle to your control window. 第一个参数( hwnd )将是控制窗口的句柄。
    • The second parameter ( fnBar ) should be either SB_HORZ to adjust the horizontal scroll bar, or SB_VERT to adjust the vertical scroll bar. 第二个参数( fnBar )应该是SB_HORZ调整水平滚动条,或SB_VERT调整垂直滚动条。
    • The third parameter ( lpsi ) is a pointer to a SCROLLINFO structure, filled in with the desired scrolling parameters, including the current position of the thumb, the minimum and maximum values, and the "page" size used to set up the proportional scroll bar. 第三个参数( lpsi )是一个指向SCROLLINFO结构的指针,用所需的滚动参数填充,包括拇指的当前位置,最小值和最大值,以及用于设置比例滚动条的“页面”大小。
    • The fourth parameter ( fRedraw ) should probably be set to TRUE . 第四个参数( fRedraw )应该设置为TRUE

  3. You will also need the EnableScrollBar function to enable/disable the scroll bar as appropriate. 您还需要EnableScrollBar函数来根据需要启用/禁用滚动条。 Like the previous function, 像以前的功能一样,

    • hwnd is a handle to your control window hwnd是控件窗口的句柄
    • wSBflags is either SB_HORZ , SB_VERT , or SB_BOTH wSBflags要么SB_HORZSB_VERT ,或SB_BOTH
    • wArrows is one of the ESB_* values, depending on what you want wArrowsESB_*值之一,具体取决于您的需求

  4. Finally, you will want to write code in your custom control's window procedure to handle the WM_HSCROLL and/or WM_VSCROLL messages. 最后,您需要在自定义控件的窗口过程中编写代码来处理WM_HSCROLL和/或WM_VSCROLL消息。 These are sent to the window whenever the scroll bar is moved. 每当移动滚动条时,都会将它们发送到窗口。 Inside of the handler for these messages, you will want to do the following things to update the control's state: 在这些消息的处理程序内部,您将需要执行以下操作来更新控件的状态:

    • Call the SetScrollInfo function to update the thumb to its new position 调用SetScrollInfo函数将拇指更新到新位置
    • Redraw the contents of your control in accordance with the scrolled distance. 根据滚动距离重绘控件的内容。 There are multiple ways of doing this, but I'd probably use the ScrollWindowEx function. 有多种方法可以做到这一点,但我可能会使用ScrollWindowEx函数。

    The custom control's window procedure will also need to handle the WM_SIZE message to update the scroll bar state (by calling SetScrollInfo and/or EnableScrollBar ) in response to changes in the window's size. 自定义控件的窗口过程需要处理WM_SIZE消息以更新滚动条状态(通过调用SetScrollInfo和/或EnableScrollBar )以响应窗口大小的变化。

Cody Gray provided excellent introduction into adding support for scrolling, however what you also have is the help from the WTL itself. Cody Gray为添加滚动支持提供了很好的介绍,但是你也有来自WTL本身的帮助。

WTL's atlscrl.h offers you classes to inherit from and implement a custom window/control with scrolling. WTL的atlscrl.h为您提供了从滚动继承和实现自定义窗口/控件的类。

// Classes in this file:
//
// CScrollImpl<T>
// CScrollWindowImpl<T, TBase, TWinTraits>
// CMapScrollImpl<T>
// CMapScrollWindowImpl<T, TBase, TWinTraits>
// CFSBWindowT<TBase>
// CZoomScrollImpl<T>
// CZoomScrollWindowImpl<T, TBase, TWinTraits>
// CScrollContainerImpl<T, TBase, TWinTraits>
// CScrollContainer

Not so much code/snippets around to demo the use, but there is still one WTL sample that covers the basics and it should be a good starting point for you as well. 没有太多的代码/片段来演示使用,但仍然有一个WTL示例涵盖了基础知识,它也应该是一个很好的起点。 \\Samples\\BmpView has a class for scrollable bitmap: \\Samples\\BmpView有一个可滚动位图的类:

class CBitmapView : 
  public CScrollWindowImpl<CBitmapView>
{
public:

You will see it's really small and it covers most of the complexity. 你会发现它非常小,它涵盖了大部分的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM