mardi 3 mars 2015

scrolling issues while dealing with graphics in c# .net

I have a panel and a richTextBox placed at the bottom corner of the panel. Panel takes text from richTextBox and Draws using DrawString() on the panel using its paint event.


When the panel is filled it does not auto-scroll the panel even if autoscroll=true.


How to enable scrollings to a paint event handling panel?


How to reflect the scrolling of richTexBox in a panel(if richtextbox is scrolled to some extent, the panel should also be scrolled to the same extent?) In short the visible content of richtextbox must be visible in the panel exactly.



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
}
}


}


Aucun commentaire:

Enregistrer un commentaire