Friday, August 6, 2010

WPF - Print - Print Preview with report viewer


/*********************************************************************
Description: Printing / Print Preview functionalities can be achieved with report viewer in C#-WPF. Just place following code snippet with your values, should be done in the class derived from Window(WPF base class).
*********************************************************************/




/* You may need to add the reference for following assemblies through Microsoft.ReportViewer.Common.dll, Microsoft.ReportViewer.WinForms.dll, WindowsFormsIntegration.dll */

using System.Windows.Forms.Integration;
using Microsoft.ReportingServices.Common;
using Microsoft.Reporting.WinForms;

    WindowsFormsHost host = new WindowsFormsHost();

    ReportViewer repViewer = new ReportViewer();
    repViewer.ProcessingMode = ProcessingMode.Local;
    repViewer.LocalReport.ReportPath = ReportPath + xx.rdlc"; // ReportPath is the folder path where xx.rdlc(your report designer file) resides

    repViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", _eventsTable.DefaultView)); // replace "DataSet1" with your dataset name embedded into the above report file (xx.rdlc)
    host.Child = repViewer;
    ReportArea.Children.Add(host);
    repViewer.RefreshReport();



Graphics - Circle Drawing


Call the Arc drawing function as bellow to make it a Circle.



TrigArc(int xOrg, int yOrg, int radius,0,360);


C# - Graphics - Distance Between 2 Points


/*********************************************************************
Description: C# implementation for calculating distance between 2 points (x1,y1) and (x2,y2).
*********************************************************************/



public int DistanceBetween2Points(int x1, int y1, int x2, int y2)
{
    return (int)Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}


Thursday, August 5, 2010

Windows Mobile - C# - Get Device ID


/*********************************************************************
Description: C# implementation of Windows Mobile to get DEVICE ID of the Smart Phone.
*********************************************************************/



using System.Runtime.InteropServices;

[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID(
    [In, Out] byte[] applicationdata,
    UInt32 cbApplictionData,
    UInt32 dwDeviceIDVersion,
    [In, Out] byte[] deviceIDOuput,
    [In, Out] uint pcbDeviceIDOutput);

// refer http://msdn.microsoft.com/en-us/library/aa908781.aspx for more details

private string GetDeviceID(string applicationName)
{
    byte[] applicationData = new byte[applicationName.Length];

    for (int i = 0; i < applicationName.Length; i++)
        applicationData[i] = (byte)applicationName[i];

    byte[] deviceIDOut = new byte[20]; // should be minimum of 20
    uint sizeID = 20; // should be minimum of 20

        GetDeviceUniqueID(applicationData, (UInt32)applicationData.Length, 1, deviceIDOut, sizeID);

        return BitConverter.ToString(deviceIDOut); // use this to return in the form of xx-xx-xx....
        // return BitConverter.ToString(deviceIDOut).Replace("-", ""); // use this to return in the form of xxxxxx....
}

// usage
string strDevID = GetDeviceID("Your Application Name");



C# - Stack


/*********************************************************************
Description: Stack class here is C# implementation.
*********************************************************************/



public class Stack
{
    private Node first = null;
    public bool Empty {
        get {
            return (first == null);
        }
    }
    public object Pop() {
        if (first == null)
            throw new Exception("Stack is empty.");
        else {
            object temp = first.Value;
            first = first.Next;
            return temp;
        }
    }
    public void Push(object o) {
            first = new Node(o, first);
        }
    class Node
    {
        public Node Next;
        public object Value;
        public Node(object value): this(value, null) {}
        public Node(object value, Node next) {
            Next = next;
            Value = value;
        }
    }
}

class Test
{
    static void Main() {
        Stack s = new Stack();
        for (int i = 0; i < 10; i++)
            s.Push(i);
        s = null;
    }
}


Tuesday, August 3, 2010

Graphics - Arc Drawing


/*********************************************************************
Description: This function draws an Arc pixel by pixel.
Parameters:
- int xOrg : x-cordinate of the origin
- int yOrg : y-cordinate of the origin
- int radius : Radius of the arc to draw
- int angleStart : Start angle of the arc (Anti Clock wise)
- int angleWidth : Width of the arc (Angle)

Language Specifics for DrawPixel(x, y,BLACK);
    Turbo/Ansi C - putpixel(x,y);
    C++_Win32 - ::SetPixel(hDC /*HDC*/, x, y, RGB(0,0,0)); // HDC hDC can be made as a parameter to the function
    VC++_MFC - pDC->SetPixel(x,y,RGB(0,0,0)); // CDC* pDC can be made as a parameter to the function

*********************************************************************/



#include "math.h" // for trignometric functions
#define PI (22.0 / 7.0)
void TrigArc(int xOrg, int yOrg, int radius,int angleStart,int angleWidth)
{
    for(int i = 0;i < angleWidth;i++)
    {
        int x = xOrg + radius * cos(((i + angleStart) * PI) / 180.0);
        int y = yOrg - radius * sin(((i + angleStart) * PI) / 180.0);
        DrawPixel(x, y,BLACK);
    }
}