如何根据窗口句柄更改窗口图标?

grearo 2013-06-04 11:57:57
如何根据窗口句柄更改窗口图标?
...全文
253 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
grearo 2013-06-04
  • 打赏
  • 举报
回复
user32.dll 下的 LoadIcon LoadImage怎么用?
编程技术应用 2013-06-04
  • 打赏
  • 举报
回复
可能是 win32API 吧... 好像没有遇到过这类的api... 等大神回复...
编程技术应用 2013-06-04
  • 打赏
  • 举报
回复
[DllImport("user32.dll")]
static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType,
   int cxDesired, int cyDesired, uint fuLoad);
相关使用参考代码:
namespace ITLN.Utils.GUI {
    public static class AuxiliaryGUIIcon {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType,
            int cxDesired, int cyDesired, uint fuLoad);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int DestroyIcon(IntPtr hIcon);
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, LoadLibraryFlags dwFlags);
        private enum LoadLibraryFlags : uint {
            DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
            LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
            LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
            LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
            LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
            LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
        }
        /// <summary>
        /// Returns an icon of given size.
        /// </summary>
        /// <param name="path">Path to a file (.exe/.dll) that contains the icons.
        ///        Skip it or use <c>null</c> to use current application's file.</param>
        /// <param name="resId">Name of the resource icon that should be loaded.
        ///        Skip it to use the default <c>#32512</c> (value of <c>IDI_APPLICATION</c>) to use
        ///        the application's icon.</param>
        /// <param name="size">Size of the icon to load. If there is no such size available, a larger or smaller
        ///        sized-icon is scaled.</param>
        /// <returns>List of all icons.</returns>
        public static Icon GetIconFromExe(string path = null, string resId = "#32512", int size = 32) {
            // load module
            IntPtr h;
            if (path == null)
                h = Marshal.GetHINSTANCE(Assembly.GetEntryAssembly().GetModules()[0]);
            else {
                h = LoadLibraryEx(path, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE);
                if (h == IntPtr.Zero)
                    return null;
            }

            // 1 is IMAGE_ICON
            IntPtr ptr = LoadImage(h, resId, 1, size, size, 0);
            if (ptr != IntPtr.Zero) {
                try {
                    Icon icon = (Icon)Icon.FromHandle(ptr).Clone();
                    return icon;
                } finally {
                    DestroyIcon(ptr);
                }
            }
            return null;
        }
    }
}
打开链接下载源码: https://pan.quark.cn/s/6917f48f5ad5 易语言是一种为中国人量身定制的、操作简便的编程语言,其宗旨在于使不熟悉英文的普通用户也能参与程序的开发工作。 在易语言环境中,用户可以便捷地完成对操作系统窗口的各类操作,例如获取窗口句柄以及调整窗口的标题和图标,这些均属于Windows API调用的核心内容。 窗口句柄在Windows程序设计领域占据着举足轻重的地位,它是由系统分配给每个窗口的一个独特标识,借助该标识,我们能够对特定的窗口执行操作。 在易语言中,获取窗口句柄通常需要借助`窗口句柄.取`这一指令。 举例来说,倘若已知窗口的标题,便可以使用`窗口句柄.取(窗口标题)`这一方式来获取相应窗口句柄。 这一流程涉及到易语言的API调用机制,需要将Windows API函数转化为易语言的内置指令或用户自定义指令。 调整窗口标题是通过设定窗口属性来实现的,易语言提供了`窗口属性.设置`指令来完成此功能。 譬如,`窗口属性.设置(窗口句柄, 窗口属性类型.标题, 新标题)`这行指令能够将拥有指定句柄窗口标题更改为“新标题”。 在此处,`窗口属性类型.标题`意味着我们即将调整的是窗口的标题属性。 至于更改窗口图标,Windows API提供了`LoadIcon`以及`SetClassLong`或`SetWindowLong`等函数来达成,但在易语言环境下,可能需要创建自定义指令或模块来封装这些功能。 你需要载入图标资源(一般是以.ico为后缀的文件),然后运用`LoadIcon`函数获取图标句柄,随后通过`SetClassLong`或`SetWindowLong`来替换窗口类的图标属性。 这一流程关联到资源管理、图标载入以及对窗口类或实例属性的变...

111,127

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