Windows 窗体模拟时钟集成到 Web 部件 Moss 2007

发布于 2024-10-23 12:54:01 字数 8776 浏览 6 评论 0原文

我有一个 Windows 形式的模拟时钟,我需要创建一个 Web 部件(moss 2007)。

我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Web;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Drawing;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;


namespace ClockWebPart 
{
    public class ClockWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        Form form = new Form();

        ///  Constructor
        public ClockWebPart()
        {
            InitializeComponent();
        }

        /// Initialization here
        private void ClockDesign_Load(object sender, EventArgs e)
        {
            try
            {
                // read the embeded resource
                Assembly asmImage = Assembly.GetExecutingAssembly();
                Stream streamImage = asmImage.GetManifestResourceStream("ClockWebPart.clock.bmp");
                Bitmap bmpBackground = new Bitmap(streamImage);
                SetFormBackgroundImage(bmpBackground);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Resource wasn't found!");
            }
        }


        /// <summary>
        ///  Set the form background
        /// </summary>
        /// <param name="bmpImage"></param>
        private void SetFormBackgroundImage(Bitmap bmpImage)
        {
            Color clrPixel = bmpImage.GetPixel(0, 0);
            bmpImage.MakeTransparent(clrPixel);
            form.BackgroundImage = bmpImage;
            // Set the form size from image size
            form.Size = bmpImage.Size;
        }

        /// Override the paint event
        //protected override void OnLoad(EventArgs e)
        //{
          //  base.OnLoad(e);
        //}


        protected void OnPreRender(System.Windows.Forms.PaintEventArgs e)
        {
            // Set the origin to center of the form
            e.Graphics.TranslateTransform(80.0F, 80.0F);

            // Save translated graphics state; So origin will remain at center of form when restore
            GraphicsState transState = e.Graphics.Save();

            // Capture a copy of current time for consistent
            DateTime dtNow = DateTime.Now;

            // rotation starts from new center of the form
            e.Graphics.RotateTransform(dtNow.Second * 6.0F - 90.0F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            // draw the second hand at new center of the form
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 55, 2);

            //// Restore graphics state to translated state and fill second hand
            e.Graphics.Restore(transState);

            // minus 90 degree because start at x-axis
            e.Graphics.RotateTransform(dtNow.Minute * 6.0F - 90.0F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 45, 3);

            //// Restore graphics state to translated state and fill minute hand
            //gHands.Restore(transState);
            // Reset transformation matrix to identity and fill rectangle.
            e.Graphics.ResetTransform();
            // Set the origin to center of the form
            e.Graphics.TranslateTransform(80.0F, 80.0F);

            // minus 90 degree because start at x-axis; Minute affects hour hand too
            e.Graphics.RotateTransform(dtNow.Hour * 30.0F - 90.0F + dtNow.Minute * 0.5F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 35, 4);
        }


        /// Force the form to repaint for every tick

