Visual CSharp .NET - Tabcontrol Custom

 
Vista:
sin imagen de perfil
Val: 1
Ha aumentado su posición en 12 puestos en Visual CSharp .NET (en relación al último mes)
Gráfica de Visual CSharp .NET

Tabcontrol Custom

Publicado por Luis Eduardo (1 intervención) el 28/01/2020 14:48:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
 
namespace UI.Controls
{
  ///
 
  /// Summary description for DisableTabControl.
  /// use DisableTabControl, you can really disable individual TabPage by
  /// setting Enabled property to false(you cannot switch to dimmed page by mouse
  /// or key board unless the current selected page is dimmed). usage:
  /// 1. add reference Control.dll or the Control project
  /// 2. using DigitalLab.UI.Controls
  /// 3. Design your form
  /// 4. find the variable name of your TabControl(Declaration and
  /// initizlization)
  /// 4.1 private System.Windows.Forms.TabControl tabControl1; ==>
  ///     private DigitalLab.UI.DisableTabControl tabControl1;
  /// 4.2 in InitializeComponent()
  ///      tabControl1 = new System.Windows.Forms.TabControl(); ==>
  ///      tabControl1 = new DigitalLab.UI.Controls.DisableTabControl() ==>
  /// 5. Switch to form designer
  /// 5.1 set the Porperty DrawMode to OwnerDrawFixed
  /// 5.2 add an handler to the event DrawItem
  ///     add the following code to the event handler:
  ///     tabControl1.DisableTabControl_DrawItem(sender,e);
  /// 6  Enable/Disable a tabpage by
  ///     EnableDisablePage(tabpage, true/false)
  ///
  public class DisableTabControl : System.Windows.Forms.TabControl
  {
 
    public DisableTabControl()
    {
    }
 
    protected override void WndProc(ref Message m)
    {
      try
      {
    const int WM_LBUTTONDOWN = 0x201;
    if( m.Msg == WM_LBUTTONDOWN )
    {
      Point pt = new Point( (int)m.LParam );
      for(int index = 0; index < base.TabPages.Count; index++)
      {
        if( GetTabRect(index).Contains(pt) )
        {
          if (TabPages[index].Enabled == true)
          {
        base.WndProc(ref m);
          }
          return;
        }
      }
    }
    base.WndProc (ref m);
      }
      catch(Exception ex)
      {
    Debug.Assert(false, ex.ToString() );
      }
    }
 
    protected override void OnKeyDown(KeyEventArgs e)
    {
      int currentIndex = this.SelectedIndex;
      try
      {
    if( e.KeyCode == Keys.Left && !(e.Alt && !e.Control ) )
    {
      int rewind = 0;
      int i = currentIndex-1;
      if( i == -1) i = TabPages.Count -1;
 
      for(; rewind < 2 && i >= 0; i--)
      {
        if( TabPages[i].Enabled )
        {
          this.SelectedIndex = i;
          break;
        }
        if( i == 0 )
        {
          rewind++;
          i = TabPages.Count;
        }
      }
      e.Handled = true;
    }
    else if(e.KeyCode == Keys.Right && !(e.Alt && !e.Control) )
    {
      int rewind = 0;
      int i = currentIndex + 1;
      if( i == TabPages.Count) i = 0;
      for(; rewind < 2 && i < TabPages.Count; i++)
      {
        if( TabPages[i].Enabled )
        {
          this.SelectedIndex = i;
          break;
        }
        if( i == TabPages.Count - 1)
        {
          rewind ++;
          i = -1;
        }
      }
      e.Handled = true;
    }
    else if( (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) &&
        !(e.Alt && !e.Control) )
    {
      if(this.Multiline == false)
      {
        e.Handled = false;
        base.OnKeyDown (e);
        return;
      }
      int targetIdx = -1;
      Rectangle curr_rect = GetTabRect(currentIndex);
      for(int i = 0; i < TabPages.Count; i++)
      {
        if( i == currentIndex) continue;
        if( curr_rect.Contains(GetTabRect(i).X, curr_rect.Y) )
        {
          targetIdx = i;
          break;
        }
      }
      if( targetIdx != -1 && TabPages[targetIdx].Enabled == true)
      {
        this.SelectedIndex = targetIdx;
      }
      e.Handled = true;
    }
    base.OnKeyDown (e);
      }
      catch(Exception ex)
      {
    Debug.Assert(false, ex.ToString() );
      }
    }
 
    ///
 
    /// you can also use tabPage.Enabled = false; tabControl.Refresh() directly
    /// this method just give you a way to ignore Refresh();
    ///
    ///
    public void EnableDisablePage(TabPage pTabPage, bool enable )
    {
      bool curr_enable = pTabPage.Enabled ;
      pTabPage.Enabled = enable;
      if( curr_enable != enable)
    this.Refresh();
    }
 
    public void DisableTabControl_DrawItem(object sender , System.Windows.Forms.DrawItemEventArgs e)
    {
      try
      {
    int intOffsetLeft ;
    int intOffsetTop ;
    RectangleF r = RectangleF.op_Implicit(e.Bounds);
    RectangleF r2;
    SolidBrush ItemBrush = new SolidBrush(this.BackColor);
    Brush b = null;
    if( this.TabPages[e.Index].Enabled )
      b = Brushes.Black;
    else
      b = Brushes.Gray;
 
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
 
    Bitmap im = null;
    if(this.TabPages[e.Index].ImageIndex != -1 )
    {
      im = this.ImageList.Images[this.TabPages[e.Index].ImageIndex] as Bitmap;
    }
 
    if(this.TabPages[e.Index].ImageIndex != -1 )
      r2 = new RectangleF(r.X + (im.Width / 2), r.Y, r.Width, r.Height);
    else
      r2 = new RectangleF(r.X, r.Y, r.Width, r.Height);
 
    if( (int)(e.State & DrawItemState.Selected) != 0 )
    {
      e.Graphics.FillRectangle(ItemBrush, e.Bounds);
      e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, b, r2, sf);
      //e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, Brushes.Red, r2, sf);
      intOffsetLeft = 5;
      intOffsetTop = 5 ;
    }
    else
    {
      e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, b, r2, sf);
      //e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, Brushes.Blue, r2, sf);
      intOffsetLeft = 2;
      intOffsetTop = 2 ;
    }
 
    if(this.TabPages[e.Index].ImageIndex != -1 )
    {
      this.ImageList.Draw(e.Graphics, Convert.ToInt32(r.Left) +
          intOffsetLeft, Convert.ToInt32(r.Top) + intOffsetTop,
          this.TabPages[e.Index].ImageIndex);
    }
      }
      catch(Exception ex)
      {
    Debug.Assert(false, ex.ToString() );
      }
    }
 
  }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder