oooo

oooo

  • NA
  • 1
  • 0

Drawing text on a panel that scrolls

Jan 12 2009 4:37 AM
I am drawing various rectangles with texts inside them.  Right now the method draws to the main form(and it doesn't scroll even with setting autoscroll to true).  What I would like to do is to draw on a panel and make this panel scrollable.  Here is the drawing routine that someone else helped me with.  I would like to use this routine to draw the text and rectangle because it does EXACTLY what i need.  What I need is how to draw this text on a panel and make that panel scroll if appropriate.  Thank you in advance.

using System;   
using System.Linq;   
using System.Collections.Generic;   
using System.ComponentModel;   
using System.Data;   
using System.Drawing;   
using System.Text;   
using System.Windows.Forms;   
using System.Runtime.InteropServices;   
  
namespace Drawing   
{   
    public partial class Form1 : Form   
    {   
        [DllImport("coredll.dll")]   
        static extern int DrawText(IntPtr hdc, string lpStr, int nCount, ref Rect lpRect, int wFormat);   
  
        private const int DT_CALCRECT = 0x00000400;   
        private const int DT_WORDBREAK = 0x00000010;   
        private const int DT_EDITCONTROL = 0x00002000;   
  
        private List<string> m_strings;   
        private const int ITEM_PADDING = 5;   
        private int m_maxItemWidth;   
        private int  currentYPosition;   
           
        [StructLayout(LayoutKind.Sequential)]   
        private struct Rect    
        {    
            public int Left;   
            public int Top;   
            public int Right;   
            public int Bottom;    
            public Rect(Rectangle r)    
            {    
                this.Left = r.Left;    
                this.Top = r.Top;    
                this.Bottom = r.Bottom;    
                this.Right = r.Right;    
            }    
        }   
  
        public Form1()   
        {   
            m_strings = new List<string>();   
            m_strings.AddRange(new[] {"This is an example of one long string that wraps itself  when it reaches the end of the box.",   
                                       "The 2nd string.",    
                                       "The third string.  Notice the size of each box is different"});   
            InitializeComponent();   
        }   
  
  
        private void Form1_Load(object sender, EventArgs e)   
        {   
            m_maxItemWidth = (Size.Width / 2);   
            currentYPosition = 0;   
        }   
  
        protected override void OnPaint(PaintEventArgs e)   
        {   
            currentYPosition = ITEM_PADDING;   
            IntPtr dc = e.Graphics.GetHdc();   
            int currentRectInflate = ITEM_PADDING - 1;   
               
            try  
            {   
                using (Pen blackPen = new Pen(Color.Black))   
                {   
                    using (Brush blackBrush = new SolidBrush(Color.Black))   
                    {   
                        foreach (string item in m_strings)   
                        {   
                            Rectangle rec = new Rectangle(ITEM_PADDING, currentYPosition, (m_maxItemWidth - ITEM_PADDING), Height);   
                            Rect drawTextRC = new Rect(rec);   
                           
                            int flags = DT_CALCRECT | DT_WORDBREAK;   
                               
                            int height = DrawText(dc, item, item.Length, ref drawTextRC, flags);                               
                            rec.Height  = height;   
                            e.Graphics.DrawString(item, Font, blackBrush, rec);   
  
  
  
                            rec.Inflate(currentRectInflate, currentRectInflate);   
                            e.Graphics.DrawRectangle(blackPen, rec);   
                            currentYPosition += (height + (2* ITEM_PADDING));   
                        }   
  
                    }   
                }   
            }   
            finally  
            {   
                e.Graphics.ReleaseHdc(dc);   
            }   
  
        }   
    }   
}