自定义图标未显示在左上角或任务栏上

发布于 2024-09-26 05:59:20 字数 2457 浏览 3 评论 0原文

我已经使用 windows api 创建了一个基本应用程序。它只显示一个小窗口。我从主函数开始,获取实例,创建我的 Windows 类,等等。一切都很顺利。然而,我遇到的问题是我的自定义图标不会显示在窗口的左上角或任务栏上,它只显示窗口的默认小图片。然而,它确实显示为我实际可点击的 exe 文件的图标。我使用 resedit 来制作我的资源,并创建了所有 4 个图标尺寸,因此它应该有一个尺寸合适的可用图标。我得到了句柄,

HICON hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

然后使用 WNDCLASSEX 并将句柄提供给 hIcon 和 hIconsm。 如果有什么可能导致它不显示在角落或任务栏中,请提供帮助。

#include <Windows.h>
#include <iostream>
#include "resource.h"
//globals
    MSG msg;
    HWND hwndwnd;
    HICON hMyIcon;
//Windows Procedure
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
    switch ( message )
    {
    case WM_CLOSE:    
        exit( 0 );
        break;    
    case WM_CREATE:
        SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
        break;
    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}

int main(int ArgumentNum, char *arg[])
{
    //get instance
    char title[500];
    GetConsoleTitleA( title, 500 );
    HWND hwndConsole = FindWindowA( NULL, title );
    HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwndConsole, GWLP_HINSTANCE);
    //get icon handle
    hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    if (hMyIcon == NULL)
    {
         std::cout<< "NULL\n";
    }
    //create & register class
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_DROPSHADOW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = hMyIcon;
    wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = 0;
    wc.lpszClassName = "Jacob";
    wc.hIconSm = hMyIcon;
    RegisterClassEx(&wc);
    //create window
    hwndwnd = CreateWindow("Jacob", "My Window", 
    WS_OVERLAPPEDWINDOW, 520, 20, 300, 300, NULL, NULL, hInstance, NULL);
    //Tried sendmessage here as well
    //SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
    ShowWindow( hwndwnd, SW_SHOWNORMAL);
    UpdateWindow( hwndwnd );
    //hide console, not using to see if icon is null
    //ShowWindow( hwndConsole, 0 );
    //message loop
    while(GetMessage( &msg, hwndwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

这是我的源代码。我开始怀疑我的问题是否与我的资源有关。当我使用 resedit 时,我制作了各种可能大小的图标。希望这对您有所帮助,并感谢您的耐心等待。

I have created a basic application with with windows api. It just displays a small window. I am starting from main function, getting the instance, created my windows class, etc. Everything works out fine. The problem I have however is that my custom icon will not display in the top left corner of the window or on the task bar, it just shows the default little picture of a window. It does however show up as the icon for my the actual click-able exe file. I used resedit to make my resources, and created all 4 icon sizes so it should have one available of the proper size. I got the handle with

HICON hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

I then used WNDCLASSEX and gave the handle to both hIcon and hIconsm.
If there is anything that could cause it to not show up in the corner or task bar, please help.

#include <Windows.h>
#include <iostream>
#include "resource.h"
//globals
    MSG msg;
    HWND hwndwnd;
    HICON hMyIcon;
//Windows Procedure
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
    switch ( message )
    {
    case WM_CLOSE:    
        exit( 0 );
        break;    
    case WM_CREATE:
        SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
        break;
    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}

int main(int ArgumentNum, char *arg[])
{
    //get instance
    char title[500];
    GetConsoleTitleA( title, 500 );
    HWND hwndConsole = FindWindowA( NULL, title );
    HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwndConsole, GWLP_HINSTANCE);
    //get icon handle
    hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    if (hMyIcon == NULL)
    {
         std::cout<< "NULL\n";
    }
    //create & register class
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_DROPSHADOW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = hMyIcon;
    wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = 0;
    wc.lpszClassName = "Jacob";
    wc.hIconSm = hMyIcon;
    RegisterClassEx(&wc);
    //create window
    hwndwnd = CreateWindow("Jacob", "My Window", 
    WS_OVERLAPPEDWINDOW, 520, 20, 300, 300, NULL, NULL, hInstance, NULL);
    //Tried sendmessage here as well
    //SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
    ShowWindow( hwndwnd, SW_SHOWNORMAL);
    UpdateWindow( hwndwnd );
    //hide console, not using to see if icon is null
    //ShowWindow( hwndConsole, 0 );
    //message loop
    while(GetMessage( &msg, hwndwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

This is my source code. What im starting to wonder is if my problem has anything to do with my resources. When i used resedit i mad an icon of every possible size. Hope this Helps, and thanks for the patience.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-10-03 05:59:20

我的第一个建议是尝试加载标准图标而不是您自己的图标:

hMyIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ERROR));

这应该可行,并且您应该看到红色错误消息图标。

接下来要做的就是尝试以不同的方式获取实例句柄。控制台窗口是一个奇怪的品种,不要将它们与 Win32 API 的其余部分混合太多。尝试:

hInstance = GetModuleHandle(NULL);

My first suggestion would be to try loading a standard icon instead of your own icon:

hMyIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ERROR));

