lunes, 27 de abril de 2020

Visual Studio 2003 .NET Panel con Scroll solo vertical o horizontal


Si estás trabajando con Visual Studio 2003 .NET es decir con el framework 1.1 puedes que tenga la siguiente problemática dentro de los controles de tipo “Panel”, ya que dicho panel tienes la opción de “AutoScroll” que esta opción habilitaría el scroll dentro de vuestro panel pero claro podría darse el caso que solo queremos que el scroll aparezca en horizontal o el vertical solamente. Esto por defecto no está implementado en el framework 1.1 de Microsoft pero buscando por internet encontrado una solución para poder realizar esta tarea.

Vamos a trabaja con el siguiente ejemplo:


Resultado:


Como vemos tenemos la propiedad de “AutoScroll” activado y aparece los dos scroll tanto el vertical como el horizontal. Este ejemplo vamos es modificar nuestra aplicación para solo mostrar el Scroll vertical.

Paso 1: Añadir nueva clase a nuestro proyecto.

Esta clase se base el componente System.Windows.Forms.Panelpero añadiendo nuevas funcionalidades y propiedades.

ScrollablePanel.cs

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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
 
namespace CustomAutoScrollPanel
{
 using System;
 using System.Windows.Forms;
 using System.Runtime.InteropServices ;
 
 /// <summary>
 /// Description résumée de ScrollablePanel.
 /// </summary>
 public class ScrollablePanel: System.Windows.Forms.Panel
 {
 
  #region Delegates & events
 
  public event System.Windows.Forms.ScrollEventHandler ScrollHorizontal;
  public event System.Windows.Forms.ScrollEventHandler ScrollVertical;
  public event System.Windows.Forms.MouseEventHandler  ScrollMouseWheel;
 
  #endregion
 
  #region Const
 
  private const int SB_LINEUP = 0;
  private const int SB_LINEDOWN = 1;
  private const int SB_PAGEUP = 2;
  private const int SB_PAGEDOWN = 3;
  private const int SB_THUMBPOSITION = 4;
  private const int SB_THUMBTRACK = 5;
  private const int SB_TOP = 6;
  private const int SB_BOTTOM = 7;
  private const int SB_ENDSCROLL = 8;
 
  private const int WM_HSCROLL = 0x114;
  private const int WM_VSCROLL = 0x115;
  private const int WM_MOUSEWHEEL = 0x020A;
  private const int WM_NCCALCSIZE =0x0083;
  private const int WM_PAINT =0x000F;
  private const int WM_SIZE =0x0005;
 
  private const uint SB_HORZ = 0;
  private const uint SB_VERT = 1;
  private const uint SB_CTL = 2;
  private const uint SB_BOTH = 3;
 
  private const uint  ESB_DISABLE_BOTH = 0x3;
  private const uint  ESB_ENABLE_BOTH = 0x0;
 
  private const int  MK_LBUTTON = 0x01;
  private const int  MK_RBUTTON = 0x02;
  private const int  MK_SHIFT = 0x04;
  private const int  MK_CONTROL = 0x08;
  private const int  MK_MBUTTON = 0x10;
  private const int  MK_XBUTTON1 = 0x0020;
  private const int  MK_XBUTTON2 = 0x0040;
 
  #endregion
 
  #region Vars
   
  private bool enableAutoHorizontal = true;
  private bool enableAutoVertical = true;
  private bool visibleAutoHorizontal = true;
  private bool visibleAutoVertical = true;
 
  private int autoScrollHorizontalMinimum = 0;
  private int autoScrollHorizontalMaximum = 100;
 
  private int autoScrollVerticalMinimum = 0;
  private int autoScrollVerticalMaximum = 100;
 
  #endregion
 
  #region Constructor
 
  public ScrollablePanel()
  {
   this.Click += new EventHandler(ScrollablePanel_Click);
   this.AutoScroll = true;
  }
 
