Ask a questionAsk a question
 

AnswerRaised border on UserControl

  • Thursday, October 29, 2009 12:36 PMmarkusb51 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi

    I'm trying to create a subclass of UserControl that has a raised border. The builtin BorderStyle property only seems to have sunken or flat borderstyles, which I don't want. I've tried overriding the OnPaint method and using ControlPaint::DrawBorder3D but this seems to have no effect. I don't know why, as the OnPaint is definitely being called (I put in a breakpoint to check this). If anyone knows how to do this I would be very grateful. The code is here:

    using namespace System;
    using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::ComponentModel;
    
    namespace MidiCompozerControls
    {
    	public ref class MidiCompozerControl: public System::Windows::Forms::UserControl
    	{
    	private:
    		System::Windows::Forms::TableLayoutPanel^ mainPanel;
    		System::Windows::Forms::Label^ title;
    		// Initialize the control elements.
    		void InitializeComponent();
    
    	protected:
    		// Overridden paint event handler
    		virtual void OnPaint(System::Windows::Forms::PaintEventArgs^) override;
    
    	public:
    		// Define the constructor.
    		MidiCompozerControl()
    		{
    			InitializeComponent();
    		}
    	};
    }
    
    #include "stdafx.h"
    #include "MidiCompozerControl.h"
    
    using namespace MidiCompozerControls;
    
    // Initialize the control elements.
    void MidiCompozerControl::InitializeComponent()
    {
    	this->SetStyle(static_cast<ControlStyles>(ControlStyles::DoubleBuffer | ControlStyles::UserPaint), true);
    	this->UpdateStyles();
    
    	Size = System::Drawing::Size(100, 150);
    	// The border style is wrong! poo
    	//this->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
    
    	// Table layout panel for all the child controls
    	mainPanel = gcnew System::Windows::Forms::TableLayoutPanel;
    	mainPanel->Location = Point(0, 0);
    	mainPanel->Size = this->Size;
    	mainPanel->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top | AnchorStyles::Bottom | AnchorStyles::Left  | AnchorStyles::Right);	
    
    	// Title label
    	title = gcnew System::Windows::Forms::Label;
    	title->Location = System::Drawing::Point(0, 0);
    	title->Size = System::Drawing::Size(100, 20);
    	title->Text = "Midi In";
    
    	this->mainPanel->Controls->Add(title);
    	this->Controls->Add(mainPanel);
    }
    
    
    void MidiCompozerControl::OnPaint(System::Windows::Forms::PaintEventArgs^ e)
    {
    	Rectangle borderRectangle = this->ClientRectangle;
    
    	// Call base onpaint
    	UserControl::OnPaint(e);
    	// Draw 3d raised border, not working
    	ControlPaint::DrawBorder3D(e->Graphics, borderRectangle, Border3DStyle::Raised);
    }
    


    Mary thanks

    Mark

Answers

  • Wednesday, November 04, 2009 4:02 PMmarkusb51 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Wesley,

    Sorry about messing up the 'mark as answer' bit, I was trying to mark my own reply as the answer and somehow made a mess of it, losing my reply in the process. Anyway, I found the problem. My UserControl contained a PanelControl which was anchored to the top, bottom, left and right, and this had obscured the raised border of the parent control. When I explicitly positioned the panel with a smaller size the parent control border came into view, perfectly raised :-).

    If this thread would be better placed in the WinForms thread please put it there as it might help someone else. If you know any way to anchor a child control without obscuring the parent control borders that would be great, but I will scour the documentation.

    Thanks again for your help,

    All the best

    Mark
    • Marked As Answer bymarkusb51 Wednesday, November 04, 2009 4:02 PM
    •  

All Replies

  • Tuesday, November 03, 2009 6:45 AMWesley YaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Mark,

    What does the "borderRectangle" refer to? I tried e->ClipRectangle and it works.
    And I think this question is not related to VC++ Express, you could post WinForm question at Windows Forms General forum.

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, November 03, 2009 3:11 PMmarkusb51 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi Wesley,

    Thanks for your reply, and my apologies if I put this question in the wrong place. I am happy to move it, how might I do that?

    I tried your suggestion and used the cliprectangle but with the same results. No border is drawn. Here is my modified OnPaint.

    void MidiCompozerControl::OnPaint(System::Windows::Forms::PaintEventArgs^ e)
    {
    	System::Drawing::Rectangle borderRectangle = e->ClipRectangle;
    
    	// Call base onpaint
    	UserControl::OnPaint(e);
    	// Draw 3d raised border, not working
    	ControlPaint::DrawBorder3D(e->Graphics, borderRectangle, Border3DStyle::Raised);
    }
    
    In the original code the borderRectangle was obtained from the Control->ClientRectangle property. The SetStyle property is set as below.
    this
    ->SetStyle(static_cast
    <ControlStyles>(ControlStyles::DoubleBuffer | ControlStyles::UserPaint), true
    );
    I checked again that OnPaint was being called, and it is. But there is no change to the border at all. Further information that might help: I'm creating these controls at runtime, they are not in the design time toolbox, although I wouldn't have thought that would make a difference, they are subclassed from UserControl and they work fine apart from this one thing.

    Thanks again for your help.

    Cheers

    Mark
  • Wednesday, November 04, 2009 3:45 AMWesley YaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Mark,

    I'm not a WinForm expert but your code looks good to me, I'm not sure what would be the problem in your project, I uploaded my test project for you at here:
    http://www.mediafire.com/file/o4ovxkylwyw/CPPWinForm.zip
    Hope it can help you, if you still cannot figure it out, I can move it to the WinForm forum. :)

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer bymarkusb51 Wednesday, November 04, 2009 3:53 PM
    • Unmarked As Answer bymarkusb51 Wednesday, November 04, 2009 3:53 PM
    •  
  • Wednesday, November 04, 2009 4:02 PMmarkusb51 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Wesley,

    Sorry about messing up the 'mark as answer' bit, I was trying to mark my own reply as the answer and somehow made a mess of it, losing my reply in the process. Anyway, I found the problem. My UserControl contained a PanelControl which was anchored to the top, bottom, left and right, and this had obscured the raised border of the parent control. When I explicitly positioned the panel with a smaller size the parent control border came into view, perfectly raised :-).

    If this thread would be better placed in the WinForms thread please put it there as it might help someone else. If you know any way to anchor a child control without obscuring the parent control borders that would be great, but I will scour the documentation.

    Thanks again for your help,

    All the best

    Mark
    • Marked As Answer bymarkusb51 Wednesday, November 04, 2009 4:02 PM
    •