想用GDI+2.0做设计器,但在实现过程中遇到大麻烦

今天花了基本上大天半的时间写了一个基于GDI+的设计器半成品,(.NET 2.0)

以前在CNBLOGS.COM上看到有位朋友也用GDI+做过报表设计器,但我已经不记得是哪位了
希望这们朋友看到后能留个言,我有很多东西想请教一下,
这个程序是完全用GDI+来完成的,但最大的麻烦是我不能很好的安排整体的PAINT事件,
不论是画在IMAGE上或是直接画在容器中,都会有很严重的闪烁,

这个东西只是一个自己的测试,最终的目的还是为了做出一个类似VB6的窗体设计器,
在现在的设计器中包括了
     FocusBox对象选择框
     ReportDesigner基于Panel的设计容器
     RpUIComponent所有设计对象的基础类,类似于System.Windows.Forms.Control

     RpLabel基于RpUIComponent的标签对象

目前这个东西支持自定义组件的扩展,也就是可以重RpUIComponent中派生出自定义组件,当然现在的功能还是很弱的,

源码下载地址:https://files.cnblogs.com/pvistely/GdiDsg.rar
 希望大家指点一下,有怎么样更好的进行GDI画图的方法或模式,





哈,意外发现,把FocusBox类处理一下就可以直接与一般控件使用,而且效果不错,嘿嘿嘿,意外呀~~~~
  public class FocusBox
  
{
    Control mBindCtrl 
= null;
    Graphics mDrawGrp 
= null;
    Control mParent 
= null;
    Rectangle[] mDot 
= new Rectangle[9];
    
bool mMouseD = false;
    
bool mInRact = false;
    Rectangle mClientRect 
= new Rectangle();
    Cursor[] mDotCur 
=
      Cursors.SizeAll, 
      Cursors.SizeNWSE, 
      Cursors.SizeNS, 
      Cursors.SizeNESW, 
      Cursors.SizeWE, 
      Cursors.SizeWE, 
      Cursors.SizeNESW, 
      Cursors.SizeNS,
      Cursors.SizeNWSE 
    }
;
    
int mCurPos = -1;
    Point mMDPos 
= new Point(00);
    
public FocusBox(Control pRd, Control pBindCtrl)
    
{
      mParent 
= pRd;
      mBindCtrl 
= pBindCtrl;
      mDrawGrp 
= Graphics.FromHwnd(pRd.Handle);
      mParent.Paint 
+= new PaintEventHandler(mParent_Paint);
      mParent.MouseMove 
+= new MouseEventHandler(mParent_MouseMove);
      mParent.MouseDown 
+= new MouseEventHandler(mParent_MouseDown);
      mParent.MouseUp 
+= new MouseEventHandler(mParent_MouseUp);
      mClientRect 
= pBindCtrl.ClientRectangle;
      DrawFocus();
    }

    
void mParent_MouseUp(object sender, MouseEventArgs e)
    
{
      mMDPos 
= new Point(00);
      mCurPos 
= -1;
      mMouseD 
= false;
    }

    
void mParent_MouseDown(object sender, MouseEventArgs e)
    
{
      
if (e.Button == MouseButtons.Left)
      
{
        mMDPos 
= e.Location;
        mMouseD 
= true;
      }

    }

    
void mParent_MouseMove(object sender, MouseEventArgs e)
    
{
      
if (!mMouseD)
      
{
        mParent.Cursor 
= Cursors.Default;
        mInRact 
= (new Rectangle(mDot[1].Left, mDot[1].Top, mDot[8].Right, mDot[8].Bottom)).Contains(e.Location);
        
if (mInRact)
        
{
          mParent.Cursor 
= Cursors.SizeAll;
          mCurPos 
= 0;
        }

        
for (int i = 1; i < 9; i++)
        
{
          
if (mDot[i].Contains(e.Location))
          
{
            mParent.Cursor 
= mDotCur[i];
            mCurPos 
= i;
          }

        }

      }

      
if (mCurPos == -1return;
      Rectangle tmpRect 
= new Rectangle();
      
////tmpRect.Size = mClientRect.Size;
      
////tmpRect.Location = mClientRect.Location;

      tmpRect.Size = mBindCtrl.Size;
      tmpRect.Location 
= mBindCtrl.Location;
      Point changed 
= CalcSize(mDot[mCurPos].Location, e.Location);
      changed 
= CalcSize(mMDPos, e.Location);
      
if (mMouseD)
      
{
        
switch (mCurPos)
        
{
          
case 0:
            tmpRect.Location 
= tmpRect.Location + (new Size(changed.X, changed.Y));
            
break;
          
case 1:
            tmpRect.Location 
= tmpRect.Location + (new Size(changed.X, changed.Y));
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X * (-1), changed.Y * (-1)));
            
break;
          
case 2:
            tmpRect.Location 
= tmpRect.Location + (new Size(0, changed.Y));
            tmpRect.Size 
= tmpRect.Size + (new Size(0, changed.Y * (-1)));
            
break;
          
case 3:
            tmpRect.Location 
= tmpRect.Location + (new Size(0, changed.Y));
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X, changed.Y * -1));
            
