Posts
[C#]使用Reflection檢查指定類別是否含有預設建構子
namespace ConsoleApplication17 { class Program { static void Main(string[] args) { Console.WriteLine(HasDefaultConstructor1<TestClass1>()); Console.WriteLine(HasDefaultConstructor2<TestClass1>());
Console.WriteLine(HasDefaultConstructor1<TestClass2>()); Console.WriteLine(HasDefaultConstructor2<TestClass2>()); } private static Boolean HasDefaultConstructor1<T>() { var type = typeof (T); foreach (var c in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)) { if (c.GetParameters().Length ==0) return true; } return false; } private static Boolean HasDefaultConstructor2<T>() { var type = typeof (T); return type.GetConstructor(Type.EmptyTypes) != null; } } internal class TestClass1 { public TestClass1() { } public TestClass1(int arg1) { } } internal class TestClass2 { public TestClass2(int arg1) { } } }
read morePosts
[C#]使用SharpShell實現Shell Icon Overlay功能
namespace ReadOnlyFileIconOverlayHandler { /// <summary> /// The ReadOnlyFileIconOverlayHandler is an IconOverlayHandler that shows /// a padlock icon over files that are read only. /// </summary> [ComVisible(true)] public class ReadOnlyFileIconOverlayHandler : SharpIconOverlayHandler { /// <summary> /// Called by the system to get the priority, which is used to determine /// which icon overlay to use if there are multiple handlers. The priority /// must be between 0 and 100, where 0 is the highest priority.
read morePosts
[C#]使用SHEmptyRecycleBin API清除資源回收桶
<td valign="top" width="200">No dialog box confirming the deletion of the objects will be displayed.</td> </tr> <tr> <td valign="top" width="200"><strong>SHERB_NOPROGRESSUI</strong></td> <td valign="top" width="200">No dialog box indicating the progress will be displayed.</td> </tr> <tr> <td valign="top" width="200"><strong>SHERB_NOSOUND</strong></td> <td valign="top" width="200">No sound will be played when the operation is complete.</td> </tr>
read morePosts
[C#]使用ShowCaret amp; HideCaret控制元件上的插入符號
[DllImport("user32.dll")] static extern bool HideCaret(IntPtr hWnd);</pre></div> namespace WindowsFormsApplication22 { public partial class Form1 : Form { [DllImport(“user32.dll”)] static extern bool ShowCaret(IntPtr hWnd);
[DllImport("user32.dll")] static extern bool HideCaret(IntPtr hWnd); public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { if (rbtnShowCaret.Checked) { ShowCaret(textBox1.Handle); } else { HideCaret(textBox1.Handle); } } } }
read morePosts
[C#]偵測系統Power狀態的改變以及是否進入Sleep mode
namespace WindowsFormsApplication15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); } void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { textBox1.Text += e.Mode.ToString() + Environment.NewLine; } } }
namespace WindowsFormsApplication15 { public partial class Form1 : Form { private const int WM_POWERBROADCAST = 0x218; private const int PBT_APMSUSPEND = 0x4; private const int PBT_APMRESUMESUSPEND = 0x7; private const int PBT_APMRESUMEAUTOMATIC = 0x12; public Form1() { InitializeComponent(); }
read morePosts
[C#]取用.picasa.ini內存的現有資訊來做臉部偵測
rect64(3f845bcb59418507) break this number into 4 16-bit numbers by using substrings: ‘3f845bcb59418507’.substring(0,4) //“3f84” ‘3f845bcb59418507’.substring(4,8) //“5bcb” ‘3f845bcb59418507’.substring(8,12) // “5941” ‘3f845bcb59418507’.substring(12,16) // “8507” convert each obtained substring to an integer and divide it by the highest 16-bit number (2^16 = 65536), which should give 0 < results < 1. these are the relative coordinates of the crop rectangle (left,top,right,bottom): parseInt(“3f84”,16)/65536 //0.24810791015625 - left parseInt(“5bcb”,16)/65536 //0.3585662841796875 - top parseInt(“5941”,16)/65536 //0.3486480712890625 - right parseInt(“8507”,16)/65536 //0.
read morePosts
[C#]在.NET程式中要如何指定Windows的ClassName去接收視窗的訊息
protected override CreateParams CreateParams { get { base.CreateParams.ClassName = "test"; return base.CreateParams; } } }</pre></div> #region Delegate delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); #endregion #region DllImport /// <summary> /// Registers the class W. /// </summary> /// <param name="lpWndClass">The lp WND class.</param> /// <returns></returns> [DllImport("user32.dll", SetLastError = true)] static extern UInt16 RegisterClassW( [In] ref WNDCLASS lpWndClass ); /// <summary> /// Creates the window ex W. /// </summary> /// <param name="dwExStyle">The dw ex style.
read morePosts
[C#]如何使用TraceListener實作類似Visual Studio的輸出視窗
/// <summary> /// Initializes a new instance of the <see cref="ListBoxLogTraceListener"/> class. /// </summary> /// <param name="listBox">The list box.</param> public ListBoxLogTraceListener(ListBox listBox) { m_ListBox = listBox; } /// <summary> /// Writes the output to the OutputDebugString function and to the <see cref="M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)"/> method, followed by a carriage return and line feed ( ). /// </summary> /// <param name=“message”>The message to write to OutputDebugString and <see cref=“M:System.Diagnostics.Debugger.Log(System.Int32,System.String,System.String)”/>.</param> /// <PermissionSet> /// <IPermission class=“System.
read morePosts
[C#]如何使用Windows Shell做Zip檔的壓縮與解壓縮
public static void Compress(string sourceFolderPath, string zipFile) { if (!Directory.Exists(sourceFolderPath)) throw new DirectoryNotFoundException(); if (!File.Exists(zipFile)) File.Create(zipFile).Dispose(); ShellCopyTo(sourceFolderPath, zipFile); } public static void DeCompress(string zipFile, string destinationFolderPath) { if (!File.Exists(zipFile)) throw new FileNotFoundException(); if (!Directory.Exists(destinationFolderPath)) Directory.CreateDirectory(destinationFolderPath); ShellCopyTo(zipFile, destinationFolderPath); } </pre> public class ZipEntry { #region Private Property private FolderItem m_ShellItem { get; set; } private IEnumerable<ZipEntry> _entrys; #endregion #region Public Property /// <summary> /// Gets the name. /// </summary> /// <value>The name.
read more