Friday, January 30, 2009

C#: Generic Error Handling Routine (WinApp)

Just call the LogError method from any function within the catch block and pass the exception (ex) to this method...

public static void LogError(Exception ex)
{
string EventLogSourceName = "Application Name";
int EventLogErrorCode = "99";
string msg = "The application encountered an unknown error:";
msg += "\r\nExecuting Method: " + new System.Diagnostics.StackFrame(1, false).GetMethod().Name;
msg += "\r\nError Message: " + ex.Message;

EventLog.WriteEntry(EventLogSourceName, msg, EventLogEntryType.Error, EventLogErrorCode);
MessageBox.Show(msg, "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}

Thursday, December 4, 2008

C#: Calculate Statistics of a Word Document

//***
//Label names are self Explanatory
//Stats include content from FootNotes and EndNotes
//Use objFalse = false instead of objMissing to exclude FootNotes and EndNotes
//***

Word.Document WDocument = Globals.ThisAddIn.Application.ActiveDocument;
object objMissing = System.Reflection.Missing.Value;

lblNumberOfPages.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref objMissing).ToString();

lblNumberOfParagraphs.Text = WDocument.ComputeStatistics(Word.WdStatistic.wd
StatisticParagraphs, ref objMissing).ToString();

lblNumberOfSentences.Text = WDocument.Sentences.Count.ToString();

lblNumberOfLines.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticLines, ref objMissing).ToString();

lblNumberOfWords.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticWords, ref objMissing).ToString();

lblNumberOfCharactersIncludingSpaces.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces, ref objMissing).ToString();

lblNumberOfCharactersExcludingSpaces.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters, ref objMissing).ToString();

Thursday, November 20, 2008

C#: Capture ScreenShot

Courtesy: NOBUGZ (http://forums.microsoft.com/MSDN/User/Profile.aspx?UserID=170041&SiteID=1)


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace KSeeSharp
{
class ScreenShot
{
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);

public void CaptureScreenShot(string filePath)
{
Size sz = Screen.PrimaryScreen.Bounds.Size;
IntPtr hDesk = GetDesktopWindow();
IntPtr hSrce = GetWindowDC(hDesk);
IntPtr hDest = CreateCompatibleDC(hSrce);
IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
IntPtr hOldBmp = SelectObject(hDest, hBmp);
bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
Bitmap bmp = Bitmap.FromHbitmap(hBmp);
SelectObject(hDest, hOldBmp);
DeleteObject(hBmp);
DeleteDC(hDest);
ReleaseDC(hDesk, hSrce);
bmp.Save(filePath);
bmp.Dispose();
}

}
}