Resizer.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.Data;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. namespace XmlNotepad {
  11.     public class PaneResizer : Control {
  12.         // panes could be horizontally aligned, or vertically aligned, 
  13.         // it will do the right thing.
  14.         Control pane1; 
  15.         Control pane2;
  16.         Point pos;
  17.         bool vertical = true;
  18.         int width = 5;
  19.         bool down;
  20.         Rectangle start;
  21.         Border3DStyle style = Border3DStyle.Raised;
  22.         HatchControl feedback;
  23.         
  24.         public int PaneWidth { 
  25.             get { return this.width; }
  26.             set { this.width = value; }
  27.         }
  28.         public Control Pane1 { 
  29.             get { return this.pane1; }
  30.             set { this.pane1 = value; }
  31.         }
  32.         public Control Pane2 { 
  33.             get { return this.pane2; }
  34.             set { this.pane2 = value; }
  35.         }    
  36.         
  37.         public Border3DStyle Border3DStyle {
  38.             get { return this.style; }
  39.             set { this.style = value; }
  40.         }
  41.         public bool Vertical {
  42.             get {return this.vertical;}
  43.             set { this.vertical = value; }
  44.         }
  45.         protected override void OnPaint(PaintEventArgs e){
  46.             base.OnPaint(e);
  47.             Border3DSide sides = this.Vertical ?  
  48.                 (Border3DSide.Left | Border3DSide.Right) : (Border3DSide.Top | Border3DSide.Bottom);
  49.             ControlPaint.DrawBorder3D(e.Graphics, 0, 0, this.Width, this.Height, this.style, sides);
  50.         }
  51.         
  52.         protected override void OnMouseEnter(EventArgs e){
  53.             this.Cursor = this.Vertical ? Cursors.VSplit : Cursors.HSplit;
  54.         }
  55.         protected override void OnMouseLeave(EventArgs e){
  56.             if (!this.down) this.Cursor = Cursors.Default;            
  57.         }
  58.         protected override void OnMouseDown(MouseEventArgs e){
  59.             if (this.feedback == null){
  60.                 this.feedback = new HatchControl();
  61.                 this.Parent.Controls.Add(this.feedback);
  62.                 int index = 0;
  63.                 foreach (Control c in this.Parent.Controls){
  64.                     if (c == this)
  65.                         break;
  66.                     index++;
  67.                 }
  68.                 this.Parent.Controls.SetChildIndex(this.feedback, index);
  69.             }
  70.             this.start = this.Bounds;
  71.             this.feedback.Bounds = this.Bounds;
  72.             this.pos = new Point(e.X, e.Y);
  73.             this.down = true;
  74.         }
  75.         protected override void OnMouseUp(MouseEventArgs e){
  76.             this.down = false;  
  77.             HandleMove(e.X, e.Y);
  78.             this.Parent.Controls.Remove(this.feedback);
  79.             this.Cursor = Cursors.Default;
  80.             this.feedback = null;
  81.         }
  82.         void HandleMove(int x, int y) {
  83.             if (this.Vertical){
  84.                 x = this.Left + (x - this.pos.X);
  85.                 if (x < this.pane1.Left+10) x= this.pane1.Left+10;
  86.                 if (x > this.pane2.Right-10) x = this.pane2.Right-10;
  87.                 y = this.Top;
  88.             } else {
  89.                 x = this.Left;
  90.                 y = this.Top + (y - this.pos.Y);
  91.                 if (y < this.pane1.Top + 20) y = this.pane1.Top + 20;
  92.                 if (y > this.pane2.Bottom - 20) y = this.pane2.Bottom - 20;
  93.             }
  94.             Rectangle newBounds = new Rectangle(x,y, this.Width, this.Height);
  95.             if (this.Bounds != newBounds ){
  96.                 if (!this.down){
  97.                     this.Bounds = newBounds;
  98.                     if (this.Vertical) {
  99.                         int diff = (this.start.Left - newBounds.Left);
  100.                         if (this.pane1 != null) {
  101.                             this.pane1.Width -= diff;
  102.                         }
  103.                         if (this.pane2 != null) {
  104.                             this.pane2.Width += diff;
  105.                         }
  106.                     } else {
  107.                         int diff = (this.start.Top - newBounds.Top);
  108.                         if (this.pane1 != null) {
  109.                             this.pane1.Height -= diff;
  110.                         }
  111.                         if (this.pane2 != null) {
  112.                             this.pane2.Height += diff;
  113.                         }
  114.                     }
  115.                     this.Parent.PerformLayout();
  116.                 } else {
  117.                     this.feedback.Bounds = newBounds;
  118.                 }
  119.             }
  120.         }
  121.         protected override void OnMouseMove(MouseEventArgs e){
  122.             if (this.down){
  123.                 HandleMove(e.X, e.Y);
  124.             }
  125.         }
  126.         internal class HatchControl : Control {
  127.             Pen pen;
  128.             Brush brush;
  129.             public HatchControl() {
  130.                 this.brush = new HatchBrush(HatchStyle.Percent50, Color.Black, Color.White);
  131.                 this.pen = new Pen(brush);
  132.             }
  133.             protected override void Dispose(bool disposing) {
  134.                 base.Dispose (disposing);
  135.                 if (disposing){
  136.                     pen.Dispose();
  137.                     brush.Dispose();
  138.                 }
  139.             }
  140.             protected override void OnPaint(PaintEventArgs e) {
  141.                 base.OnPaint (e);
  142.                 Graphics g = e.Graphics;
  143.                 if (this.Width>this.Height){
  144.                     pen.Width = this.Height;
  145.                     int y = this.Height/2;
  146.                     g.DrawLine(this.pen, 0, y, this.Right, y);
  147.                 } else {
  148.                     pen.Width = this.Width;
  149.                     int x = this.Width/2;
  150.                     g.DrawLine(this.pen, x, 0, x, this.Bottom);
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }