How to set an icon to OpenCV UI window in Win32?

2,163 views
Skip to first unread message

Furqan Ullah

unread,
Aug 14, 2013, 2:55:09 AM8/14/13
to fltkg...@googlegroups.com
I want to set an icon to OpenCV image display window (top left corner icon). I tried and nothing is wrong with the code, but I do not know why icon does not appear. Below is my code.

        cvNamedWindow("Live Video", CV_WINDOW_AUTOSIZE);
        cvMoveWindow
("Live Video", 100, 100);
       
SleepInSeconds(2);

        HICON hIcon
= LoadIcon(fl_display, MAKEINTRESOURCE(IDI_ICON1));
       
if(hIcon)
       
{
            HWND camWindow
= (HWND)cvGetWindowHandle("Live Video");
           
if(camWindow)
           
{
               
SendMessage(camWindow, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
               
DestroyIcon( hIcon );
           
}
       
}

Kindly guide me what I need to do further in order to set an icon to the third party UI window such as OpenCV.
I am using Windows XP with Visual Studio 2010 C++.
Thanks.

Greg Ercolano

unread,
Aug 14, 2013, 3:07:52 AM8/14/13
to fltkg...@googlegroups.com
On 08/13/13 23:55, Furqan Ullah wrote:
> I want to set an icon.. (top left corner icon)

I don't know anything about opencv, but in fltk to change the titlebar icon:
http://seriss.com/people/erco/fltk/howto-windows-app-icon.html

If your app has multiple windows and you want different icons in each,
add more lines to the foo.rc file, each with a unique resource number (e.g. 101, 102..)
and refer to each icon by the unique number in your LoadIcon() command.

Furqan Ullah

unread,
Aug 14, 2013, 4:54:31 AM8/14/13
to fltkg...@googlegroups.com, erco_...@seriss.com
I added a new icon to resource which has a new IDI_ICON2 name in .rc file.

Now I am doing like this (below code).. but still no success. Am I using a right instance fl_display in order to set icon to OpenCV?

BOOL SetAnIconToOCV(const char* title)  
{
    HICON hIcon
= LoadIcon(fl_display, MAKEINTRESOURCE(IDI_ICON2));
   
if(hIcon)
   
{
        HWND win
= (HWND)cvGetWindowHandle(title);
       
if(win)
       
{
           
SendMessage(win, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
           
DestroyIcon( hIcon );
           
return TRUE;
       
}
   
}
   
return FALSE;
}

some
where in code..........
.
.

    cvNamedWindow
("live video", CV_WINDOW_AUTOSIZE);  // OpenCV window
   
SleepInSeconds(1);
   
if(SetAnIconToOCV("live video"))             // It always returns TRUE but does not set the icon.\
       
DisplayMessageBox("Icon has been set...");

Whats wrong now ?

MacArthur, Ian (Selex ES, UK)

unread,
Aug 14, 2013, 4:54:46 AM8/14/13
to fltkg...@googlegroups.com
Not sure, and I guess this is more a WIN32 question than a fltk one.

Your icon code looks a bit different from mine - I wrote the code I use a *long* time ago and don't really remember what all the bits do now; I just cut and paste it between apps and it Just Works...

Anyway, what I do first is create an icon with multiple sizes in it; that seems to make Windows happier, I find.

Then, before the window is shown (for fltk windows) I do:

// Set icon for window before we show it (MacOS uses app bundle for icon...)
#ifdef WIN32
main_window->icon((char *)LoadIcon(fl_display, MAKEINTRESOURCE(IDI_ICON)));
#endif // WIN32

main_window->show();



Then, once the window is shown, I add some extra icon sizes...


#ifdef WIN32
// The fltk icon code above only loads the default icon.
// Here, once the window is shown, we can assign
// additional icons, just to make things a bit nicer.
{
HANDLE bigicon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 32, 32, 0);
SendMessage(fl_xid(main_window), WM_SETICON, ICON_BIG, (LPARAM)bigicon);
HANDLE smallicon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 16, 16, 0);
SendMessage(fl_xid(main_window), WM_SETICON, ICON_SMALL, (LPARAM)smallicon);
}
#endif // !WIN32


I note that I am using LoadImage to get the icon images form the resource, and then sending those to the window; that seems to differ from what you are doing - might be worth a try, at any rate!

Hope that helps...





Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

Furqan Ullah

unread,
Aug 14, 2013, 10:53:02 AM8/14/13
to fltkg...@googlegroups.com, ian.ma...@selex-es.com
Solved: Thanks a lot for your kind words.

There is nothing wrong in both methods. LoadImage and LoadIcon both works fine. The main issue is, we can not perform such operations onto the third party UI window without "EnumWindows", such as Set an icon, disable close/maximum/minimum buttons, and BringToTop. When I used "EnumWindows", it works fine as expected.

Regards:
furqan
Reply all
Reply to author
Forward
0 new messages