我有一个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部件错误:发生了错误。
什么是问题?
答案 0 :(得分:0)
此错误可能有很多原因。
using System.Linq;
MOS 2007中默认不支持。删除此行并进行测试。
同时在web.config中禁用自定义错误以显示完整错误。
答案 1 :(得分:0)
这可能不会导致您的错误,但除非您需要,否则建议您的Web部件继承自System.Web.UI.WebControls.WebParts.WebPart而不是Microsoft.SharePoint.WebPartPages.WebPart。
有关详细信息,请参阅WebPart Class的备注部分。