Skip to main content
Email Password   helpLost your password?

Introduction

The tool can be used as a short cut for launching applications using mouse.
There aren't many technical things to tell in this article. Most of the things are available on the Internet and I searched and used them. I will give the links in this article.

The techniques used are:

  1. Mouse hook
  2. How to get icons from EXE

Background

Thanks to Stephen Toub for the useful article on how to hook the mouse in C#.

How to Use

Using the Code

Mouse Hook

Please follow the above article for the mouse hook process.

The main window has two delegates that will be called by the mouse hook methods.
One for show and hide the window. ShowHide(int x, int y) will show or hide the window in mouse middle click.

The second one is for scrolling the window. ScrollIcon() will increase the page number of the window.

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0 &&
         MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
    {
         //MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)
         //Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
         //Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);
         showhide(0,0);
    }
    else if (nCode >= 0 &&
         MouseMessages.WM_MBUTTONDOWN == (MouseMessages)wParam)
    {
         MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)
	Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
         showhide(hookStruct.pt.x, hookStruct.pt.y);
         return (IntPtr)(-1);
    }
    else if (nCode >= 0 &&
         MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
    {
         if (scrolIcon() == true)
         {
             return (IntPtr)(-1);
         }
    }
    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}		

Transparent Window

The second part to consider in the tool is a transparent window containing a matrix of buttons.To make a transparent window in WPF is very simple. Below is the XAML code. Make AllowsTransparency = true and Background='Transparent".

<Window x:Class="ZWheel.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="180" Width="180"  WindowStyle="None" 
		AllowsTransparency="True"
    Background="Transparent" Visibility="Hidden" 
	Icon="/ZWheel;component/ZWheel.ico" Topmost="False" ShowInTaskbar="False">

Get Icons from Filename

The following code is used to get the icons from the file name:

[DllImport("Shell32.dll")]
public extern static int ExtractIconEx(string libName, int iconIndex,
IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);

public static Icon GetIconFromPath(string filePath)
{
    int numIcons = 10;//if you want 10 icons for example

    IntPtr[] largeIcon = new IntPtr[numIcons];
    IntPtr[] smallIcon = new IntPtr[numIcons];

    //retrieve icon from array
    Icon smallIco = null;

    try
    {
        if (Directory.Exists(filePath) == true)
        {
             ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, numIcons);
             smallIco = System.Drawing.Icon.FromHandle(smallIcon[4]);
        }
        else
        {
             ExtractIconEx(filePath, 0, largeIcon, smallIcon, numIcons);
             smallIco = System.Drawing.Icon.FromHandle(smallIcon[0]);
        }
    }
    catch (Exception)
    { }

    return (smallIco);
} 

Put the Icon in the Button

To put the icon on the button, declare each button with an image as below. Get the System.Drawing.icon for the file using the above code. Create a bitmap from the icon and assign the image.Source property as shown in the code below:

 <Button Name="P1_Btn00" Grid.Column="0" Grid.Row="0" Click="Btn_Click">
        <Image Name="IC_Btn00" Stretch="None"  />
 </Button> 
btnIcon = GetIcon.GetIconFromPath(btnInfo.Path);

Bitmap bmp = btnIcon.ToBitmap();
MemoryStream strm = new MemoryStream();
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
strm.Seek(0, SeekOrigin.Begin);
PngBitmapDecoder pbd = new PngBitmapDecoder
	(strm, BitmapCreateOptions.None, BitmapCacheOption.Default);
btnImage.Source = pbd.Frames[0];

Improvements

I know there is nothing much in the code. I put it just to get some comments on the tool for its improvement. I would also like some ideas to use it in Notebook.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDrag and drop Pin
oned2
9:55 31 Dec '08  
GeneralRe: Drag and drop Pin
_anil_
3:19 1 Jan '09  
GeneralRe: Drag and drop Pin
_anil_
0:24 6 Jan '09  
GeneralVersion 1.0.0.1 Release Pin
_anil_
22:13 23 Dec '08  
General[Message Deleted] Pin
georgejh
3:04 23 Dec '08  
GeneralRe: This methodology is under copyright sisnce 1998 Pin
_anil_
3:42 23 Dec '08  
General[Message Deleted] Pin
georgejh
3:50 23 Dec '08  
GeneralRe: This methodology is under copyright sisnce 1998 Pin
_anil_
3:55 23 Dec '08  
General[Message Deleted] Pin
georgejh
4:11 23 Dec '08  
General[Message Deleted] Pin
georgejh
4:13 23 Dec '08  
GeneralRe: This methodology is under copyright sisnce 1998 Pin
_anil_
4:53 23 Dec '08  
GeneralRe: This methodology is under copyright sisnce 1998 Pin
Alex Kvachev
6:36 23 Dec '08  
GeneralVote 3 Pin
johannesnestler
23:29 22 Dec '08  
GeneralRe: Vote 3 [modified] Pin
_anil_
3:37 23 Dec '08  
GeneralRe: Vote 3 Pin
_anil_
22:15 23 Dec '08  


Last Updated 5 Jan 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009