您的位置:首页 > 移动开发 > Objective-C

关于仿照QQ 和 MSN的网页谈出窗口控件值得拿来学习!

2006-03-15 17:48 459 查看
using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;

namespace EeekSoft.Web
{
  /// <summary>
  /// Popup window anchor control
  /// Popup window 瞄定控件
  /// </summary>
  [DefaultProperty("PopupToShow"), ToolboxData("<{0}:PopupWinAnchor runat=server></{0}:PopupWinAnchor>")]
  [Designer(typeof(AnchorDesigner))]
  public class PopupWinAnchor : System.Web.UI.WebControls.WebControl
  {
    #region Variables:变量

    string controlId,controlLink,jsEvent;
    string snMsg,snText,snTitle;
    bool bChangeText=false;

    #endregion

    #region Constructor 构造器

    /// <summary>
    /// Create control创建控件
    /// </summary>
    public PopupWinAnchor()
    {
      jsEvent="onclick";
      bChangeText=false;
    }

    #endregion
    #region Properties 属性

    /// <summary>
    /// Should texts on PopupWin be replaced with new texts ?
    /// 该文本在PopupWin控件被替换的文本
    /// </summary>
    [Bindable(true),Category("PopupWin"),DefaultValue(false)]
    [Description("Should texts on PopupWin be replaced with new texts ?")]
    public bool ChangeTexts
    {
      get { return bChangeText; }
      set { bChangeText=value; }
    }

    /// <summary>
    /// New message text
    /// 新的消息文本
    /// </summary>
    [Bindable(true),Category("PopupWin"),DefaultValue("")]
    [Description("New message text")]
    public string NewMessage
    {
      get { return snMsg; }
      set { snMsg=value; }
    }

    /// <summary>
    /// New popup title text
    /// 新的popup标题
    /// </summary>
    [Bindable(true),Category("PopupWin"),DefaultValue("")]
    [Description("New popup title text")]
    public string NewTitle
    {
      get { return snTitle; }
      set { snTitle=value; }
    }

    /// <summary>
    /// New text to show in opened window
    /// 显示在新打开window中文本内容
    /// </summary>
    [Bindable(true),Category("PopupWin"),DefaultValue("")]
    [Description("New text to show in opened window")]
    public string NewText
    {
      get { return snText; }
      set { snText=value; }
    }

    /// <summary>
    /// JavaScript event to handle
    /// JavaScript事件处理方式
    /// </summary>
    [Bindable(true),Category("Anchor"),DefaultValue("onclick")]
    [Editor(typeof(JavaScriptEventEditor),typeof(UITypeEditor))]
    [Description("JavaScript event to handle")]
    public string HandledEvent
    {
      get { return jsEvent; }
      set { jsEvent=value; }
    }

 
4000
   /// <summary>
    /// Popup control to show when event occurs
    /// 当事件发生时Popup控件显示
    /// </summary>
    [Bindable(true),Category("Anchor"),DefaultValue("")]
    [Editor(typeof(PopupControlsEditor), typeof(UITypeEditor))]
    [Description("Popup control to show when event occurs")]
    public string PopupToShow
    {
      get { return controlId; }
      set { controlId=value; }
    }

   
    /// <summary>
    /// Contol which event will cause the popup to show
    /// 哪个事件将使popup显示出来
    /// </summary>
    [Bindable(true),Category("Anchor"),DefaultValue("")]
    [Editor(typeof(AllControlsEditor), typeof(UITypeEditor))]
    [Description("Control ")]
    public string LinkedControl
    {
      get { return controlLink; }
      set { controlLink=value; }
    }

    #endregion
    #region Methods方法

