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

    Painting and Invalidating

    I am trying to drag and resize controls during runtime with the same look and feel that VS does it. I paint the Handles and Selection Frames when a control gets the focus. However I need to remove them when the control loses focus. The Control_LostFocus function does not remove them. However, if I set a break point in the Control_Lostfocus function and them step out, the handles and Frame are removed. Why does break point do this? How can I resolve this issue?

    ControlsHash is a Hash table containing the controls.

    Thanks

    Matt


    Code:
    private void Control_GotFocus(Object sender, EventArgs e) {
    	// Retrieve related info
    	Control c = (Control) sender;	
    	ControlInfo info = (ControlInfo) ControlsHash[c];			
    	if(info.Enabled) {				
    		//Resizable rectangle
    		Rectangle rectOutside = new Rectangle(c.Location.X-6, c.Location.Y-6, c.Width+12, c.Height+12);
    		Rectangle rectInside = new Rectangle(c.Location.X, c.Location.Y, c.Width, c.Height);
    		ControlPaint.DrawSelectionFrame(GraphicPainter, true, rectOutside, rectInside, Color.Transparent);
    
    		Rectangle rect = new Rectangle(c.Location.X-8, c.Location.Y-8, 8, 8);
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    		rect.X = (c.Width-8)/2+ c.Location.X;
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    		rect.X = c.Width + c.Location.X;
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    
    		rect.X = c.Location.X-8;
    		rect.Y =  c.Location.Y + c.Height;
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    		rect.X = (c.Width-8)/2+ c.Location.X;
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    		rect.X = c.Width + c.Location.X;
    		ControlPaint.DrawGrabHandle( GraphicPainter, rect, true, true);
    	}
    }
    
    private void Control_LostFocus(Object sender, EventArgs e) {
    	Control c = (Control) sender;
    	Rectangle rectOutside = new Rectangle(c.Location.X-8, c.Location.Y-8, c.Width+16, c.Height+16);
    	c.Invalidate(rectOutside);
    	c.Update();
    }

  2. #2
    Join Date
    Oct 2004
    Location
    South Africa
    Posts
    86

    Re: Painting and Invalidating

    Hi
    I think I was having the same problem, when you say the change happens when you break into the code. Its not actually that, it when you lose focus. Instead of breaking into the code, just do an alt - tab and this should give you the same results.
    If it does then this means that the control needs updating, you should call its invalidate method and that should fix the problem.
    I havent looked at your code just your message.
    Good luck

  3. #3
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Painting and Invalidating

    Hi mrs,

    I'm not surprised you get different behaviour during debugging. Even in a debug session events still get fired to your controls, eg. Invalidate(), which may not be going just to the control you are debugging.

    I see in your GotFocus handler you are drawing to a GraphicPainter object. Have you tried Invalidate() on the control that contains the Graphics object for this. I'm not sure if you can Invalidate an area bigger than the control itself.
    Useful? Then click on (Rate This Post) at the top of this post.

  4. #4
    Join Date
    Dec 2003
    Posts
    87

    Re: Painting and Invalidating

    Thanks for your reply...

    The GraphicsPainter object is the form's graphics. I am painting the grab handles and the SelectionRectangle on the form the button sits on.

    You did give me an idea. You said:

    Have you tried Invalidate() on the control that contains the Graphics object for this.
    That's what my LostFocus function is doing now...invalidating the control but with the form's graphic object. However, it works if I invalidate the form:

    Code:
    private void Control_LostFocus(Object sender, EventArgs e) {
    	Control c = (Control) sender;
    	c.Parent.Invalidate();
    }
    I now need to figure out how to make the code more robust and reusable.

    I am trying to make a provider to give this drag/resize functionality. When it's done I'll send it to you.

    Thank you for suggestion/helping,

    Matt

  5. #5
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Painting and Invalidating

    Invalidate() is relative to the Control that invokes it. ie. For control.Invalidate() this is equivalent to control.Invalidate(new Rectangle(0, 0, control.Width, control.Height)). To invalidate the same area via the parent you would call control.Parent.Invalidate(control.Bounds).

    With your previous code you were invalidating an area that was in the wrong coordinates. Whether control.Invalidate(new Rectangle(-8, -8, c.Width+16, c.Height+16) would work I have no idea.
    Useful? Then click on (Rate This Post) at the top of this post.

  6. #6
    Join Date
    Dec 2003
    Posts
    87

    Re: Painting and Invalidating

    FYI:

    I just tried:

    control.Invalidate(new Rectangle(-8, -8, c.Width+16, c.Height+16)

    and it didn't work.

    Once again,

    Thanks,

    Matt

  7. #7
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Painting and Invalidating

    No problem, I accept all major credit cards...but failing that you could always click on the Rate This Post link.
    Useful? Then click on (Rate This Post) at the top of this post.

  8. #8
    Join Date
    Dec 2004
    Posts
    1

    Re: Painting and Invalidating

    Try using the OnEnter and OnLeave methods instead.

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