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

时间:2010-10-04 22:24:04

标签: c++ winapi icons

我用windows api创建了一个基本的应用程序。它只显示一个小窗口。我从main函数开始,获取实例,创建我的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时,我疯了一个各种可能大小的图标。希望这有助于,并感谢耐心。

2 个答案:

答案 0 :(得分:1)

  • 您确定您的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;
}

答案 1 :(得分:1)

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

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

这应该可行,你应该看到红色的错误信息图标。

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

hInstance = GetModuleHandle(NULL);