65.9K
CodeProject 正在变化。 阅读更多。
Home

存储和组织您的快捷方式,以便轻松启动文件和应用程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.80/5 (8投票s)

2008 年 12 月 22 日

CPOL

3分钟阅读

viewsIcon

60391

downloadIcon

565

存储和组织您的快捷方式,以便轻松启动文件和应用程序。

引言

此工具可用于通过鼠标启动应用程序的快捷方式。
本文中没有太多技术细节需要说明。大部分内容都可以在互联网上找到,我已经搜索并使用了它们。我会在本文中提供链接。

使用的技术包括:

  1. 鼠标钩子
  2. 如何从EXE获取图标

背景

感谢Stephen Toub撰写的关于如何在C#中钩住鼠标的有用文章。

如何使用

  • 启动应用程序后,应用程序将在后台运行。要查看工具:

    1. 使用鼠标滚轮单击。
    2. 或者双击托盘图标。(适用于没有滚轮的鼠标)
    3. 或者右键单击托盘图标,然后从菜单中选择“显示”。

    Help_new.JPG

  • 屏幕上将出现一个4x4的矩阵按钮。现在设置每个按钮以启动新应用程序。为此:

    1. 右键单击托盘图标以查看菜单。
    2. 在菜单上,选择“设置”。将打开以下对话框。

      Setting.JPG

    3. “行”和“列”将更改显示的按钮数量。
    4. 单击每个按钮以提供应用程序路径和任何参数。
    5. 如果要在启动时运行应用程序,请选中启动复选框。
    6. 共有3页,每页包含36个按钮。每个按钮都可以配置为应用程序的快捷方式,也可以设置为空白。
  • 矩阵按钮可见后,单击屏幕上的任何位置都将隐藏它。中间单击也将隐藏矩阵按钮。

    • 通过**旋转鼠标滚轮**,我们可以移动到下一页按钮。
    • **右键单击按钮**也会移动到下一页。(这是为没有滚轮的鼠标设计的)
    • 单击按钮以启动每个应用程序。

Using the Code

鼠标钩子

请遵循上述文章中的鼠标钩子过程。

主窗口有两个委托,将由鼠标钩子方法调用。
一个用于显示和隐藏窗口。ShowHide(int x, int y)将在鼠标中间单击时显示或隐藏窗口。

第二个用于滚动窗口。ScrollIcon()将增加窗口的页码。

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);
}		

透明窗口

该工具的第二个部分是一个包含按钮矩阵的透明窗口。在WPF中创建透明窗口非常简单。以下是XAML代码。将AllowsTransparency = trueBackground='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">

从文件名获取图标

以下代码用于从文件名获取图标

[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);
} 

将图标放在按钮上

要在按钮上放置图标,请像下面一样声明每个带有图像的按钮。使用上面的代码获取文件的System.Drawing.icon。从图标创建位图,并将image.Source属性赋值,如下面的代码所示。

 <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];

改进

我知道代码中没有什么太多内容。我只是为了获得一些关于工具改进的评论而添加它。我还想得到一些在笔记本电脑上使用它的想法。

历史

  • 2008年12月22日:初始发布
  • 2008年12月29日:更新了新的EXE版本 (1.0.0.2)
© . All rights reserved.