Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

c++ - Setting program icon without resources using the WIN32 API

I am working with the express version of Visual Studio. Therefore, using functions calls to MAKEINTRESOURCE are out of the question. I am tryting to set the application icon by overriding the getAdditionalClassInfo function.

WNDCLASSW *Robot::getAdditionalClassInfo(void) const {
    WNDCLASSW *wc = Window::getAdditionalClassInfo();
    HANDLE hIcon = LoadImage(NULL, L"imagepath/image.png", 32, 32, LR_LOADFROMFILE);

    wc->hIcon = .....;
    return wc;
}

Does anyone know how I can set this icon WITHOUT using a resource?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My suggestion, if you'd like to use PNGs, and be able to change the icon, is to use FreeImage to load it. Then you can use FreeImage to convert it to a standard HBITMAP fairly easily.

If you're fine with using an actual icon file, you can do the following once the window has been created:

HANDLE hIcon = LoadImage(0, _T("imagepath/image.ico"), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hIcon) {
    //Change both icons to the same icon handle.
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
    SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);

    //This will ensure that the application icon gets changed too.
    SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
    SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
}

You can likely call the similar function from within your getAdditionalClassInfo and setting it to the hIcon.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...