ListBox滚动条隐藏并受控
现有两个ListBox,
想隐藏ListBox2的竖向滚动条,然后由ListBox1的滚动条来操纵ListBox2,达到两个ListBox内容向下同步滚动效果?
望赐教。。。。
[解决办法]
GetScrollPos
SetScrollPos
用这两个函数即可
[解决办法]
呵,这是我自己瞎玩地,只是对于用鼠标点滚动条好使,如果要支持滚轮,键盘什么的,在改改吧
class TNoScrollListBox : public TListBox
{
public:
__fastcall virtual TNoScrollListBox(Classes::TComponent* AOwner)
: TListBox( AOwner )
{
}
protected:
virtual void __fastcall CreateParams(Controls::TCreateParams &Params)
{
TListBox::CreateParams( Params );
Params.Style &= ~WS_VSCROLL;
}
};
class TScrollListBox : public TListBox
{
public:
__fastcall virtual TScrollListBox(Classes::TComponent* AOwner,
TNoScrollListBox * pNoScrollListBox )
: TListBox( AOwner ), m_pNoScrollListBox( pNoScrollListBox )
{
}
protected:
virtual void __fastcall WndProc(Messages::TMessage &Message)
{
TListBox::WndProc( Message );
if ( m_pNoScrollListBox && ( WM_VSCROLL == Message.Msg ) )
{
m_pNoScrollListBox-> Perform( Message.Msg,
Message.WParam,
Message.LParam );
}
}
private:
TNoScrollListBox * m_pNoScrollListBox;
};
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TNoScrollListBox * pNoScrollListBox =
new TNoScrollListBox( this );
TScrollListBox * pScrollListBox =
new TScrollListBox( this, pNoScrollListBox );
pNoScrollListBox-> Parent = this;
pNoScrollListBox-> SetBounds( 10, 10, 100, 100 );
pScrollListBox-> Parent = this;
pScrollListBox-> SetBounds( 110, 10, 100, 100 );
for ( int i = 1; i < 100; ++i )
{
pNoScrollListBox-> Items-> Add( i );
pScrollListBox-> Items-> Add( i );
}
}
[解决办法]
wisn_wang:
不好意思,最近有事没来.
我说的设置WindowProc代码如下:
Unit1.h:
//---------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TListBox *ListBox1;
TListBox *ListBox2;
void __fastcall FormCreate(TObject *Sender);
private:// User declarations
TWndMethod FOldListBox2WndMethod;
void __fastcall NewListBox2WndMethod(Messages::TMessage &Message);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
unit1.cpp:
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for ( int i = 0; i < 100; ++i )
{
ListBox1-> Items-> Add(i);
ListBox2-> Items-> Add(i);
}
FOldListBox2WndMethod = ListBox2-> WindowProc;
ListBox2-> WindowProc = NewListBox2WndMethod;
SetWindowLong(ListBox1-> Handle, GWL_STYLE,
GetWindowLong(ListBox1-> Handle, GWL_STYLE) & ~WS_VSCROLL );
SetWindowPos( ListBox1-> Handle, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED );
}
//---------------------------------------
void __fastcall TForm1::NewListBox2WndMethod(Messages::TMessage &Message)
{
FOldListBox2WndMethod( Message );
if ( WM_VSCROLL == Message.Msg )
ListBox1-> Perform(Message.Msg, Message.WParam, Message.LParam);
}
但是
SetWindowLong(ListBox1-> Handle, GWL_STYLE,
GetWindowLong(ListBox1-> Handle, GWL_STYLE) & ~WS_VSCROLL );
SetWindowPos( ListBox1-> Handle, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED );
这个设置ListBox1滚动条隐藏的代码,不太好用,当一点ListBox2时,ListBox1滚动条又显示
我试了ShowScrollBar也不好使
另外我说的ScrollWindow我试了没试出来
呵
我感觉自己定义这两个类还是比较好用的
你的 "我在你的 TNoScrollListBox和TScrollListBox中定义ListBox1, ListBox2. "
是说的ListBox1, ListBox2使用TNoScrollListBox和TScrollListBox这两个类型吗
如果是的话直接写就可
ListBox1-> OnDrawItem = ListBox1OnDrawItem;