Prevent Button from inheriting BackColor of Parent - c#

When I have a parent control which has a BackColor other than SystemColors.Control, but I have buttons on that parent control that I want to be drawn in the system them. However, when I do not change the BackColor of the buttons, it's drawn in the color of the parent. When I change the BackColor of the button to SystemColors.Control, it isn't drawn in the Windows theme anymore.
The left version is with SystemColors.Control and the right is without changing the BackColor.
Blown up, it looks like this. Here you can see that the buttons have a solid background.
Any suggestions how I can fix this?
The effect in the image can be accomplished by creating a new .NET 2.0 WinForms project and changing the constructor of Form1 to the following:
public Form1()
{
InitializeComponent();
var textBox = new TextBox();
Controls.Add(textBox);
var button = new Button { Text = "L", Width = 23, Height = 18, Left = -1, Top = -1 };
textBox.Controls.Add(button);
// Disable the line below to get the default behavior
button.BackColor = SystemColors.Control;
}

I unforuantely only have access to Windows 7 at the moment so I can't test on XP etc. but are you calling Application.EnableVisualStyles in the Main entry point of your WinForms application?
On Windows 7 with Aero enabled the buttons do not assume the parent color if Application.EnableVisualStyles is called.
NB: You should set button.UseVisualStyleBackColor = true; and do not explicitly set the BackColor property of the button.

Related

How to make child Form transparent?

Am creating a windows application (win form). I want to create a UI something like the attached image.
Am using a form (called CalendarForm),that will contains the right side controls (blue background portion)
The issue now am facing is, when am using MDI and setting CalendarForm as a child. Then I am not able to sent the opacity to show the MDI parent form background.
Am using below mentioned code in MDIForm_Load method
this.WindowState = FormWindowState.Maximized;
CalendarForm calForm = new CalendarForm();
calForm.BackColor = Color.FromArgb(33, 66, 131);
calForm.Opacity = 0.5d;
calForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
calForm.MdiParent = this;
calForm.Show();
Is there any way to achieve my requirement ?
Using this Custom Month Calendar created by Patrik Bohman ,we can make the calendar background transparent.
To make a WinForm transparent you could use the opacity property
calForm.Opacity = 0.5;

Animate a label

I am trying to animate a label in a WPF application. The label gets created programatically (and dynamically) so it is not defined in XAML, but is created in the C# code.
Animation Story
The label appears in the bottom of the window. The label should be positioned lower than the window, so the user can not see it initially. Then a the label moves up (like sliding) and fades out before it reaches the top of the window.
What I have done
I have implemented this behavior myself in an other project. This time I want to use WPF which should perform better.
So far I have seen there should be multiple ways of doing this. Starting with a DoubleAnimation, going by a PathAnimation and VectorAnimation (the last of which I have not tested successfully).
Encountered problems
The animation works nice with a DoubleAnimation, but there is a problem: When I resize the window, the label gets resized too (similar to an anchor in Winforms). When I make the window smaller the label gets smaller too, until it disappears completely. This effect occurs only in the height of the label. I added the code snippet adding the label. Maybe you find some error. Also there should be a better way to implement this (I personally find it very ugly).
Label lbl = new Label()
{
Content = "Test",
FontSize = 36,
Foreground = new SolidColorBrush(Colors.Red),
Background = new SolidColorBrush(Colors.Black),
HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Top
};
lbl.Margin = new Thickness(0, this.MainGrid.ActualHeight + lbl.ActualHeight, 0, 0);
this.MainGrid.Children.Add(lbl);
UpdateLayout();
Transform myTransform = new TranslateTransform();
lbl.RenderTransform = myTransform;
DoubleAnimation AnimationY = new DoubleAnimation((this.MainGrid.ActualHeight + 20) * -1, TimeSpan.FromSeconds(4));
myTransform.BeginAnimation(TranslateTransform.YProperty, AnimationY);
Questions
As I said, I have found multiple ways that seem to achieve the same behavior. Which one could I use to do this. I still have to do the fade-out on the top of the window, but this animation is easier to do compared to the movement.
The animation is fine. The reason your label is resizing is because you are adding it to a Grid. If you want your label to have a fixed height, then set its Height or MinHeight property.

ListViewItem loses background color when repainted and the cursor is over the item