  #endregion
 
  #region Properties
 
  public int AutoScrollHPos
  {
   get { return GetScrollPos(this.Handle, (int)SB_HORZ); }
   set { SetScrollPos(this.Handle, (int)SB_HORZ, value, true); }
  }
 
  public int AutoScrollVPos
  {
   get { return GetScrollPos(this.Handle, (int)SB_VERT); }
   set { SetScrollPos(this.Handle, (int)SB_VERT, value, true); }
  }
 
  public int AutoScrollHorizontalMinimum
  {
   get { return this.autoScrollHorizontalMinimum;  }
   set {
    this.autoScrollHorizontalMinimum = value;
    SetScrollRange(this.Handle, (int)SB_HORZ, autoScrollHorizontalMinimum, autoScrollHorizontalMaximum, true);  
   }
  }
 
  public int AutoScrollHorizontalMaximum
  {
   get { return this.autoScrollHorizontalMaximum;  }
   set {
    this.autoScrollHorizontalMaximum = value;
    SetScrollRange(this.Handle, (int)SB_HORZ, autoScrollHorizontalMinimum, autoScrollHorizontalMaximum, true);
   }
  }
 
  public int AutoScrollVerticalMinimum
  {
   get { return this.autoScrollVerticalMinimum;  }
   set {
    this.autoScrollVerticalMinimum = value;
    SetScrollRange(this.Handle, (int)SB_VERT, autoScrollHorizontalMinimum, autoScrollHorizontalMaximum, true);
   }
  }
 
  public int AutoScrollVerticalMaximum
  {
   get { return this.autoScrollVerticalMaximum;  }
   set {
    this.autoScrollVerticalMaximum = value;
    SetScrollRange(this.Handle, (int)SB_VERT, autoScrollHorizontalMinimum, autoScrollHorizontalMaximum, true);
   }
  }
 
 
  public bool EnableAutoScrollHorizontal
  {
   get { return this.enableAutoHorizontal;  }
   set {
    this.enableAutoHorizontal = value;
    if (value)
     EnableScrollBar(this.Handle, SB_HORZ, ESB_ENABLE_BOTH);
    else
     EnableScrollBar(this.Handle, SB_HORZ, ESB_DISABLE_BOTH);
   }
  }
 
  public bool EnableAutoScrollVertical
  {
   get { return this.enableAutoVertical;  }
   set {
    this.enableAutoVertical = value;
    if (value)
     EnableScrollBar(this.Handle, SB_VERT, ESB_ENABLE_BOTH);
    else
     EnableScrollBar(this.Handle, SB_VERT, ESB_DISABLE_BOTH);
   }
  }
 
  public bool VisibleAutoScrollHorizontal
  {
   get { return this.visibleAutoHorizontal; }
   set
   {
    this.visibleAutoHorizontal = value;
    ShowScrollBar(this.Handle, (int)SB_HORZ, value);
   }
  }
 
  public bool VisibleAutoScrollVertical
  {
   get { return this.visibleAutoVertical; }
   set
   {
    this.visibleAutoVertical = value;
    ShowScrollBar(this.Handle, (int)SB_VERT, value);
   }
  }
 
  #endregion
 
  #region ScrollEventType to SB_* messages and SB_* messages to ScrollEventType
   
  private int getSBFromScrollEventType(ScrollEventType type)
  {
   int res = -1;
   switch (type)
   {
    case ScrollEventType.SmallDecrement:
     res = SB_LINEUP;
     break;
    case ScrollEventType.SmallIncrement:
     res = SB_LINEDOWN;
     break;
    case ScrollEventType.LargeDecrement:
     res = SB_PAGEUP;
     break;
    case ScrollEventType.LargeIncrement:
     res = SB_PAGEDOWN;
     break;
    case ScrollEventType.ThumbTrack:
     res = SB_THUMBTRACK;
     break;
    case ScrollEventType.First:
     res = SB_TOP;
     break;
    case ScrollEventType.Last:
     res = SB_BOTTOM;
     break;
    case ScrollEventType.ThumbPosition:
     res = SB_THUMBPOSITION;
     break;
    case ScrollEventType.EndScroll:
     res = SB_ENDSCROLL;
     break;
    default:
     break;
   }
   return res;
  }
 
