Posts
.NET 4.0 New Feature - StringBuilder.Clear
Module Module1
Sub Main() Dim str As New StringBuilder Dim size As Integer = 100000000 Dim sw As New Stopwatch '預先編譯降低測試誤差 AppendData(str, size) ClearData1(str) ClearData2(str) ClearData3(str) '開始測試 AppendData(str, size) Console.WriteLine("Test Remove...") sw.Restart() ClearData1(str) Console.WriteLine(sw.ElapsedMilliseconds) AppendData(str, size) Console.WriteLine("Test Length...") sw.Restart() ClearData2(str) Console.WriteLine(sw.ElapsedMilliseconds) AppendData(str, size) Console.WriteLine("Test Clear...") sw.Restart() ClearData3(str) Console.WriteLine(sw.ElapsedMilliseconds) End Sub Private Sub AppendData(ByVal str As StringBuilder, ByVal size As Integer) str.Append(New String("0", size)) End Sub Private Sub ClearData1(ByVal str As StringBuilder) str.
read morePosts
.NET 4.0 New Feature - Enum.HasFlag
… Dim myOrder As DinnerItems = DinnerItems.Appetizer Or DinnerItems.Entree Or DinnerItems.Beverage Or DinnerItems.Dessert Dim hasFlag As Boolean = myOrder.HasFlag(DinnerItems.Entree Or DinnerItems.Beverage) …
read morePosts
[VB.NET]用Extension Method取得CustomAttributes
Module EnumExtension
<Extension()> _ Function GetCustomAttributes(Of T)(ByVal e As [Enum]) As IEnumerable(Of T) Return e.GetType.GetField(e.ToString).GetCustomAttributes(GetType(T), False).Cast(Of T)() End Function <Extension()> _ Function GetCustomAttribute(Of T)(ByVal e As [Enum]) As T Return GetCustomAttributes(Of T)(e).FirstOrDefault End Function End Module
Module TypeExtension
<Extension()> _ Function GetCustomAttributes(Of T)(ByVal type As Type) As IEnumerable(Of T) Return type.GetCustomAttributes(GetType(T), False).Cast(Of T)() End Function <Extension()> _ Function GetCustomAttribute(Of T)(ByVal type As Type) As T Return DirectCast(GetCustomAttributes(Of T)(type).FirstOrDefault, T) End Function <Extension()> _ Function GetCustomAttributes(Of T)(ByVal type As Type, ByVal memberName As String) As IEnumerable(Of T) Dim m = type.
read morePosts
[VB.NET]Attribute與反射的搭配使用
Property Data as Object End Class
Property RelativedType As Type Sub New(ByVal relativedType As Type) Me.RelativedType = relativedType End Sub End Class
Enum ApplicationType <RelativedType(GetType(LevelUp.Office.Word))> _ Word
<RelativedType(GetType(LevelUp.Office.Excel))> _ Excel <RelativedType(GetType(LevelUp.Office.Access))> _ Access <RelativedType(GetType(LevelUp.Office.PowerPoint))> _ PowerPoint <RelativedType(GetType(LevelUp.Office.Visio))> _ Visio End Enum
Sub StartApplication(ByVal apType As ApplicationType) Dim type As Type = apType.GetType() Dim field = type.GetField(apType.ToString) Dim att = field.GetCustomAttributes(GetType(RelativedTypeAttribute), False).Cast(Of RelativedTypeAttribute)().FirstOrDefault() Activator.CreateInstance(att.RelativedType).Start() End Sub</pre></div> Property ResourceID As String Sub New(ByVal resourceID As String) Me.
read morePosts
[VB.NET]Merge MDI ToolStrip
ReadOnly Property MainToolStrip() As ToolStrip End Interface 子視窗實做IChildForm介面,把子視窗的工具列開出。 Public Class Form1 Implements IChildForm
Public ReadOnly Property MainToolStrip() As System.Windows.Forms.ToolStrip Implements IChildForm.MainToolStrip Get Return ToolStrip1 End Get End Property End Class 在父視窗中處理MDIChildActivate事件,將工具列合併。 Public Class MDIParent1 Protected Overrides Sub OnMdiChildActivate(ByVal e As System.EventArgs) MyBase.OnMdiChildActivate(e) ToolStripManager.RevertMerge(ToolStrip) Dim childForm As IChildForm = CType(ActiveMdiChild, IChildForm) If childForm IsNot Nothing Then ToolStripManager.Merge(childForm.MainToolStrip, ToolStrip) End If End Sub End Class DownloadMergeMDIToolStrip.zip
read morePosts
[C#]使用WM_SYSCOMMAND訊息控制螢幕模式切換
const int SC_MONITORPOWER = 0xF170; const int WM_SYSCOMMAND = 0x0112; ... SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER , -1); SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER , 1); SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER , 2); </pre></div> <p> </p> <p>這邊為方便後續使用,將程式整理成類別,有需要的自行取用。</p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c3cd5a55-1615-41d4-9ccd-45dd058acb8b" class="wlWriterSmartContent"><pre name="code" class="c#"> public static class MonitorControler { [DllImport(“user32.dll”)] private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
const int SC_MONITORPOWER = 0xF170; const int WM_SYSCOMMAND = 0x0112; //const int SC_SCREENSAVE = 0xF140; public enum MonitorMode : int { MONITOR_ON = -1, MONITOR_STANBY = 1, MONITOR_OFF } public static void ChangeMonitorState(MonitorMode mode) { SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, (int)mode); } public static void MonitorOff() { ChangeMonitorState(MonitorMode.
read more