break;
          
case 4:
            tmpRect.Location 
= tmpRect.Location + (new Size(changed.X, 0));
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X * -10));
            
break;
          
case 5:
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X, 0));
            
break;
          
case 6:
            tmpRect.Location 
= tmpRect.Location + (new Size(changed.X, 0));
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X * -1, changed.Y));
            
break;
          
case 7:
            tmpRect.Size 
= tmpRect.Size + (new Size(0, changed.Y));
            
break;
          
case 8:
            tmpRect.Size 
= tmpRect.Size + (new Size(changed.X, changed.Y));
            
break;
        }

      }

      mMDPos 
= e.Location;
      
if (mBindCtrl.Location == tmpRect.Location && mBindCtrl.Size == tmpRect.Size)
        
return;
      mBindCtrl.Location 
= tmpRect.Location;
      mBindCtrl.Size 
= tmpRect.Size;
      
////mClientRect.Location = tmpRect.Location;
      
////mClientRect.Size = tmpRect.Size;

      DrawFocus();
      mParent.Refresh();
    }

    
void mParent_Paint(object sender, PaintEventArgs e)
    
{
      
//mParent.DrawGraphic.Clear(Color.White);
      DrawFocus();
    }

    Point CalcSize(Point po1, Point po2)
    
{
      Point s 
= new Point();
      s.X 
= po2.X - po1.X;
      s.Y 
= po2.Y - po1.Y;
      
return s;
    }

    
void DrawFocus()
    
{
      
////Rectangle tmpFocusRect = new Rectangle(mClientRect.Location - new Size(3, 3), mClientRect.Size + new Size(6, 6));
      Rectangle tmpFocusRect = new Rectangle(mBindCtrl.Location - new Size(33), mBindCtrl.Size + new Size(66));
      
//Bitmap tmpBmp = new Bitmap(mBindCtrl.Size.Width + 14, mBindCtrl.Size.Height + 14, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
      
//mImg = Image.FromHbitmap(tmpBmp.GetHbitmap());
      
//Graphics g = Graphics.FromImage(mImg);
      
//ControlPaint.DrawFocusRectangle(g, tmpFocusRect);
      ControlPaint.DrawFocusRectangle(mDrawGrp, tmpFocusRect);

      mDot[
1].Location = mBindCtrl.Location - new Size(77);
      
//mDot[1].Location = new Point(0, 0);
      mDot[2].Location = mDot[1].Location + new Size(mBindCtrl.Width / 2 + 40);
      mDot[
3].Location = mDot[1].Location + new Size(mBindCtrl.Width + 70);

      mDot[
4].Location = mDot[1].Location + new Size(0, mBindCtrl.Height / 2 + 4);
      mDot[
5].Location = mDot[1].Location + new Size(mBindCtrl.Width + 7, mBindCtrl.Height / 2 + 4);

      mDot[
6].Location = mDot[1].Location + new Size(0, mBindCtrl.Height + 7);
      mDot[
7].Location = mDot[1].Location + new Size(mBindCtrl.Width / 2 + 4, mBindCtrl.Height + 7);
      mDot[
8].Location = mDot[1].Location + new Size(mBindCtrl.Width + 7, mBindCtrl.Height + 7);

      
for (int i = 1; i <= 8; i++)
      
{
        mDot[i].Size 
= new Size(66);
        mDrawGrp.FillRectangle(
new SolidBrush(SystemColors.Highlight), mDot[i]);
        mDrawGrp.DrawRectangle(
new Pen(SystemColors.HighlightText), mDot[i]);
        
//g.FillRectangle(new SolidBrush(SystemColors.Highlight), mDot[i]);
        
//g.DrawRectangle(new Pen(SystemColors.HighlightText), mDot[i]);
      }

      
//mDrawGrp.DrawImage(mImg,mBindCtrl.Location-new Size(7,7));
    }

    
void DrawControl()
    
{
    }

    
~FocusBox()
    
{
      mBindCtrl 
= null;
      mDrawGrp 
= null;
      
if (mParent != null) mParent.Paint -= mParent_Paint;
      mParent 
= null;
    }

    
public void Dispose()
    
{
      mParent.Cursor 
= Cursors.Default;
      mBindCtrl 
= null;
      mDrawGrp 
= null;
      mParent.Paint 
-= mParent_Paint;
      mParent.MouseMove 
-= mParent_MouseMove;
      mParent.MouseUp 
-= mParent_MouseUp;
      mParent.MouseDown 
-= mParent_MouseDown;
      mParent 
= null;
    }

  }

posted @ 2005-09-18 19:29  Pvistely  阅读(3290)  评论(11编辑  收藏  举报