  private ScrollEventType getScrollEventType(System.IntPtr wParam)
  {
   ScrollEventType res = 0;
   switch (LoWord((int)wParam))
   {
    case SB_LINEUP:
     res = ScrollEventType.SmallDecrement;
     break;
    case SB_LINEDOWN:
     res = ScrollEventType.SmallIncrement;
     break;
    case SB_PAGEUP:
     res = ScrollEventType.LargeDecrement;
     break;
    case SB_PAGEDOWN:
     res = ScrollEventType.LargeIncrement;
     break;
    case SB_THUMBTRACK:
     res = ScrollEventType.ThumbTrack;
     break;
    case SB_TOP:
     res = ScrollEventType.First;
     break;
    case SB_BOTTOM:
     res = ScrollEventType.Last;
     break;
    case SB_THUMBPOSITION:
     res = ScrollEventType.ThumbPosition;
     break;
    case SB_ENDSCROLL:
     res = ScrollEventType.EndScroll;
     break;
    default:
     res = ScrollEventType.EndScroll;
     break;
   }
   return res;
  }
 
  #endregion
 
  #region WndProd override
 
  protected override void WndProc(ref Message msg)
  {
   base.WndProc(ref msg);
   if (msg.HWnd != this.Handle)
    return;
   switch (msg.Msg)
   {
    case WM_MOUSEWHEEL:
     if (!this.VisibleAutoScrollVertical)
      return;
     try
     {
      int zDelta = HiWord((int)msg.WParam);
      int y = HiWord((int)msg.LParam);
      int x = LoWord((int)msg.LParam);
      System.Windows.Forms.MouseButtons butt;
      switch (LoWord((int)msg.WParam))
      {
       case MK_LBUTTON:
        butt = System.Windows.Forms.MouseButtons.Left;
        break;
       case MK_MBUTTON:
        butt = System.Windows.Forms.MouseButtons.Middle;
        break;
       case MK_RBUTTON:
        butt = System.Windows.Forms.MouseButtons.Right;
        break;
       case MK_XBUTTON1:
        butt = System.Windows.Forms.MouseButtons.XButton1;
        break;
       case MK_XBUTTON2:
        butt = System.Windows.Forms.MouseButtons.XButton2;
        break;
       default:
        butt = System.Windows.Forms.MouseButtons.None;
        break;
      }
      System.Windows.Forms.MouseEventArgs arg0 = new System.Windows.Forms.MouseEventArgs(butt, 1, x, y, zDelta);
      this.ScrollMouseWheel(this, arg0);
     }
     catch (Exception) { }
      
     break;
 
    case WM_VSCROLL:
      
     try
     {
      ScrollEventType type = getScrollEventType(msg.WParam);
      ScrollEventArgs arg = new ScrollEventArgs(type, GetScrollPos(this.Handle, (int)SB_VERT));
      this.ScrollVertical(this, arg);
     }
     catch (Exception) { }
      
     break;
 
    case WM_HSCROLL:
 
     try
     {
      ScrollEventType type = getScrollEventType(msg.WParam);
      ScrollEventArgs arg = new ScrollEventArgs(type, GetScrollPos(this.Handle, (int)SB_HORZ));
      this.ScrollHorizontal(this, arg);
     }
     catch (Exception) { }
      
     break;
 
    default:
     break;
   }
  }
 
  #endregion
 
  #region Perform Manuel scrolling
 
