Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Can't change icon with win32 module

by palkia (Scribe)
on Sep 21, 2012 at 17:03 UTC ( #994949=perlquestion: print w/ replies, xml ) Need Help??
palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone
I tried to change my perl window's icon by
use strict; use warnings; use diagnostics; use Win32::GUI(); $|=1; my $thisWindow = Win32::GUI::GetPerlWindow(); until($thisWindow) #doesn't always work at 1st { #print "Rechecking winID\n"; sleep 1; $thisWindow = Win32::GUI::GetPerlWindow(); #print 'winID = '.$thisWindow."\n"; } my $icon = new Win32::GUI::Icon('box.ico'); print "beofre\n"; if(1) #tried if 0 as well { Win32::GUI::ChangeIcon($thisWindow, $icon ); Win32::GUI::ChangeSmallIcon($thisWindow, $icon ); }else { $thisWindow->ChangeIcon($icon ); $thisWindow->ChangeSmallIcon($icon ); } print "after\n"; sleep 5; exit;
But nothing works.
in the if(0) version it says Can't call method "ChangeIcon" without a package or object referenc
and in the if(1) version, nothing happens (but the prints ^^).
What am I doing wrong ?
Could this have something to do with the fact I'm running in Padre ?
Thx

Comment on Can't change icon with win32 module
Select or Download Code
Re: Can't change icon with win32 module
by Anonymous Monk on Sep 21, 2012 at 22:14 UTC

    What am I doing wrong ?

    Well, you seem to be assuming that what you're trying to do is allowed/possible -- I don't think it is, but I've long ago given up on win32/msdn

      This is very encouraging ^^.
      What do you use instead of win32 ?
      I tired both wx and gtk2 but I got tired of trying to figure out why both won't work.
      If I remember correctly, it was some install problem or something.
      So if you got alternatives to offer, please exclude these 2.

        This is very encouraging

        Well, trying to change the icon on a console is kind of a weird thing to do :)

        I tired both wx and gtk2 but I got tired of trying to figure out why both won't work. If I remember correctly, it was some install problem or something. So if you got alternatives to offer, please exclude these 2.

        Yeah, I'd go with Wx, it comes with dwimperl/citrusperl, and is available via ppm for activeperl or any ppm capable perl

        FWIW, Gtk2 is available via some PPM::Repositories, but I prefer Wx

Re: Can't change icon with win32 module
by bulk88 (Priest) on Sep 22, 2012 at 04:02 UTC

      Win32::GUI object around a HWND

      You can fake it with bless, but it isn't required, every Win32::GUI method is also a function, it will take a hwnd as first argument

Re: Can't change icon with win32 module
by Anonymous Monk on Sep 22, 2012 at 07:11 UTC
    Its me again, some code for you
    #!/usr/bin/perl -- #~ wget -c -O perlmonks.favicon.ico http://perlmonks.org/favicon.ico use strict; use warnings; use Data::Dump; use Win32::GUI(); my $icon = 'Win32::GUI::Icon'->new( 'perlmonks.favicon.ico' ) or die $^E; my $cmd = Win32::GUI::GetPerlWindow() or die $^E; dd [ $cmd, $^E ]; dd [ Win32::GUI::ChangeIcon($cmd, $icon) , $^E ]; ## "Access is denied +" as expected BEGIN { use Win32::API; Win32::API::->Import("kernel32","BOOL SetConsoleIcon(HWND icon )") +; } $^E=0; dd [ SetConsoleIcon( $icon->{-handle} ), $^E ]; ## UNDOCUMENTED + MAGIC __END__ [46334276, ""] [0, "Access is denied"] [1, ""]
    SetConsoleIcon() naturally works where ChangeIcon() fails
      SetConsoleIcon is implemented as an IPC message/call to CSRSS.

        SetConsoleIcon is implemented as an IPC message/call to CSRSS.

        I figured it was something like that :) but how do you know, its undocumented?

      Won't seem to go online for me for some reason,
      but I just wanted it to use my icons anyway,
      so after some tweaking it worked gr8 (thank you very very much).
      In my version
      use Win32::GUI(); use Win32::API; Win32::API->Import('kernel32','BOOL SetConsoleIcon(HWND icon)'); SetConsoleIcon( Win32::GUI::Icon->new('box.ico')->{-handle} );
      For some reason it only works for some icons.
      It seems that Win32::GUI::Icon->new('box.ico') returns undef for some icons, or to be exact, it does the same as in the case I refer to a non-existing icon.
      Some icon-format limitation ?
        Tried checking Win32::GetLastError() if creating an Win32::GUI::Icon failed?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994949]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-10-23 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls