如何实现控件的拉伸?

yyuc20000 2006-10-18 12:33:37
例如动态生成一个Edit控件,我怎样才能实现它的各个方向的拉伸呢?
...全文
241 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
truelove7283159 2006-10-31
  • 打赏
  • 举报
回复
上面那些方法调来调去都要调到resize()VCL就是这样干的。
truelove7283159 2006-10-31
  • 打赏
  • 举报
回复
在控件的resize方法中处理。
i_love_pc 2006-10-31
  • 打赏
  • 举报
回复
学习.
yyuc20000 2006-10-30
  • 打赏
  • 举报
回复
好,我试试。谢谢。
jiangshx 2006-10-18
  • 打赏
  • 举报
回复
////////////////////////////.h文件

class TForm1 : public TForm
{
__published:// IDE-managed Components
TButton *Button2;
TButton *Button1;

void __fastcall ControlMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall ControlMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall ControlMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
private:// User declarations
int Type;
enum rectINFO{
RIGHT=1,
LEFT=2,
BOTTOM=4,
TOP=8,
};
TPoint Pos0;
TCursor __fastcall GetCursor(int X, int Y, TObject *Sender);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};

////////////////////////////.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)
{
TButton *newbtn=new TButton(this);
newbtn->Parent=Form1;
newbtn->Left=100;
newbtn->Top=100;
newbtn->Height=25;
newbtn->Width=60;
newbtn->Caption="新的按钮";
newbtn->OnMouseUp=ControlMouseUp;
newbtn->OnMouseDown=ControlMouseDown;
newbtn->OnMouseMove=ControlMouseMove;
}
//-------------------------------------------------------------
TCursor __fastcall TForm1::GetCursor(int X, int Y, TObject *Sender)
{
TControl* Control;

Type = 0;
Control = dynamic_cast<
TControl*>(Sender);
if (Control == NULL)
return crDefault;
if (Control->ClientWidth-X<=4)
Type |= RIGHT;
else if(X <= 4)
Type |= LEFT;
if (Control->ClientHeight-Y<=4)
Type |= BOTTOM;
else if(Y <= 4)
Type |= TOP;
if (Type==LEFT || Type==
RIGHT)
return crSizeWE;
if (Type==TOP || Type==
BOTTOM)
return crSizeNS;
if (Type==(LEFT|TOP) ||
Type==(RIGHT|BOTTOM))
return crSizeNWSE;
if (Type==(LEFT|BOTTOM)
|| Type==(RIGHT|TOP))
return crSizeNESW;
return crDefault;
}
//------------------------------------------------------------
void __fastcall TForm1::ControlMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
int dx, dy;
TRect Rect;
TPoint Pos;
TWinControl* Control;

Control = dynamic_cast<
TWinControl*>(Sender);
if (Control==NULL) return;

if (Mouse->Capture==Control->Handle) {
Pos.x = X; Pos.y = Y;
::ClientToScreen(Control
->Handle, &Pos);
dx = Pos.x - Pos0.x;
dy = Pos.y - Pos0.y;
GetWindowRect(Control->
Handle, &Rect);
if (Type & LEFT)
Rect.left += dx;
else if(Type & RIGHT)
Rect.right += dx;
if (Type & TOP)
Rect.top += dy;
else if(Type & BOTTOM)
Rect.bottom += dy;
MapWindowPoints(0, Handle,
(POINT*)&Rect, 2);
MoveWindow(Control->Handle,
Rect.left, Rect.top, Rect
.Width(), Rect.Height(),
TRUE);
UpdateWindow(Control->Handle);
Pos0.x = Pos.x; Pos0.y = Pos.y;
}
else {
TCursor cursor = GetCursor(X, Y,
Sender);
if (cursor != Control->Cursor) {
Control->Cursor = cursor;
::SetCursor(Screen->Cursors[
cursor]);
}
}
}
//--------------------------------------------------------
void __fastcall TForm1::ControlMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{

TWinControl* Control;
Control = dynamic_cast<
TWinControl*>(Sender);
if (Type==0){
if(Button == mbLeft){//如果是左键按下控件,则可以移动控件
ReleaseCapture();
SendMessage( Control->Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
return;
}
if (Type!=0 && Control!=0) {
Pos0.x = X; Pos0.y = Y;
::ClientToScreen(Control->
Handle, &Pos0);
Mouse->Capture = Control
->Handle;
}

}
//------------------------------------------------------
void __fastcall TForm1::ControlMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TWinControl* Control;

if (Type==0) return;
Control = dynamic_cast<
TWinControl*>(Sender);
if (Control!=NULL && Mouse->
Capture==Control->Handle)
Mouse->Capture = NULL;
}
BlueDeepOcean 2006-10-18
  • 打赏
  • 举报
回复
参考这个帖子吧,道理是一样的:

http://community.csdn.net/Expert/topic/4750/4750745.xml?temp=.1326563
BlueDeepOcean 2006-10-18
  • 打赏
  • 举报
回复
需要动态定义它的OnMouseDown,OnMouseMove和OnMouseUp事件。并在OnMouseMove中处理它的Width和Height的变化事件。利用POINT决定x和y。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