CSDN博客

img wu_07

BCB中利用鼠标拖放调整TControl的大小

发表于2004/7/2 11:26:00  1461人阅读

        对于继承自TWinControl的控件,如果要通过鼠标来调整其大小,只需要通过SetWindowLong在GWL_STYLE风格中添加WS_SIZEBOX即可。但是对于其他继承自TControl的对象,例如TGraphicControl,因为不是windows控件,所以无法使用这种方法。因此需要自己来进行处理。


以下给出BCB的示例代码(假定更改尺寸的对象是TBevel):

变量:


//定义枚举类型,判断鼠标从哪个位置进入到控件
enum TMoveInType{mtNone,mtLeft,mtTop,mtRight,mtBottom,mtTopLeft,mtTopRight,mtBottomLeft,mtBottomRight};
TMoveInType moveInType=mtNone;
TRect SizeRect;
bool SizeState =false;
const int SizeRange = 8;
__inline int Min(int a,int b){return (a__inline int Max(int a,int b){return (a>b) ? a : b;}

在控件的MouseMove、MouseUp事件中处理:

void __fastcall TForm1::Bevel1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
 TBevel *SdBevel;
 SdBevel = (TBevel *)Sender;
 //判断size开始的位置
 if(!SizeState)
 {
if (abs(Y -SdBevel->Height/2)<=SizeRange&&(abs(X)<=SizeRange))
 {
    moveInType=mtLeft;
    SdBevel->Cursor = crSizeWE;
 }
 else if (abs(Y -SdBevel->Height/2)<=SizeRange&&abs(SdBevel->Width -X<=SizeRange))
 {
    moveInType=mtRight;
    SdBevel->Cursor = crSizeWE;
 }
 else if (abs(X -SdBevel->Width/2)<=SizeRange&&(abs(Y)<=SizeRange))
 {
    moveInType=mtTop;
    SdBevel->Cursor = crSizeNS;
 }
 else if (abs(X -SdBevel->Width/2)<=SizeRange&&abs(SdBevel->Height - Y<=SizeRange))
 {
    moveInType=mtBottom;
    SdBevel->Cursor = crSizeNS;
 }
  else if ((abs(X)<= SizeRange)&&(abs(Y)<=SizeRange))
 {
    moveInType=mtTopLeft;
    SdBevel->Cursor = crSizeNWSE;
 }
 else if (abs(SdBevel->Width -X <= SizeRange)&&(abs(Y)<=SizeRange))
 {
    moveInType=mtTopRight;
    SdBevel->Cursor = crSizeNESW;
 }
  else if ((abs(X)<= SizeRange)&&abs(SdBevel->Height - Y<=SizeRange))
 {
    moveInType=mtBottomLeft;
    SdBevel->Cursor = crSizeNESW;
 }
  else if (abs(SdBevel->Width -X)<= SizeRange&&abs(SdBevel->Height - Y<=SizeRange))
 {
    moveInType=mtBottomRight;
    SdBevel->Cursor = crSizeNWSE;
 }
 else if (!SizeState)
 {
   SdBevel->Cursor = crArrow;
   moveInType=mtNone;
 }
}
if ((moveInType!=mtNone)&&(Shift.Contains(ssLeft)))
{
    SizeState =true;
    Canvas->Pen->Mode = pmNotXor;
    Canvas->Pen->Style = psDot;
    Canvas->Pen->Color = clDkGray;
    Canvas->Brush->Style = bsClear;
    //利用xor,将上次的 SizeRect抹去
    Canvas->Rectangle(SizeRect.Left,SizeRect.Top,SizeRect.Right,SizeRect.Bottom);

    TRect BRect;
    //可能Rect的Right>Left,Top>Bottom,所以利用临时的BRect做一些调整
    BRect = SdBevel->BoundsRect;

    switch (moveInType)
    {
      case mtLeft:
        BRect.Left += X;
        break;
      case mtRight:
        BRect.Right =BRect.Left + X;
        break;
      case mtTop:
        BRect.Top += Y;
        break;
      case mtBottom:
        BRect.Bottom = BRect.Top+Y;
        break;
      case  mtTopLeft:
        BRect.Left += X;
        BRect.Top += Y;
        break;
      case  mtTopRight:
        BRect.Right =BRect.Left + X;
        BRect.Top += Y;
        break;
      case  mtBottomLeft:
        BRect.Left += X;;
        BRect.Bottom = BRect.Top+Y;
        break;
      case  mtBottomRight:
        BRect.Right =BRect.Left + X;
        BRect.Bottom = BRect.Top+Y;
        break;
    }


    SizeRect.Left= Min(BRect.Left,BRect.Right);
    SizeRect.Right= Max(BRect.Left,BRect.Right);
    SizeRect.Top= Min(BRect.Top,BRect.Bottom);
    SizeRect.Bottom=Max(BRect.Top,BRect.Bottom);
    //画SizeRect
    Canvas->Rectangle(SizeRect.Left,SizeRect.Top,SizeRect.Right,SizeRect.Bottom);
}

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Bevel1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
    if (SizeState)
    {
     //利用xor,将画的最后一个 SizeRect抹去
     Canvas->Rectangle(SizeRect.Left,SizeRect.Top,SizeRect.Right,SizeRect.Bottom);
     SizeState =false;
     ((TBevel *)Sender)->BoundsRect=SizeRect;
     SizeRect =Rect(0,0,0,0);
     //恢复初始的参数
     Canvas->Pen->Mode = pmCopy;
     Canvas->Brush->Style = bsSolid;
     Canvas->Pen->Width = 1;
     Canvas->Pen->Color = clBlack         ;
    }
}
//---------------------------------------------------------------------------

 

0 0

相关博文

我的热门文章

img
取 消
img