  public void performScrollHorizontal(ScrollEventType type)
  {
   int param = getSBFromScrollEventType(type);
   if (param == -1)
    return;
   SendMessage(this.Handle, (uint)WM_HSCROLL, (System.UIntPtr)param, (System.IntPtr)0);
  }
 
  public void performScrollVertical(ScrollEventType type)
  {
   int param = getSBFromScrollEventType(type);
   if (param == -1)
    return;
   SendMessage(this.Handle, (uint)WM_VSCROLL, (System.UIntPtr)param, (System.IntPtr)0);
  }
 
  #endregion
 
  #region Panel Got focus
 
  private void ScrollablePanel_Click(object sender, EventArgs e)
  {
   this.Focus();
  }
 
  #endregion
   
  #region API32 functions
 
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  static public extern int GetSystemMetrics(int code);
 
  [DllImport("user32.dll")]
  static public extern bool EnableScrollBar(System.IntPtr hWnd, uint wSBflags, uint wArrows);
 
  //[DllImport("user32.dll")]
  //static public extern bool GetScrollInfo(System.IntPtr hwnd, int fnBar, LPSCROLLINFO lpsi);
 
  [DllImport("user32.dll")]
  static public extern int SetScrollRange(System.IntPtr hWnd, int nBar, int nMinPos, int nMaxPos, bool bRedraw);
 
  [DllImport("user32.dll")]
  static public extern int SetScrollPos(System.IntPtr hWnd, int nBar, int nPos, bool bRedraw);
   
  [DllImport("user32.dll")]
  static public extern int GetScrollPos(System.IntPtr hWnd, int nBar);
   
  /*
  public struct LPSCROLLINFO
  {
   uint cbSize;
   uint fMask;
   int  nMin;
   int  nMax;
   uint nPage;
   int  nPos;
   int  nTrackPos;
  }
  */
 
  [DllImport("user32.dll")]
  static public extern bool ShowScrollBar(System.IntPtr hWnd, int wBar, bool bShow);
   
  [DllImport("user32.dll")]
  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
 
  [DllImport("user32.dll")]
  static extern int HIWORD(System.IntPtr wParam);
 
  static int MakeLong(int LoWord, int HiWord)
  {
   return (HiWord << 16) | (LoWord & 0xffff);
  }
  
  static IntPtr MakeLParam(int LoWord, int HiWord)
  {
   return (IntPtr) ((HiWord << 16) | (LoWord & 0xffff));
  }
                
  static int HiWord(int number)
  {
   if ((number & 0x80000000) == 0x80000000)
    return (number >> 16);
   else
    return (number >> 16) & 0xffff ;
  }
   
  static int LoWord(int number)
  {
   return number & 0xffff;
  }
 
  #endregion
 
 }
}

Paso 2: Modificar nuestro panel.

  • Modificamos el tipo de nuestro panel por el nuevo tipo
1
2
3
// Cambio su tipo
//private System.Windows.Forms.Panel panel1;
private CustomAutoScrollPanel.ScrollablePanel panel1;


  • Modificamos al InitializeComponent que donde se inicializa el objeto, ya que ahora es de otro tipo.
1
2
3
private void InitializeComponent()
{
       this.panel1 = new CustomAutoScrollPanel.ScrollablePanel();

  • Modificar el constructor de nuestro formulario.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public Form1()
{
 //
 // Necesario para admitir el Diseñador de Windows Forms
 //
 InitializeComponent();
 
 //
 // TODO: agregar código de constructor después de llamar a InitializeComponent
 //
 this.panel1.AutoScroll = true;
 this.panel1.EnableAutoScrollHorizontal = false;
 this.panel1.VisibleAutoScrollHorizontal = false;
 this.panel1.EnableAutoScrollVertical = true;
 this.panel1.VisibleAutoScrollVertical = true;
}
Ahora ya desde nuestro editor ya podremos configurar que Scroll queremos mostrar el horizontal o el vertical.


Fuente: URL

No hay comentarios:

Publicar un comentario