Assam

I am developing a custom control which would act as a PDF viewer. I am extending this control from the Panel control(Please note i have enabled double buffering of controls). I am not using the auto scroll property i.e. i am manually controlling the scrolling. The control works fine if there are no controls. But when there are controls in it i have to change the poistion of the controls in the paint event. Now the problem is that the whole re-painting occurs of those added controls. Is there a way to change the position of the child control on scrolling



Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

sirjis

You shouldn't be moving the controls in the Paint event. Override OnLayout instead and do it there.




Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

Assam

Thanks for the reply but it didnt resolve my issue. Now i am not changing the location of the controls on the paint even rather Onlayout i am calling the following method. But now the problem is there's alot of flickering of child controls. Please see the code below may be i am doing something wrong. Also please note that i have to dynamically judge the position of the child controls

private void RenderPDFFields()
{
int scrollValue = vScroll.Value == 0 10 : vScroll.Value;
//Getting the current PDF page
int activePage = GetPageNumberAtPoint(new Point(Width / 2, scrollValue));

if (activePage > -1)
{
//Calculating the location of the page
Rectangle firstRect = _currentRectPages[activePage];
Point firstPageLoc = new Point(5);
Point secondPageLoc = new Point(5);

if (firstRect.Width < Width)
firstPageLoc = new Point(Width / 2 - firstRect.Width / 2);

Point p = new Point(firstPageLoc.X - hScroll.Value,
firstRect.Top - vScroll.Value);
_pnlPages[activePage].Location = p;

_pnlPages[activePage].Visible = true;
_pnlPages[activePage].Invalidate();

//Hiding previous page
if (activePage > 0)
_pnlPages[activePage - 1].Visible = false;

//If there's enough virtual space left then show the next page as well
if (activePage + 1 < _defaultRectPages.Length & _pnlPages[activePage].Location.Y < 0)
{
Rectangle secondRect = _currentRectPages[activePage + 1];
if (secondRect.Width < Width)
secondPageLoc = new Point(Width / 2 - secondRect.Width / 2);

_pnlPages[activePage + 1].Location = new Point(secondPageLoc.X - hScroll.Value,
(firstRect.Top - vScroll.Value) + firstRect.Height + 5);

_pnlPages[activePage + 1].Visible = true;
_pnlPages[activePage].Invalidate();
}
}
}





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

sirjis

For a PDF renderer I would personally not have any child controls, but would just render the pages manually. The only child control I might use would be a temporary TextBox (or something similar) for when the user wants to edit a field value (if you're not just displaying the PDFs).





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

Assam

This is exactly what i am doing. I have overridden the OnPaint event to render the PDF page. I am only adding controls that relate to PDF Form Fields(i.e. Empty Signature, text boxes, radio buttons etc) page wise. For each page i have a panel and then i add those form fileds init as child control. So the method which i sent was only specifc to the form fileds. I sucessfully add them but when i try to move those controls on scrolling the contrlols flicker and they really do look ugly. I want those controls to move smothly just like when added to windows form and scrolled.





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

sirjis

What are you doing in terms of OnPaintBackground, OnPaint, and double buffering with your control





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

Assam

I have overridden the OnPaint event to render the PDF page. For double buffering i have enabled the Panel through the following statements:

SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.DoubleBuffer, true);

UpdateStyles();

I am not handling the OnPaintBackGround event.





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

Assam

Ok i have come with an idea i have tried it and it seems to work for me. But i am not sure of the idea i.e. whether i should be going for it or not so plz look and help me.
I have made a a class ControlRenderer just like the ComboBoxRenderer. This would render all the desired controls on the screen and that also on Paint event. Here is the sample code for it

internal sealed class ControlRendrer
{
public static void DrawTextBox(Graphics g,TextBoxAdapter textBoxAdapter)
{
VisualStyleElement visualStyleElement = VisualStyleElement.TextBox.TextEdit.ReadOnly;
VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(visualStyleElement);
DrawBackground(g,visualStyleRenderer,textBoxAdapter.Bounds);
TextRenderer.DrawText(g,textBoxAdapter.Text,textBoxAdapter.Font,textBoxAdapter.Bounds,textBoxAdapter.ForeColor);
}

private static void DrawBackground(Graphics g, VisualStyleRenderer visualStyleRenderer, Rectangle bounds)
{
visualStyleRenderer.DrawBackground(g, bounds);
if ((visualStyleRenderer.GetColor(ColorProperty.FillColor) != SystemColors.Window))
{
Rectangle backgroundContentRectangle = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
backgroundContentRectangle.Inflate(-2, -2);
g.FillRectangle(SystemBrushes.Window, backgroundContentRectangle);
}
}


}

interface IRenderable
{
void Render(Graphics g,Rectangle destRect);
}


class TextBoxAdapter : TextBox,IRenderable
{
private string _name;
private AcroFields _form;
private AcroFields.Item _item;
private TextField _txt_field;

public TextBoxAdapter()
{

}

public void Render(Graphics g, System.Drawing.Rectangle destRect)
{
System.Drawing.Rectangle boundRect = (System.Drawing.Rectangle)Tag;
Point p =
new Point(destRect.X + boundRect.X,
destRect.Top + boundRect.Y);
Location = p;

ControlRendrer.DrawTextBox(g, this);
}


public TextBoxAdapter(string name, TextField txtField, AcroFields form)
{
_name = name;
_form = form;
_item = form.GetFieldItem(name);
Name = name;
_txt_field = txtField;

SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.DoubleBuffer, true);

UpdateStyles();
}


#region IRenderable Members


#endregion
}





Re: Windows Forms General How to scroll controls in a custom control i.e. s there a way to change the position of the child control on scrolling?

sirjis

That seems like a good way to do it to me. Very well designed, too. The only trick will be disabling the regular painting of the child controls, but you can probably do that just by setting Visible to false.