CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    56

    Vis 2008 Drawing Controls?

    I'm wondering if anyone knows of some standard drawing controls in VS 2008. I'm trying to build a windows form which creates and shows some data in a form of branching diagrams. I wanted to place it in a control which I could allow to be manipulated similar to how Google Maps works.

    The idea is for the user to be able to scroll the view by holding left click and dragging to move the view. I'm stuck trying to find a simple control which allows me to draw in it and when the view is moved that objects drawn don't simply get drawn on the rest of the window.

    -Thanks, in Advance.

    Edit: I'm using Visual C# 2008 Express Edition
    Last edited by Salbrismind; March 1st, 2009 at 06:50 PM.

  2. #2
    Join Date
    Jan 2009
    Posts
    56

    Re: Vis 2008 Drawing Controls?

    Anyone know anything, at all??

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Vis 2008 Drawing Controls?

    You paint a control by overriding the OnPaint method and drawing to the graphics object supplied.

    Code:
    class DrawOnMe : Panel
    {
        protected override OnPain(PaintEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {
                rect = this.ClientRectangle;
                g.FillRectangle(Brushes.Pink, rect);
            }
        }
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    56

    Re: Vis 2008 Drawing Controls?

    So I can create my own control, and when it draws itself I can use that to draw things inside it which will not just flood out into the rest of the window?

    What happens if I tell it to draw things outside of its visible bounds?

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Vis 2008 Drawing Controls?

    You could always try it and see. You can draw wherever you want, but a control probably should not need to draw outside of its own area.

  6. #6
    Join Date
    Jan 2009
    Posts
    56

    Re: Vis 2008 Drawing Controls?

    I tried creating a new class called DrawPanel:
    Code:
    class DrawPanel : Panel

    And this is what it looks like:
    Code:
    class DrawPanel : Panel
        {
            protected override void OnPaint(PaintEventArgs e)
            {
                using (Graphics g = e.Graphics)
                {
                    Rectangle rect = this.ClientRectangle;
                    g.FillRectangle(Brushes.Pink, rect);
                }
            }
        }
    But it draws nothing to the screen.

    I have this in my window:

    Code:
    private DrawPanel drawPanel;
    
    public MainWindow()
            {
                InitializeComponent();
    
                //Drawing Control
                drawPanel = new DrawPanel();
                drawPanel.Location = new Point(50, 50);
                drawPanel.Size = new Size(200, 500);

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Vis 2008 Drawing Controls?

    You need to add the panel to the Controls collection of the form (I assume that "MainWindow" derives from Form):

    Code:
    private DrawPanel drawPanel;
    
    public MainWindow()
            {
                InitializeComponent();
    
                //Drawing Control
                drawPanel = new DrawPanel();
                drawPanel.Location = new Point(50, 50);
                drawPanel.Size = new Size(200, 500);
                this.Controls.Add(drawPanel);

  8. #8
    Join Date
    Jan 2009
    Posts
    56

    Re: Vis 2008 Drawing Controls?

    Thanks, It works perfectly!

    Thank you very much for all the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured