Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在自定义CF控件中覆盖环境背景色_.net_Visual Studio_Compact Framework_Custom Controls - Fatal编程技术网

.net 如何在自定义CF控件中覆盖环境背景色

.net 如何在自定义CF控件中覆盖环境背景色,.net,visual-studio,compact-framework,custom-controls,.net,Visual Studio,Compact Framework,Custom Controls,我有一个紧凑的框架(3.5)控件,我希望它有一个默认颜色SystemColors.Window,类似于编辑控件。我遇到的问题是Visual Studio(2008)总是在设计图面上使用控件的默认背景色。我假设它是从基本控件的默认背景色、父控件或站点中提取的 有人知道如何指定或以其他方式通知VisualStudio默认背景色应该是什么吗 public class MoneyInput : Control { public MoneyInput() {

我有一个紧凑的框架(3.5)控件,我希望它有一个默认颜色
SystemColors.Window
,类似于编辑控件。我遇到的问题是Visual Studio(2008)总是在设计图面上使用
控件的默认
背景色
。我假设它是从基本
控件
的默认背景色、父控件或
站点
中提取的

有人知道如何指定或以其他方式通知VisualStudio默认背景色应该是什么吗

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文件的相应部分。请注意,我并不真的希望默认颜色为白色,正如您在下面看到的那样,我只是想让任何东西正常工作:

true
真的
设计器序列化可见性。可见
系统、绘图、颜色
系统。绘图。颜色。白色
外表
控件的背景色

为了添加更多的进度(或缺少进度),我在控件中添加了一个日志,以便在将控件放到设计图面上时可以看到发生了什么。对构造函数中背景色的写入确实生效,并将颜色从控件更改为窗口。但是设计器基础结构中的某些东西(通过属性)将颜色设置回控制状态。这发生在ctor之后,但在OnResize和OnHandleCreated之前。现在,如果控件使用正确的值将BackColor属性序列化到设计器中,则可以安全地将其设置回原位,但设计器不包含默认值


最后一次编辑,我认为用于指定枚举(相对于基类型)的默认值语法不正确。我正在使用我在下面发布的解决方案,但这是一个黑客攻击,希望下一个出现的人能够真正解决它。

我仍然希望得到一个真正的答案,但我将在这里列出我的解决方案:

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);
      }
  }

}     

你能创建一个
OnShow
事件并在那里给你的控件上色吗?@jp2code没有要重写的OnShow方法。
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);
      }
  }

}