- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Compact Framework (3.5) 控件,我希望它具有 SystemColors.Window
的默认颜色,外观类似于编辑控件。我遇到的问题是 Visual Studio (2008) 始终使用 Control
的默认 BackColor
在设计界面上呈现该控件。我假设它是从基本 Control
、父级或 Site
的默认背景色中提取它。
任何人都知道如何指定或以其他方式通知 Visual Studio 默认的 BackColor 应该是什么?
public class MoneyInput : Control
{
public MoneyInput()
{
base.BackColor = SystemColors.Window;
base.ForeColor = SystemColors.WindowText;
}
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
Repaint();
}
}
}
这里是我的 DesignTimeAttributes.xmta 文件的相应部分。请注意,正如您在下面看到的那样,我真的不想将默认颜色设置为白色,我只是想让任何工作:
<DesktopCompatible>true</DesktopCompatible>
<Property Name="BackColor">
<Browsable>true</Browsable>
<DesignerSerializationVisibility>
DesignerSerializationVisibility.Visible
</DesignerSerializationVisibility>
<DefaultValue>
<Type>System.Drawing.Color</Type>
<Value>System.Drawing.Color.White</Value>
</DefaultValue>
<Category>Appearance</Category>
<Description>The background color of the control</Description>
</Property>
只是为了增加一些进展(或缺乏进展),我向控件添加了一个日志,这样当我将控件放在设计图面上时我可以看到发生了什么。构造函数中对 BackColor 的写入确实生效,并将颜色从 Control 更改为 Window。但是随后设计器基础结构中的某些东西将颜色(通过属性)设置回 Control。这发生在 ctor 之后但在 OnResize 和 OnHandleCreated 之前。现在,如果控件使用正确的值将 BackColor 属性序列化到设计器中,那么它将被安全地设置回去,但设计器不包含默认值。
最后一次编辑,我认为指定枚举(相对于基类型)的默认值语法不正确。我正在使用我在下面发布的解决方案,但它是一个 hack,希望下一个出现的人能够真正解决它。
最佳答案
我仍然希望得到真正的答案,但我会在这里写下我的解决方案:
public class MoneyInput : Control
{
// workaround for colors
private bool _constructed = false;
private int _handlesCreated;
public MoneyInput()
{
this.BackColor = SystemColors.Window;
this.ForeColor = SystemColors.WindowText;
_constructed = true;
}
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
// The logic below is based on:
//
// 1. If not _constructed then we are getting called
// from our ctor and we want the set to happen.
// 2. If we do not have a Handle yet then we are
// getting called by the infrastructure that
// creates a control.
// 3. At design-time, the VS designer applies the
// Ambient color even if we explicitly set
// our own color. It is supposed to only apply
// the Ambient if we have not explictly set
// a color.
// 4. At runtime we must accept any color changes
// that are passed to us.
// see if VS designer is screwing with us
bool ignoreColorSet =
_constructed
&& _handlesCreated == 0
&& RunningInDesigner;
if (!ignoreColorSet)
{
base.BackColor = value;
Repaint();
}
}
}
protected override void OnHandleCreated(EventArgs e)
{
_handlesCreated++;
base.OnHandleCreated(e);
}
private bool RunningInDesigner
{
get
{
// this control is mobile-only so if we are running on full
// windows we must be in design mode. Note that this works
// even if we are not Sited yet, where as checking the
// DesignMode property can't be done untl the control has
// a Site.
return (System.Environment.OSVersion.Platform == PlatformID.Win32NT
|| System.Environment.OSVersion.Platform == PlatformID.Win32Windows);
}
}
}
关于.net - 如何在自定义 CF 控件中覆盖 Ambient BackColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068809/
我在两个不同的 C# Windows 窗体示例中使用了工具提示。这些示例的工具提示的背景颜色会有所不同,如下所示。 ToolTip with yellow BackColor ToolTip with
我想知道如何为TextBox中的选定文本实现自定义背景色。默认情况下,它对所选文本使用Windows的标准颜色(浅蓝色)。由于我使用的是基于winforms TextBox的皮肤文本编辑器,而Text
当某个值来自串行端口时,我想在固定时间内更改 Button 的 BackColor。我在这里设置了 Timer 对象: public formLabel() { InitializeCompo
如何将代码隐藏中的文本框 BackColor 更改为如下所示: Textbox1.BackColor = "#F2F0E1 代替 Textbox1.BackColor = System.Drawing
我想写一个简单的“注册/登录”程序,只是为了我,只是为了好玩。 我想更改用户键入姓名的 TxtBox 的颜色。当txtBox.Length<4它应该将其背景更改为红色。 我不知道为什么我的下面的代码不
我使用的是 OwnerDraw = true,我无法更改项目的背景颜色(我还有几个子项目并且 ListView 设置为详细信息 View )。 最佳答案 我不确定这是否有帮助,因为这个问题有点含糊,但
我的 UITableView 显示来自 SOAP 网络服务的可变数据。最后一行显示总计。我怎样才能仅 TableView 的最后一行设置灰色背景? 这是我的: - (UITableViewCell *
我有一个像下面两个这样的 ProgressBar 控件: 第一个画得很好。如您所见,第二个只有一个0,它应该有两个但另一个看不到,因为ProgressBar 的ForeColor 与TextColor
我想知道如何将用户窗体中标签的背景颜色更改回默认值? (透明或类似 UserForm 的颜色) Info_Label.BackColor = xlTransparent 或 Info_Label.Ba
概览 我目前正在为我的应用开发一个主题系统,允许用户选择浅色和深色主题(类似于 visual studio)。整个过程非常简单,这就是我目前更改 ListView 控件的 BackColor 属性的方
是否可以更改 ToolStripSeparator 控件的 BackColor?设计器中有一个 BackColor 属性,但它似乎没有被使用 - 颜色始终为白色。 最佳答案 我只是将分隔符的 Pain
我有一个看起来像这样的数据表: Row1 Row2 Row3 Row4 Row5 Row6 Gold Gold Pink
我有一个包含 4 个标签的表单。我希望这些标签以指定的频率闪烁,例如 12.5、10、8 和 4 HZ。我使用了计时器,但它无法正常工作,它们闪烁的频率要低得多,我知道这是因为下面的 freqMeth
我重写 OnPaint 方法是为了将颜色放入 DateTimePicker 控件的文本框中,并禁用在文本框中手动键入? 你有解决这个问题的想法吗? public class BCDateTimePic
我有一个 Compact Framework (3.5) 控件,我希望它具有 SystemColors.Window 的默认颜色,外观类似于编辑控件。我遇到的问题是 Visual Studio (20
我在 VS 2015 中使用 Win Form Metro Framework 在 Windows 7 中构建带有 Metro Tiles 的 Metro Form。 .但是,它不起作用。没有任何反应
我想使用不同的颜色为每个 ListView 单元格的 BackColor 着色。这可能吗? 最佳答案 要更改单元格的BackColor 颜色,您可以这样做: listView1.Items[0].Us
如何在 IE7 中更改 DropDownList BackColor?在 Firefox 中,它正确绘制了整个 DropDown,但在 IE7 中,DropDown 的边框始终保留白色。 最佳答案 本
我希望在 ASP.NET 表单中获取标签的背景色,并使用此值更改此标签的颜色这可能吗? '> 谢谢。 最佳答案 //标签,通过HexaColor设置前景色 lblResult.ForeColor =
有没有一种简单的方法可以让选定的 TreeNode 保留其 SystemColors.Highlight BackColor 而 TreeView 没有焦点?因为即使将 HideSelection 设
我是一名优秀的程序员,十分优秀!