This should probably work, and you should see the red error message icon.

The next thing to do is try to obtain the instance handle in a different way. Console windows are a strange breed, don't mix them too much with the rest of the Win32 API. Try:

hInstance = GetModuleHandle(NULL);
像你 2024-10-03 05:59:20
  • 您确定您的 LoadIcon 调用返回 != NULL 吗?
  • LoadIcon总是加载一个32x32的图标,MSDN说如果hIconSm为NULL,它会检查图标资源中是否有正确大小的图标,所以也许你应该尝试设置hIconSm=NULL;
  • 您可以使用 WM_SETICON

编辑:

//(Having your code from the start would have made things easier)
#include <Windows.h>
#include "resource.h"
MSG msg;
HWND hwndwnd;
HICON hMyIcon;

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
    switch ( message )
    {
    case WM_CLOSE:    
        DestroyWindow(hwnd);//exit( 0 );
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
//        SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
        break;
    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}


int main(int ArgumentNum, char *arg[]) 
{
/*
    You don't own/control the console window, don't use it's HWND if you don't have to.
    ...And there is even a function to get the HWND if you need it, no need for FindWindow

    char title[500];
    GetConsoleTitleA( title, 500 );
    HWND hwndConsole = FindWindowA( NULL, title );
    HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwndConsole, GWLP_HINSTANCE);
    */
    HINSTANCE hInstance=GetModuleHandle(NULL);
    hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_DROPSHADOW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = hMyIcon;
    wc.hCursor = LoadCursor(/*hInstance*/NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = 0;
    wc.lpszClassName = "Jacob";
#if 1 //The easy way
    wc.hIconSm = NULL;//hMyIcon; LoadIcon only loads 32x32 icons, you would get the wrong icon
#else //The hard way
    wc.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0);
#endif
    RegisterClassEx(&wc);
    //create window
    hwndwnd = CreateWindow("Jacob", "My Window", 
    WS_OVERLAPPEDWINDOW, 520 , 20, 300, 300, NULL, NULL, hInstance, NULL);
    ShowWindow(hwndwnd,SW_SHOW);
    while(GetMessage( &msg, /*hwndwnd*/NULL, 0, 0) >0 ) //normally not a good idea to specify a hwnd
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
  • Are you sure your LoadIcon calls return != NULL?
  • LoadIcon always loads a 32x32 icon, MSDN says if hIconSm is NULL, it checks the icon resource for a icon with the correct size so maybe you should try to set hIconSm=NULL;
  • You could use WM_SETICON

Edit:

//(Having your code from the start would have made things easier)
#include <Windows.h>
#include "resource.h"
MSG msg;
HWND hwndwnd;
HICON hMyIcon;

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
    switch ( message )
    {
    case WM_CLOSE:    
        DestroyWindow(hwnd);//exit( 0 );
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
//        SendMessage(hwndwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);
        break;
    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}


int main(int ArgumentNum, char *arg[]) 
{
/*
    You don't own/control the console window, don't use it's HWND if you don't have to.
    ...And there is even a function to get the HWND if you need it, no need for FindWindow

    char title[500];
    GetConsoleTitleA( title, 500 );
    HWND hwndConsole = FindWindowA( NULL, title );
    HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwndConsole, GWLP_HINSTANCE);
    */
    HINSTANCE hInstance=GetModuleHandle(NULL);
    hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_DROPSHADOW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = hMyIcon;
    wc.hCursor = LoadCursor(/*hInstance*/NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = 0;
    wc.lpszClassName = "Jacob";
#if 1 //The easy way
    wc.hIconSm = NULL;//hMyIcon; LoadIcon only loads 32x32 icons, you would get the wrong icon
#else //The hard way
    wc.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0);
#endif
    RegisterClassEx(&wc);
    //create window
    hwndwnd = CreateWindow("Jacob", "My Window", 
    WS_OVERLAPPEDWINDOW, 520 , 20, 300, 300, NULL, NULL, hInstance, NULL);
    ShowWindow(hwndwnd,SW_SHOW);
    while(GetMessage( &msg, /*hwndwnd*/NULL, 0, 0) >0 ) //normally not a good idea to specify a hwnd
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文