        private void tmrRotate_Tick(object sender, EventArgs e)
        {
            // Force to redraw
            //this.Invalidate();
            form.Refresh();
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>


        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ClockWebPart));
            this.niMainMenu = new System.Windows.Forms.NotifyIcon(this.components);
            this.cmsAllMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.tsmiAbout = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiSeparator = new System.Windows.Forms.ToolStripSeparator();
            this.tsmiExit = new System.Windows.Forms.ToolStripMenuItem();
            this.tmrRotate = new System.Windows.Forms.Timer(this.components);
            this.cmsAllMenu.SuspendLayout();
            form.SuspendLayout();
            // 
            // niMainMenu
            // 
            this.niMainMenu.ContextMenuStrip = this.cmsAllMenu;
            this.niMainMenu.Icon = ((System.Drawing.Icon)(resources.GetObject("niMainMenu.Icon")));
            this.niMainMenu.Text = "Time flies!";
            this.niMainMenu.Visible = true;
            // 
            // cmsAllMenu
            // 
            this.cmsAllMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmiAbout,
            this.tsmiSeparator,
            this.tsmiExit});
            this.cmsAllMenu.Name = "cmsAllMenu";
            this.cmsAllMenu.Size = new System.Drawing.Size(108, 54);
            // 
            // tsmiAbout
            // 
            this.tsmiAbout.Name = "tsmiAbout";
            this.tsmiAbout.Size = new System.Drawing.Size(107, 22);
            this.tsmiAbout.Text = "About";
            //this.tsmiAbout.Click += new System.EventHandler(this.tsmiAbout_Click);
            // 
            // tsmiSeparator
            // 
            this.tsmiSeparator.Name = "tsmiSeparator";
            this.tsmiSeparator.Size = new System.Drawing.Size(104, 6);
            // 
            // tsmiExit
            // 
            this.tsmiExit.Name = "tsmiExit";
            this.tsmiExit.Size = new System.Drawing.Size(107, 22);
            this.tsmiExit.Text = "Exit";
            //this.tsmiExit.Click += new System.EventHandler(this.tsmiExit_Click);
            // 
            // tmrRotate
            // 
            this.tmrRotate.Enabled = true;
            this.tmrRotate.Interval = 1000;
            this.tmrRotate.Tick += new System.EventHandler(this.tmrRotate_Tick);
            // 
            // frmIrregular
            // 
            form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            form.ClientSize = new System.Drawing.Size(160, 160);
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            form.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            form.Name = "frmIrregular";
            form.ShowInTaskbar = false;
            form.Text = "Time flies";
            form.TransparencyKey = System.Drawing.SystemColors.Control;
            form.Load += new System.EventHandler(this.ClockDesign_Load);
            //this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmIrregular_MouseDown);
            //this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmIrregular_MouseMove);
            this.cmsAllMenu.ResumeLayout(false);
            form.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NotifyIcon niMainMenu;
        private System.Windows.Forms.ContextMenuStrip cmsAllMenu;
        private System.Windows.Forms.ToolStripMenuItem tsmiAbout;
        private System.Windows.Forms.ToolStripMenuItem tsmiExit;
        private System.Windows.Forms.ToolStripSeparator tsmiSeparator;
        private System.Windows.Forms.Timer tmrRotate;



    }
}

给出错误

Web 部件错误:发生错误。

有什么问题吗?

I have a windows form analog clock and I need to create a web part (moss 2007).

my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Web;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Drawing;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;


namespace ClockWebPart 
{
    public class ClockWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        Form form = new Form();

        ///  Constructor
        public ClockWebPart()
        {
            InitializeComponent();
        }

        /// Initialization here
        private void ClockDesign_Load(object sender, EventArgs e)
        {
            try
            {
                // read the embeded resource
                Assembly asmImage = Assembly.GetExecutingAssembly();
                Stream streamImage = asmImage.GetManifestResourceStream("ClockWebPart.clock.bmp");
                Bitmap bmpBackground = new Bitmap(streamImage);
                SetFormBackgroundImage(bmpBackground);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Resource wasn't found!");
            }
        }


        /// <summary>
        ///  Set the form background
        /// </summary>
        /// <param name="bmpImage"></param>
        private void SetFormBackgroundImage(Bitmap bmpImage)
        {
            Color clrPixel = bmpImage.GetPixel(0, 0);
            bmpImage.MakeTransparent(clrPixel);
            form.BackgroundImage = bmpImage;
            // Set the form size from image size
            form.Size = bmpImage.Size;
        }

        /// Override the paint event
        //protected override void OnLoad(EventArgs e)
        //{
          //  base.OnLoad(e);
        //}


        protected void OnPreRender(System.Windows.Forms.PaintEventArgs e)
        {
            // Set the origin to center of the form
            e.Graphics.TranslateTransform(80.0F, 80.0F);

            // Save translated graphics state; So origin will remain at center of form when restore
            GraphicsState transState = e.Graphics.Save();

            // Capture a copy of current time for consistent
            DateTime dtNow = DateTime.Now;

            // rotation starts from new center of the form
            e.Graphics.RotateTransform(dtNow.Second * 6.0F - 90.0F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            // draw the second hand at new center of the form
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 55, 2);

            //// Restore graphics state to translated state and fill second hand
            e.Graphics.Restore(transState);

            // minus 90 degree because start at x-axis
            e.Graphics.RotateTransform(dtNow.Minute * 6.0F - 90.0F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 45, 3);

            //// Restore graphics state to translated state and fill minute hand
            //gHands.Restore(transState);
            // Reset transformation matrix to identity and fill rectangle.
            e.Graphics.ResetTransform();
            // Set the origin to center of the form
            e.Graphics.TranslateTransform(80.0F, 80.0F);

            // minus 90 degree because start at x-axis; Minute affects hour hand too
            e.Graphics.RotateTransform(dtNow.Hour * 30.0F - 90.0F + dtNow.Minute * 0.5F);
            // Anti-alias only affect the next shape
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(new SolidBrush(Color.Silver), -1, -1, 35, 4);
        }


