gpt4 book ai didi

.net - 如何在自定义 CF 控件中覆盖 Ambient BackColor

转载 作者:行者123 更新时间:2023-12-05 01:22:41 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com