I have a listview control obviously with numerous listviewitems, whenever the control is repainted the item which the mouse is "over" at the time loses it's background colour (ie. it's white).
Once the item is painted again and the cursor is not over it the correct background colour is restored.
Does anyone know why this happens and how it can be stopped?
Any help would be appreciated
To expand on this and hopefully give a little more context....
It's happening on Windows 7, .NET 3.5 (although it was also happening when the project was targetted at 4.0 too). The theme on my dev computer is Viao Theme 5, however it also happens on another of my machines which has the default Windows 7 theme.
The listview is instantiated like this...
this.PlaylistTrackList.Cursor = System.Windows.Forms.Cursors.Hand;
this.PlaylistTrackList.Dock = System.Windows.Forms.DockStyle.Fill;
this.PlaylistTrackList.FullRowSelect = true;
this.PlaylistTrackList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.PlaylistTrackList.LabelWrap = false;
this.PlaylistTrackList.Location = new System.Drawing.Point(4, 4);
this.PlaylistTrackList.Margin = new System.Windows.Forms.Padding(4);
this.PlaylistTrackList.MultiSelect = false;
this.PlaylistTrackList.UseCompatibleStateImageBehavior = false;
this.PlaylistTrackList.View = System.Windows.Forms.View.Details;
Let's say there are 5 list view items, A,B,C,D, and E. If the background color property of items BC and D is red, nothing is selected and nothing is going to be selected. When the list view control is redrawn for whatever reason and the cursor happens to be over any of items BC or D then the item is drawn with a white background color not red as it should be. However once the mouse is not over the item again and its redrawn is returns to red.
How can I stop it always drawing the item that the cursor is over with a white background regardless of the "real" background color property?
Initialize the ListView control as follows:
listView1.BackColor = Color.Black;
listView1.ForeColor = Color.White;
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullRowSelect = true;
For more details check the following link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw.aspx

C# Windows Form Application Transparent button

I'm new to C#. I'd like to create an invisible button, but they are click-able in C# windows form application. Is there a way? I tried BackColor to Transparent, but that does not change the fact that it is transparent
Its simple try this.
Click the button that you want to make transparent.
Select FlatStyle from Properties and set it to popup
Now change the BackColor property to Transparent.
This will make the button transparent.
However if you want to make it transparent over a PictureBox this method wont work..
It works only on normal backgrounds and background images.
Hope it works....
buttonLink.FlatStyle = FlatStyle.Flat;
buttonLink.BackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseDownBackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseOverBackColor = Color.Transparent;
The answers given just make the background color of the control you want to make transparent the same as the background color of its parent. It's not true transparency and windows forms doesn't support true transparency.
Windows Forms controls do not support true transparency. The background of a
transparent Windows Forms control is painted by its parent.
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-give-your-control-a-transparent-background?view=netframeworkdesktop-4.8&redirectedfrom=MSDN
Reference:
Original article and code can be found at:
Displaying a ToolTip when the Mouse Hovers Over a Disabled Control
# CodeProject by tetsushmz
Code:
public class TransparentSheet : ContainerControl
{
public TransparentSheet()
{
// Disable painting the background.
this.SetStyle(ControlStyles.Opaque, true);
this.UpdateStyles();
// Make sure to set the AutoScaleMode property to None
// so that the location and size property don't automatically change
// when placed in a form that has different font than this.
this.AutoScaleMode = AutoScaleMode.None;
// Tab stop on a transparent sheet makes no sense.
this.TabStop = false;
}
private const short WS_EX_TRANSPARENT = 0x20;
protected override CreateParams CreateParams
{
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
get
{
CreateParams l_cp;
l_cp = base.CreateParams;
l_cp.ExStyle = (l_cp.ExStyle | WS_EX_TRANSPARENT);
return l_cp;
}
}
}
Explanation:
What you need to do is use the given control as an overlay on your disabled TextBox (that you mentioned in one of your comments). Sibscribe to the overlay control's Click event and you have yourself a click on a disabled control.
I strongly recommend against this approach and feel it is kind of a hack. You really should look for an alternative approach instead of having to use a disabled control with an overlay control on top of it.
Maybe a different UI or atleast wrap it up in a UserControl to isolate this messy logic.
Setting the background property of the button to transparent will still leave a border. If you want a completely transparent button, do one of 2 things:
Create a transparent panel and assign a method to the Click event
or preferably
Create a custom UserControl that is filled with only BackColor (set to transparent) and assign method to Click event.
public class Invisible_Button : UserControl
{
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
this.Cursor = Cursors.Hand;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
}
}
Did you try button.Visible = false?
If all you want is to hide it, this will do the job.

WPF Border Property not working correctly

I am working on an application that is using Drag and Drop functionality. When I drag certain items over other items, I would like to show if they are towards the top, or the bottom of the item by setting the border property of the item they are over dynamically(as oppose to in the xaml).
I have looked into this, and used examples, but none of them are working. Here is the code I have that is called when a drag operation is performed over the Grid Item.
private void Grid_DragEnter(object sender, DragEventArgs e)
{
Grid grid = (Grid)sender;
grid.Background = Brushes.Cornsilk;
Border border = new Border();
border.BorderBrush = Brushes.Black;
border.BorderThickness = new Thickness(5, 10, 15, 20);
border.Background = Brushes.Black;
border.Padding = new Thickness(10);
border.Child = grid;
}
The code above is only test code so that I can see the border is showing. Once I can get this, then I will set the top or bottom border at separate times, depending on if they are above or below the center of the grid item.
You're setting the border's child, but the border itself needs to be added to some container in order for it to be visible.
That being said, you might want to look into Adorners. They are designed for exactly this scenario, and don't require changing the visual hierarchy of your UI at runtime.

Categories