Silverlight, WPF – ListBox Drag and Drop

During my development I need to implement the drag and drop feature in Silverlight. It’s pretty easy to make this feature work in Windows Form but it’s not simple to enable this feature on control in Silverlight. Therefore the developer team of Silverlight has developed a Silverlight toolkit to help us to alleviate our work. The Silverlight Toolkit is a collection of Silverlight controls, components and utilities made available outside the normal Silverlight release cycle. For more details about this toolkit you can read here
In this example I would like to illustrate how we can use this toolkit to adapt the drag and drop to list box component of Silverlight. First you need to browse to URL above and install Silverlight Toolkit, then use Microsoft Expression Blend to add two list boxes to our application

Continue reading Silverlight, WPF – ListBox Drag and Drop

C# – Send keys spacebar to windows

When I wrote a bot for game online to buff blood and mana when I let my character auto play. To buff blood and mana, we can put blood and mana item into shortcut key and press this short key to use item. Therefore we can come to the idea that we can make an auto buff blood and mana tool. The main idea consists of these steps:
– Read information of blood and mana from memory of process game.
– If value is smaller than a threshold, then send short-key, which is relevant to blood and mana items, to the game windows
To read information of character you can use Cheat Engine to locate the memory address and read info out and it’s not the theme of this blog. Maybe I will discuss this theme later in another blog. In this blog, I would like to discuss only about how we send keys to game. Normally, we can use SendKeys.Send command but for some games, this command does not work. In this example, I would like to use PostMessage API to send key to window.
First, we list all opened windows, of course, including our game window and save the list into combo box.

private void RefreshActiveWindows()
	List<string> lstActiveWnd = ModelData.ListAllActiveWindows(this.Handle);
	foreach (string strTemp in lstActiveWnd)

public static List<string> ListAllActiveWindows(IntPtr ipMainHandle)
	List<string> lstReturn = new List<string>();
	IntPtr ipDeskWndHandle = NativeWin32.GetDesktopWindow();
	IntPtr ipChildHandle = NativeWin32.GetWindow(ipDeskWndHandle, (uint)NativeWin32.GetWindow_Cmd.GW_CHILD);
	while (ipChildHandle != IntPtr.Zero)
		if (ipChildHandle == ipMainHandle)
			ipChildHandle = NativeWin32.GetWindow(ipChildHandle, (uint)NativeWin32.GetWindow_Cmd.GW_HWNDNEXT);

		if (NativeWin32.IsWindowVisible(ipChildHandle))
			StringBuilder sbTitle = new StringBuilder(1024);
			NativeWin32.GetWindowText(ipChildHandle, sbTitle, sbTitle.Capacity);
			String sWinTitle = sbTitle.ToString();
			if (sWinTitle.Length > 0)
		ipChildHandle = NativeWin32.GetWindow(ipChildHandle, (uint)NativeWin32.GetWindow_Cmd.GW_HWNDNEXT);
	return lstReturn;

After the user chooses the game window through combo box, click Start button, we should get the handle of game window and send key to it through Timer component. In this example I will send a space bar key continuously.

private void btnBrowse_Click(object sender, EventArgs e)
	if (btnBrowse.Text == "Start")
		if (cbWindowsTitle.Text == "")
			ErrorManager.ShowError("Please choose the process of game");
			m_ipHandle = NativeWin32.FindWindow(null, cbWindowsTitle.Text);
			m_mdData = new ModelData(m_ipHandle);
		btnBrowse.Text = "Click x to stop";
		btnBrowse.Text = "Start";

private void tmTimer_Tick(object sender, EventArgs e)

The core function for sending key which uses PostMessage is in code snippet below

[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

Keys keySpace;
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
public void BuffPet()
	keySpace = Keys.Space;
	PostMessage(m_ipHandle, WM_KEYDOWN, (uint)keySpace, 0);
	PostMessage(m_ipHandle, WM_KEYUP, (uint)keySpace, 0);

The source code of class NativeWin32 you can see here “NativeWin32.cs”

Silverlight – Routed Event

The routed event was first introduced in WPF. It allows us to handle the event through a tree of elements in WPF application. The routed event can be understood as functional or implementation definition.
– Functional definition: A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.
– Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.
A typical WPF application contains many elements. Whether created in code or declared in XAML, these elements exist in an element tree relationship to each other. The event route can travel in one of two directions depending on the event definition.

Continue reading Silverlight – Routed Event

C# – Convert Word to HTML

During my development I need to convert Word to HTML. I have installed Word on my local machine and I believe there are already many available ways on Internet to convert Word to HTML. In this small example I would like to introduce a method without adding any reference to any COM Component. This method uses namespace System.Reflection to create an instance of word document and invoke functionality “Export to HTML” for converting. The following functions will be used
Type.GetTypeFromProgID Method: Gets the type associated with the specified program identifier (ProgID).
Activator.CreateInstance Method: Creates an instance of the specified type using the constructor that best matches the specified parameters.
Type.InvokeMember Method: When overridden in a derived class, invokes the specified member, using the specified binding constraints and matching the specified argument list, modifiers and culture.

And the complete source code of example is in following snippet

static void Main(string[] args)
            ConvertWord2Html(@"E:\temp.doc", @"E:\temp.html");
            Console.WriteLine("Convert done");

        static void ConvertWord2Html(string strSource, string strDestination)
            // Constant for WORD-TO-HTML exporting format
            const int WORD_HTML_FORMAT = 8;

            // Load COM-Metadata of Word application from registry
            Type tWordApplication = Type.GetTypeFromProgID("Word.Application");

            // Create new instance of Word
            object objWord = Activator.CreateInstance(tWordApplication);

            // List all documents
            object objDocuments = tWordApplication.InvokeMember("Documents", BindingFlags.IgnoreCase | BindingFlags.GetProperty | BindingFlags.Public, null, objWord, new object[0]);

            // Get COM-Metadata of Word Documents
            Type tWordDocuments = objDocuments.GetType();

            // Load source
            object objDocument = tWordDocuments.InvokeMember("Open", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objDocuments, new object[1] { strSource });

            // Get COM-Metadata of Word Documents
            Type tWordDocument = objDocument.GetType();

            // Create HTML 
            tWordDocument.InvokeMember("SaveAs", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objDocument, new object[2] { strDestination, WORD_HTML_FORMAT });

            // Close Word
            tWordApplication.InvokeMember("Quit", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding, null, objWord, new object[0]);

Although this method is very comfortable, it doesn’t not allow us to monitor how long the converting still runs so that we can notify the user and allow him to cancel if it lasts too long, for example with very large Word document.

Tết Canh Dần 2010 – Happy Lunar New Year

Now it’s time to say “Happy New Year” again. Maybe some of you will be surprised when reading this post. How can a new year come in February but it’s true. Today is a new year day according to lunar calender and it’s really for me a new year. No matter where I am, in this holy time, I always missed my family very much and wished that I could be home to wish all members of my family a lucky, happy new year. If I am not wrong with time calculation, this post will be published exactly at 0:0:0 of the first lunar new year day.
So the year of tiger is coming, I wish all of you with Asian style:
– abundant health (dồi dào sức khỏe)
– prosperity (thịnh vượng)
– may a myriad things go according to your will (vạn sự như ý)
– to be successful in all aspects (thành công trong mọi mặt)
– to have a prosperous business (làm ăn phát tài)
– live up to 100 years (sống lâu trăm tuổi)
– to make good progress in school (học hành tấn tới)
– May money flow in like water (tiền vô như nước)

and a traditional song for new year too

If you want to understand more about this Tet holiday, you can read article at wikipedia