    /// <summary>
    /// Render script for displaying popup control
    /// 使popup控件展开script
    /// </summary>
    /// <param name="output">The HTML writer to write out to</param>
    protected override void Render(HtmlTextWriter output)
    {
      output.Write(@"
        <script type=""text/javascript"">
        //<![CDATA[

        var "+ID+@"oldOnLoad=window.onload;
        window.onload="+ID+@"espopup_anchorInit;
        function "+ID+@"espopup_anchorInit()
        {
          if ("+ID+@"oldOnLoad!=null) "+ID+@"oldOnLoad();
          document.getElementById('"+controlLink+@"')."+jsEvent+@"="+
        ID+@"espopup_anchorEvent;
        }

        function "+ID+@"espopup_anchorEvent()
        {
          ");
      if (bChangeText)
      {
        Control ct=Page.FindControl(controlId);
        if (ct!=null)
        {
          output.Write(controlId+"nText=/""+
            ((PopupWin)ct).GetWinText(snTitle,snText)+"/";/n");
        }
        output.Write(controlId+"nMsg=/""+snMsg+"/";/n");
        output.Write(controlId+"nTitle=/""+snTitle+"/";/n");
        output.Write(controlId+"bChangeTexts=true;/n");
      }
      else
      {
        output.Write(controlId+"bChangeTexts=false;/n");
      }
      output.Write("/n"+controlId+@"espopup_ShowPopup('"+ID+@"');
        }
        //]]>
        </script>");
    }

    #endregion
  }

 
  /// <summary>
  /// Class for displaying PopupWin in designer
  /// 在设计器使用PopupWin的类
  /// </summary>
  public class AnchorDesigner : ControlDesigner
  {
    #region Overriden methods

    /// <summary>
    /// Returns HTML code to show in designer
    /// 返回显示在设计器的 HTML代码
    /// </summary>
    public override string GetDesignTimeHtml()
    {
      return "<div style=/"padding:2px; background-color: ButtonFace;color:ButtonText; "+
        "border-style:outset; border-width:1px; font: 75% 'Microsoft Sans Serif';/"><b>"+
        "PopupWinAnchor</b> - "+((Control)Component).ID+"</div>";
    }
   
    #endregion
  }

  /// <summary>
  /// Editor for selecting JavaScript event
  /// 编辑选择JavaScript的事件
  /// </summary>
  public class JavaScriptEventEditor : UITypeEditor
  {
    #region Variables变量

    private System.Windows.Forms.Design.IWindowsFormsEditorService edSvc=null;
    private System.Windows.Forms.ListBox lb;

    #endregion
    #region Methods方法

    /// <summary>
    /// Overrides the method used to provide basic behaviour for selecting editor.
    /// Shows our custom control for editing the value.
 /// 忽略使用的方法为选择提供基本的行为编辑。
 /// 显示我们的习惯控制的编辑方式。
    /// </summary>
    /// <param name="context">The context of the editing control组件的上下文信息</param>
    /// <param name="provider">A valid service provider服务提供的机制</param>
    /// <param name="value">The current value of the object to edit编辑对象的通用值</param>
    /// <returns>The new value of the object对象的新值</returns>
    public override object EditValue(ITypeDescriptorContext context,
      IServiceProvider provider,object value)
    {
      if (context!=null&&context.Instance!=null&&provider!=null)
      {
        edSvc=(System.Windows.Forms.Design.IWindowsFormsEditorService)
          provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
        if (edSvc!=null)
        {     
          lb=new System.Windows.Forms.ListBox();
          lb.BorderStyle=System.Windows.Forms.BorderStyle.None;
          lb.SelectedIndexChanged+=new EventHandler(lb_SelectedIndexChanged);
          lb.Items.Add("onclick");
          lb.Items.Add("ondblclick");
          lb.Items.Add("onmouseover");
          lb.Items.Add("onfocus");
          lb.Items.Add("oncontextmenu");
          edSvc.DropDownControl(lb);
          if (lb.SelectedIndex==-1) return value;
          return lb.SelectedItem;
        }
      }

      return value;
    }

    /// <summary>
    /// Choose editor type
    /// 选择编辑类别
    /// </summary>
    /// <param name="context">The context of the editing control编辑组件的上下文信息</param>
    /// <returns>Returns <c>UITypeEditorEditStyle.DropDown编辑样式</c></returns>
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
      return UITypeEditorEditStyle.DropDown;   
    }

    /// <summary>
    /// Close the dropdowncontrol when the user has selected a value
    /// 当选择值时关闭dropdowncontrol控件
    /// </summary>
    private void lb_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (edSvc != null)
      {
        edSvc.CloseDropDown();
      }
    }

    #endregion
  }

