2011-03-16 64 views
0

我有一個Windows窗體模擬時鐘,我需要創建一個Web部件(青苔2007年)。窗體模擬時鐘集成到Web部件莫斯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部件錯誤:發生了錯誤。

什麼是問題?

回答

0

這個錯誤可能有很多原因。

using System.Linq; 

在MOS 2007中肯定不受支持。刪除此行並進行測試。

還禁用web.config中的自定義錯誤以顯示完整的錯誤。

0

這可能不會導致您的錯誤,但除非需要,否則建議讓您的Web部件繼承自System.Web.UI.WebControls.WebParts.WebPart而不是Microsoft.SharePoint.WebPartPages.WebPart。

欲瞭解更多詳情,請參閱WebPart Class的備註部分。