1. 程式人生 > >Winform自定義表單(轉)

Winform自定義表單(轉)

serialize ria tro 修改 bench ron att design control

出處:http://www.newlifex.com/showtopic-167.aspx

好吧,附件真的損壞了,原始代碼我也沒有了,再提取我也沒精力了,不好意思,哪位之前下過可以重發一遍嗎?不過即使沒有也可以參考下面幾個示例很快就可以做出來了......

最近在做個項目,業務方面較繁瑣,用戶需要自定義數據庫,也就是石頭開源的魔方所提供的功能,但winform實現自定義表單,這個......
在網上搜索了前人的代碼發現以下幾個參考意義:
http://www.codeproject.com/Articles/24385/Have-a-Great-DesignTime-Experience-with-a-Powerful
http://www.codeproject.com/Articles/60175/The-DesignSurface-Extended-Class-is-Back-Together
http://www.cnblogs.com/michael-zhang/articles/655267.html
http://www.cnblogs.com/pvistely/archive/2006/02/09/327656.html

不過功能都不是很全面,相似之處都是在SD中提取的,哈哈。
把這幾個例子整合了一下,發現還缺少最重要的功能對設計窗體的序列化功能都沒有啊,於是重新翻閱了一個SD3.2的源代碼把XmlForm相關的代碼提取出來,然後用XML to Linq重新修改了一遍,效果圖如下,界面是用dotnetbar組件:

技術分享圖片技術分享圖片

由於整個編輯器關聯太多東東,所以我只把核心功能提出出來,有興趣的朋友可以看看,由於時間關系,菜單命令我只添加了有限的幾個,完整的可以自己添加看看效果,全在源代碼中。
還有自定義屬性在資源文件中Properties.xml設置,由於我用的是多語言包的,所以在分享的源代碼中屏蔽了部分代碼,在HmCustomProperty類中進行設置,大家看看就會懂的。

另外如果有些控件的屬性對象復雜的話,需要對這個屬性單獨進行序列化,需要在DesignSurfaceExt類的GetElementFor方法和XmlLoader類的SetAttributes方法中分別設置,我用的是SharpSerializer來實現屬性對象序列化的。

還有就是比較復雜的控件如Tabcontrol之類的,也需要自己單獨設置,大家可以查看一下sd的源代碼看看。

還有一個問題沒有解決,如果在設計窗體中使用快捷鍵,我把sd中的FormKeyHandler類提取出來,就會有一個問題,我按del鍵刪除一個控件時沒有問題,但是在在屬性中編輯某個屬性時也會用到del鍵,這時也會把設計窗體中的選中的控件刪除,技術分享圖片

.net版本用的是2.0,所以需要手動添加system.core.dll和system.xml.linq.dll的引用,還有newlife.core.dll的引用。

下面是我上傳的源代碼,運行時的截圖:

技術分享圖片
*******************************************************
壓縮包損壞缺少一個文件EventBindingService.cs,現在附件上傳不了啦,自己該下命名空間把
using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;

namespace HmFramework.UI.Sprite.Services
{
internal class EventBindingServiceExt : System.ComponentModel.Design.EventBindingService
{
public EventBindingServiceExt(IServiceProvider provider)
: base(provider)
{
}

protected override String CreateUniqueMethodName(IComponent component, EventDescriptor e)
{
return String.Format("{0}_{1}", Char.ToUpper(component.Site.Name[0]) + component.Site.Name.Substring(1), e.DisplayName);
}

// sohuld look around in form class for compatiable methodes
protected override ICollection GetCompatibleMethods(EventDescriptor e)
{
ArrayList al = new ArrayList();
MethodInfo methodInfo = e.EventType.GetMethod("Invoke");
if (null != methodInfo)
{
al.Add(methodInfo.Name);
}
return al;
}

protected override Boolean ShowCode()
{
//IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow;
//if (window == null) {
// return false;
//}
//FormsDesignerViewContent formDesigner = window.ActiveViewContent as FormsDesignerViewContent;
//if (formDesigner != null) {
// formDesigner.ShowSourceCode();
// return true;
//}
return false;
}

protected override Boolean ShowCode(Int32 lineNumber)
{
//IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow;
//if (window == null) {
// return false;
//}
//FormsDesignerViewContent formDesigner = window.ActiveViewContent as FormsDesignerViewContent;
//if (formDesigner != null) {
// formDesigner.ShowSourceCode(lineNumber);
// return true;
//}
return false;
}

protected override Boolean ShowCode(IComponent component, EventDescriptor edesc, String methodName)
{
//System.Windows.Forms.MessageBox.Show("to add:" + component.Site.Name + "\r\n" + methodName);
return false;
}
}
}
new.png (94.08 K, 下載次數:2)

(2013/8/13 15:24:02 上傳)

技術分享圖片

designer.png (133.23 K, 下載次數:8)

(2013/8/13 15:24:02 上傳)

技術分享圖片

Winform自定義表單(轉)