  /// <summary>
  /// Editor for selecting controls from Asp.Net page
  /// 从Asp.Net页面文件里面选择编辑器
  /// </summary>
  public abstract class ControlsEditor : UITypeEditor
  {
    #region Variables变量

    private System.Windows.Forms.Design.IWindowsFormsEditorService edSvc=null;
    private System.Windows.Forms.ListBox lb;
    private Type typeShow;

    #endregion
    #region Constructor构造

    /// <summary>
    /// onstructor - show specified types
    /// 构造--显示特殊类型
    /// </summary>
    /// <param name="show">Type descriptor类型描述符</param>
    public ControlsEditor(Type show)
    {
      typeShow=show;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Overrides the method used to provide basic behaviour for selecting editor.
    /// Shows our custom control for editing the value.
 /// 忽略使用的方法为选择提供基本的行为编辑。
 /// 显示我们的习惯控制的编辑方式。
    /// </summary>
    /// <param name="context">The context of the editing control编辑控件的上下文信息</param>
    /// <param name="provider">A valid service provider 一个有效的服务提供者</param>
    /// <param name="value">The current value of the object to edit编辑对象的当前值</param>
    /// <returns>The new value of the object返回对象的值</returns>
    public override object EditValue(ITypeDescriptorContext context,
      IServiceProvider provider,object value)
    {
      if (context!=null&&context.Instance!=null&&provider!=null)
      {
        edSvc=(System.Windows.Forms.Design.IWindowsFormsEditorService)
          provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
        if (edSvc!=null)
        {     
          lb=new System.Windows.Forms.ListBox();
          lb.BorderStyle=System.Windows.Forms.BorderStyle.None;
          lb.SelectedIndexChanged+=new EventHandler(lb_SelectedIndexChanged);
          foreach(Control ctrl in ((Control)context.Instance).Page.Controls)
          {
            if (ctrl.GetType().IsSubclassOf(typeShow)||
              ctrl.GetType().FullName==typeShow.FullName) lb.Items.Add(ctrl.ID);
          }
          edSvc.DropDownControl(lb);
          if (lb.SelectedIndex==-1) return value;
          return lb.SelectedItem;
        }
      }

      return value;
    }

    /// <summary>
    /// Choose editor type
    /// 选择编辑类别
    /// </summary>
    /// <param name="context">The context of the editing control编辑控件的上下文信息</param>
    /// <returns>Returns <c>UITypeEditorEditStyle.DropDown控件样式</c></returns>
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
      return UITypeEditorEditStyle.DropDown;   
    }

    /// <summary>
    /// Close the dropdowncontrol when the user has selected a value
    /// </summary>
    private void lb_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (edSvc != null)
      {
        edSvc.CloseDropDown();
      }
    }

    #endregion
  }

  /// <summary>
  /// Editor for selecting all Asp.Net controls
  /// </summary>
  public class AllControlsEditor : ControlsEditor
  {
    #region Members

    /// <summary>
    /// Invoke base constructor 调用基类构造器
    /// </summary>
    public AllControlsEditor() : base(typeof(Control)) {}

    #endregion
  }

  /// <summary>
  /// Editor for selecting PopupWin controls
  /// </summary>
  public class PopupControlsEditor : ControlsEditor
  {
    #region Members

    /// <summary>
    /// Invoke base constructor
    /// </summary>
    public PopupControlsEditor() : base(typeof(PopupWin)) {}

    #endregion
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息