WPF: Rounded rects on only certain corners?
-
Hey all. I posted this here because I don't see a specific WPF forum. I'm curious if anyone else has wanted to or figured out a way to draw rectangles with only certain of the four corners rounded (for e.g. the bottom corners only). I bet I could sit down and work out a way to do this, but I'm new to WPF so wanted to see if it's been done before. Thanks, Logan
{o,o}.oO( Did somebody say MouseDown? ) |)””’) -”-”-
-
Hey all. I posted this here because I don't see a specific WPF forum. I'm curious if anyone else has wanted to or figured out a way to draw rectangles with only certain of the four corners rounded (for e.g. the bottom corners only). I bet I could sit down and work out a way to do this, but I'm new to WPF so wanted to see if it's been done before. Thanks, Logan
{o,o}.oO( Did somebody say MouseDown? ) |)””’) -”-”-
You can do it, but you have to get a bit tricky. The window border itself can’t be changed, but you can make a borderless window with a shape inside of it that does what you want. You then have to enable dragging the ‘window’ based on dragging its contents, but it’s not a big deal. Start by setting the following styles on the window: WindowStyle="None" Background="{x:Null}" AllowsTransparency="True" ResizeMode="NoResize" Inside this window you’ll need to place a Border object. Inside the Border you can place whatever you want. Now on the Border set its CornerRadius to something like CornerRadius="0,22,0,22" and set your fill and stroke as desired. Finally, you’ll need to be able to reposition the window regardless of where the user clicks. Fortunately WPF lets you capture events both on the way down (Preview) and on the way back up through the element stack (Element). So to complete your project, in your window class add the following code: protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); DragMove(); } And that’s it. You can use this trick for creating windows of literally any shape. Even animation should be supported, though I haven’t tried it. Disclaimer: I’ve only tried this on Vista. I think it SHOULD work on XP, but the Desktop Window Manager in Vista may make a difference.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team Blog -
You can do it, but you have to get a bit tricky. The window border itself can’t be changed, but you can make a borderless window with a shape inside of it that does what you want. You then have to enable dragging the ‘window’ based on dragging its contents, but it’s not a big deal. Start by setting the following styles on the window: WindowStyle="None" Background="{x:Null}" AllowsTransparency="True" ResizeMode="NoResize" Inside this window you’ll need to place a Border object. Inside the Border you can place whatever you want. Now on the Border set its CornerRadius to something like CornerRadius="0,22,0,22" and set your fill and stroke as desired. Finally, you’ll need to be able to reposition the window regardless of where the user clicks. Fortunately WPF lets you capture events both on the way down (Preview) and on the way back up through the element stack (Element). So to complete your project, in your window class add the following code: protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); DragMove(); } And that’s it. You can use this trick for creating windows of literally any shape. Even animation should be supported, though I haven’t tried it. Disclaimer: I’ve only tried this on Vista. I think it SHOULD work on XP, but the Desktop Window Manager in Vista may make a difference.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team BlogHa ha ha. I just realized you only needed the rounded rectangle and I took you all the way to the rounded window. Oh well, all you needed was the CornerRadius bit. And I think you even wrote an article already? Well, good job!
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team Blog -
Ha ha ha. I just realized you only needed the rounded rectangle and I took you all the way to the rounded window. Oh well, all you needed was the CornerRadius bit. And I think you even wrote an article already? Well, good job!
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team BlogHaha, yes I wrote an article for a Shape-derived class called PartiallyRoundedRectangle before I found out about Border's ability to do essentially the same thing. I found out about that through the article though, so it wasn't a complete waste of time. ;) Thanks for the in-depth instructions for making rounded borderless windows even if it's not exactly what I was asking for. ;) Logan
{o,o}.oO( Did somebody say MouseDown? ) |)””’) -”-”-