在c#.net中处理图形时滚动问题

时间:2015-03-03 17:39:38

标签: c# .net winforms

我在panel的底角放置了richTextBoxpanel。面板使用richTextBox从面板上DrawString()获取文字并使用面板上的paint event进行绘制。

当面板填满时,即使autoscroll=true,也不会自动滚动面板。

如何启用滚动到绘图事件处理面板?

如何在面板中反映richTexBox的滚动(如果在某种程度上滚动了richtextbox,面板也应该滚动到相同的范围?) 简而言之,richtextbox的可见内容必须在面板中完全可见。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace TextFormatter
{
public partial class Form1 : Form
{
    //int scrol;
    public Form1()
    {
        InitializeComponent();
        //Set Double Buffering
        panel1.GetType().GetMethod("SetStyle",     
                System.Reflection.BindingFlags.Instance | 
                System.Reflection.BindingFlags.NonPublic)
                .Invoke(panel1, new object[] {  
                     System.Windows.Forms.ControlStyles.UserPaint | 
                     System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | 
                     System.Windows.Forms.ControlStyles.DoubleBuffer, true });

    }
    class myFont
    {
        public int size;
        public string family;
        public Color c;
    }
    myFont f = new myFont();
    private void Form1_Load(object sender, EventArgs e)
    {

    }

   private void UpdateStatus()
    {
        panel1.Refresh();
    }
    delegate void UpdateStatusInvoker();

    private void panel1_Paint_1(object sender, PaintEventArgs e)
    {
        Point P = new Point();
        int w = this.ClientSize.Width;
        int h = this.ClientSize.Height;
        P.X = w - 180;
        P.Y = h - 52;
        richTextBox1.Location = (P);
        Graphics g = e.Graphics;
        Brush b;
        b = new SolidBrush(f.c);
        if (f.c.IsEmpty)
        {
            b = new SolidBrush(Color.Brown);
        }
        Point p = new Point();
        p.X = 100;
        p.Y = 100;
        Font ff = new Font(f.family, f.size | 20);
        g.DrawString("" + richTextBox1.Text, ff, b, p);
        // richTextBox1.AppendText(
        b.Dispose();
      //  g.Dispose();
    }

 private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
       // Invalidate();
        this.Invoke(new UpdateStatusInvoker(UpdateStatus));
    }

    private void richTextBox1_VScroll(object sender, EventArgs e)
    {
       // same scrolling must be applied on panel.
    }

    private void panel1_Scroll(object sender, ScrollEventArgs e)
    {
       // control the scrolling of graphical content of the panel
    }
}

}

0 个答案:

没有答案