Posts
[C#]Linq在使用Distinct去除重複資料時如何指定所要依據的成員屬性
/// <summary> /// Gets or sets the name. /// </summary> /// <value>The name.</value> public string Name { get; set; } #endregion #region Public Method /// <summary> /// Returns a <see cref="System.String"/> that represents this instance. /// </summary> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> public override string ToString() { return Name; } #endregion</pre> public bool Equals(Person x, Person y) { return x.Name.Equals(y.Name); } public int GetHashCode(Person obj) { return obj.
read morePosts
[C#]ListBox如何偵測Item的新增、插入、與刪除
switch (m.Msg) { case LB_ADDSTRING: itemValue = Marshal.PtrToStringUni(m.LParam); OnItemAdded(EventArgs.Empty); break; case LB_INSERTSTRING: itemIndex = (int)m.WParam; itemValue = Marshal.PtrToStringUni(m.LParam); OnItemInserted(EventArgs.Empty); break; case LB_DELETESTRING: itemIndex = (int)m.WParam; OnItemDeleted(EventArgs.Empty); break; default: break; } base.WndProc(ref m); } </pre></div> namespace WindowsFormsApplication4 { public partial class Form1 : Form { class ListBoxEx : ListBox { #region Const private const int LB_ADDSTRING = 0x180; private const int LB_INSERTSTRING = 0x181; private const int LB_DELETESTRING = 0x182; #endregion
read morePosts
[C#]MEF開發系列 - Managed Extensibility Framework(MEF)的概念與簡介
public MainForm() { InitializeComponent(); var catalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"); var container = new CompositionContainer(catalog); container.ComposeParts(this); foreach (IModule module in Modules) { module.Host = this; 模組MToolStripMenuItem.DropDownItems.Add(module.Name, null, Module_Click).Tag = module; } } ... } }
read morePosts
[C#]PE檔案格式簡易介紹與PE檔案的檢測
typedef struct _IMAGE_NT_HEADERS { DWORD Signature; IMAGE_FILE_HEADER FileHeader; IMAGE_OPTIONAL_HEADER OptionalHeader; } IMAGE_NT_HEADERS, *PIMAGE_NT_HEADERS;
// Verify file starts with "MZ" signature. if ((bytes[0] != 0x4d) || (bytes[1] != 0x5a)) { // Not a PE file. return false; } // OFFSET_TO_PE_HEADER_OFFSET = 0x3c s.Seek(0x3c, SeekOrigin.Begin); // read the offset to the PE Header uint offset = r.ReadUInt32(); // go to the beginning of the PE Header s.Seek(offset, SeekOrigin.Begin); bytes = r.ReadBytes(4); // Verify PE header starts with 'PE '.
read morePosts
[C#]Process.Exited事件觸發的執行緒會受Process.SynchronizingObject屬性設定的影響
namespace WindowsFormsApplication13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { var process = Process.Start("calc.exe"); process.EnableRaisingEvents = true; process.Exited += new EventHandler(process_Exited); textBox1.Text = "Main Thread ID:" + Thread.CurrentThread.ManagedThreadId.ToString(); Console.Read(); } void process_Exited(object sender, EventArgs e) { MessageBox.Show("Process Thread ID:" + Thread.CurrentThread.ManagedThreadId.ToString()); } } }
namespace WindowsFormsApplication13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
read morePosts
[C#]Set Windows 7 Progress Bar's State
#region DllImport [DllImport("user32.dll", CharSet = CharSet.Auto)] internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); #endregion #region Private Method void SetPaused(ProgressBar progressBar) { SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_PAUSE, 0); } void SetError(ProgressBar progressBar) { SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_ERROR, 0); } void SetNormal(ProgressBar progressBar) { SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_NORMAL, 0); } #endregion</pre></div> namespace WindowsFormsApplication3 { public partial class Form1 : Form { #region Const const int PBM_SETPOS = 0x402; const int PBM_GETPOS = 0x0408; const int PBM_SETSTATE = 0x410; const int PBST_PAUSE = 0x0003; const int PBST_ERROR = 0x0002; const int PBST_NORMAL = 0x0001; #endregion
read morePosts
[C#]仿照Chrome的Multi-process Architecture
var options = new Options(); ICommandLineParser parser = new CommandLineParser(); if (parser.ParseArguments(args, options)) { m_ReceiverHandel = (IntPtr)options.Handle; if (options.IsBrowser) { var browserTab = new WebBrowserPage() { StartPosition = FormStartPosition.Manual, Top = -3200, Left = -3200, Width = 0, Height = 0 }; browserTab.TextChanged += browserTab_TextChanged; Application.Run(browserTab); return; } } Application.Run(new MainForm()); }</pre> var host = new ApplicationHost() { File = Application.ExecutablePath, Arguments = string.Format("-b -h {0}", this.Handle), HideApplicationTitleBar = true, Dock = DockStyle.
read morePosts
[C#]使用BitmapDecoder快速取用圖檔內含的縮圖
public Image GetThumbnail(string file) { var decoder = BitmapDecoder.Create(new Uri(file), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); var frame = decoder.Frames.FirstOrDefault();
return (frame.Thumbnail == null) ? null : frame.Thumbnail.GetBitmap(); } …
namespace WindowsFormsApplication33 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
public Image GetThumbnail(string file) { var decoder = BitmapDecoder.Create(new Uri(file), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); var frame = decoder.Frames.FirstOrDefault(); return (frame.Thumbnail == null) ? null : frame.Thumbnail.GetBitmap(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.
read more