        /// Force the form to repaint for every tick

        private void tmrRotate_Tick(object sender, EventArgs e)
        {
            // Force to redraw
            //this.Invalidate();
            form.Refresh();
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>


        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ClockWebPart));
            this.niMainMenu = new System.Windows.Forms.NotifyIcon(this.components);
            this.cmsAllMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.tsmiAbout = new System.Windows.Forms.ToolStripMenuItem();
            this.tsmiSeparator = new System.Windows.Forms.ToolStripSeparator();
            this.tsmiExit = new System.Windows.Forms.ToolStripMenuItem();
            this.tmrRotate = new System.Windows.Forms.Timer(this.components);
            this.cmsAllMenu.SuspendLayout();
            form.SuspendLayout();
            // 
            // niMainMenu
            // 
            this.niMainMenu.ContextMenuStrip = this.cmsAllMenu;
            this.niMainMenu.Icon = ((System.Drawing.Icon)(resources.GetObject("niMainMenu.Icon")));
            this.niMainMenu.Text = "Time flies!";
            this.niMainMenu.Visible = true;
            // 
            // cmsAllMenu
            // 
            this.cmsAllMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmiAbout,
            this.tsmiSeparator,
            this.tsmiExit});
            this.cmsAllMenu.Name = "cmsAllMenu";
            this.cmsAllMenu.Size = new System.Drawing.Size(108, 54);
            // 
            // tsmiAbout
            // 
            this.tsmiAbout.Name = "tsmiAbout";
            this.tsmiAbout.Size = new System.Drawing.Size(107, 22);
            this.tsmiAbout.Text = "About";
            //this.tsmiAbout.Click += new System.EventHandler(this.tsmiAbout_Click);
            // 
            // tsmiSeparator
            // 
            this.tsmiSeparator.Name = "tsmiSeparator";
            this.tsmiSeparator.Size = new System.Drawing.Size(104, 6);
            // 
            // tsmiExit
            // 
            this.tsmiExit.Name = "tsmiExit";
            this.tsmiExit.Size = new System.Drawing.Size(107, 22);
            this.tsmiExit.Text = "Exit";
            //this.tsmiExit.Click += new System.EventHandler(this.tsmiExit_Click);
            // 
            // tmrRotate
            // 
            this.tmrRotate.Enabled = true;
            this.tmrRotate.Interval = 1000;
            this.tmrRotate.Tick += new System.EventHandler(this.tmrRotate_Tick);
            // 
            // frmIrregular
            // 
            form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            form.ClientSize = new System.Drawing.Size(160, 160);
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            form.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            form.Name = "frmIrregular";
            form.ShowInTaskbar = false;
            form.Text = "Time flies";
            form.TransparencyKey = System.Drawing.SystemColors.Control;
            form.Load += new System.EventHandler(this.ClockDesign_Load);
            //this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmIrregular_MouseDown);
            //this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmIrregular_MouseMove);
            this.cmsAllMenu.ResumeLayout(false);
            form.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NotifyIcon niMainMenu;
        private System.Windows.Forms.ContextMenuStrip cmsAllMenu;
        private System.Windows.Forms.ToolStripMenuItem tsmiAbout;
        private System.Windows.Forms.ToolStripMenuItem tsmiExit;
        private System.Windows.Forms.ToolStripSeparator tsmiSeparator;
        private System.Windows.Forms.Timer tmrRotate;



    }
}

It's give error

Web Part Error: An error has occurred.

what is problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-10-30 12:54:01

导致此错误的原因可能有很多。

using System.Linq;

MOS 2007 中肯定不支持。删除此行并进行测试。

还要禁用 web.config 中的自定义错误以显示完整错误。

There can be many reasons for this error.

using System.Linq;

is defenitely not supported in MOS 2007. Remove this line and test.

Also disable custom errors in web.config to display full error.

耳钉梦 2024-10-30 12:54:01

这可能不会导致您的错误,但除非您需要,否则建议您的 Web 部件从 System.Web.UI.WebControls.WebParts.WebPart 继承,而不是 Microsoft.SharePoint.WebPartPages.WebPart。

有关更多详细信息,请参阅 WebPart 类。

This probably is not causing your error, but unless you need to, it is recommended to have your web part inherit from System.Web.UI.WebControls.WebParts.WebPart instead of Microsoft.SharePoint.WebPartPages.WebPart.

For more details, see the Remarks section of WebPart Class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文