BASIC

Author Topic: Set A Window Icon, internal coded icon image.  (Read 3973 times)

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1635
  • Particle Hawk
    • View Profile
This function will build an icon by using internal coding instead of a prebuilt image.
This is good if you dont want users altering the icon.ico with thier own modifications.

Important: Call Icon Function at the top of your code, once, before the main loop .
To change the image design and colors:
Alter the icon image in the function below where sprite$[] is declared.
Alter  Icon_Center_Color and Icon_Edge_Color to change background colors.

Code: GLBasic [Select]
FUNCTION InitializeWindowIcon:
        //- Build A New Window Icon Sprite, with colors and Background!
        //- Created By Hemlos@GLBasic.com
        //- 2d Window Icon Builder Created By Hemlos@GLBasic.com
        //- Additional Credits:
        //- Thank you (Schranz0r) for the INLINE C++ Window Icon Draw
        // Place Function call before main loop!

//Format: InitializeWindowIcon()

//Background Colors: (Edit Center and Edge color)
LOCAL Icon_Center_Color=RGB(255,0,0)  // red (change this is desired)
LOCAL Icon_Edge_Color=RGB(40,40,40)   // grey (change this is desired)
LOCAL ICON$="icon.ico"                        //temporary icon name(dont need to change this)
DIM SPRITE$ [16]

//Preset Foreground Colors: (Edit Icon Image With Color Presets)
// 0=Black W=White R=Red G=Green B=Blue C=Cyan Y=Yellow P=Purple

SPRITE$[0] ="................"
SPRITE$[1] =".......CC......."
SPRITE$[2] ="......CCCC......"
SPRITE$[3] =".....CCCCCC....."
SPRITE$[4] =".....CCCCCC....."
SPRITE$[5] =".....CCCCCC....."
SPRITE$[6] ="......CCCC......"
SPRITE$[7] =".......CC......."
SPRITE$[8] =".......CC......."
SPRITE$[9] =".......CC......."
SPRITE$[10]="................"
SPRITE$[11]=".......CC......."
SPRITE$[12]="......CCCC......"
SPRITE$[13]=".......CC......."
SPRITE$[14]="................"
SPRITE$[15]="................"

        //Build Icon Background -Nothing to edit
        ALPHAMODE -1
        STARTPOLY -1
        POLYVECTOR 7, 7, -1, -1,                Icon_Center_Color
        POLYVECTOR  0,   0,  -1,  -1,   Icon_Edge_Color
        POLYVECTOR   0, 15,  -1, -1,    Icon_Edge_Color
        ENDPOLY
        STARTPOLY -1
        POLYVECTOR  7,   7,  0,  0,     Icon_Center_Color
        POLYVECTOR   0, 15,  0, 0,              Icon_Edge_Color
        POLYVECTOR 15, 15, 0, 0,                Icon_Edge_Color
        ENDPOLY
        STARTPOLY -1
        POLYVECTOR  7,   7,  0,  0,     Icon_Center_Color
        POLYVECTOR   15, 15,  0, 0,     Icon_Edge_Color
        POLYVECTOR 15, 0, 0, 0,                 Icon_Edge_Color
        ENDPOLY
        STARTPOLY -1
        POLYVECTOR  7,   7,  0,  0,     Icon_Center_Color
        POLYVECTOR   15, 0,  0, 0,              Icon_Edge_Color
        POLYVECTOR 0, 0, 0, 0,                  Icon_Edge_Color
        ENDPOLY

        //Build Icon Foreground -You can add more colors in this loop
        LOCAL SPRITECOL, SPRITEROW, PixelColor, Pixel$
        ALPHAMODE 1
        FOR SPRITEROW=0 TO 15
                FOR SPRITECOL=0 TO 15
                        Pixel$=MID$(SPRITE$[SPRITEROW],SPRITECOL,1)
                        IF Pixel$="."; PixelColor=RGB(0,0,0);   ENDIF//black
                        IF Pixel$="W"; PixelColor=RGB(255,255,255); ENDIF//white
                        IF Pixel$="R"; PixelColor=RGB(255,0,0);         ENDIF//red
                        IF Pixel$="G"; PixelColor=RGB(0,255,0);         ENDIF//green
                        IF Pixel$="B"; PixelColor=RGB(0,0,255);         ENDIF//blue
                        IF Pixel$="Y"; PixelColor=RGB(255,255,0);       ENDIF//yellow
                        IF Pixel$="P"; PixelColor=RGB(255,0,255);       ENDIF//Purple
                        IF Pixel$="C"; PixelColor=RGB(0,255,255);       ENDIF//Cyan
                        //Add more Colors here, use lines above for example.
                        SETPIXEL SPRITECOL, SPRITEROW, PixelColor
                NEXT
        NEXT

        //Build and Save Window Icon -Nothing to edit
        GRABSPRITE 0, 0,0, 16,16
        SAVESPRITE ICON$,0
        BLACKSCREEN

        //Draw window icon: -Inline Code below, written by Schranz0r -Nothing to edit
        LOCAL hwnd,icon
        INLINE
        DECLARE_C_ALIAS(u32_GetActiveWindow,"user32.dll", "GetActiveWindow",(),int);
        DECLARE_C_ALIAS(u32_SetClassLong,"user32.dll", "SetClassLongA",(int,int,int), void);
        DECLARE_C_ALIAS(shell32_LoadIcon,"Shell32.dll", "ExtractIconA",(int,const char*,int), int);
        hwnd = u32_GetActiveWindow();
        icon = shell32_LoadIcon(hwnd,ICON_Str.c_str(),0);
        u32_SetClassLong(hwnd,-14,icon);
        ENDINLINE

ENDFUNCTION
« Last Edit: 2008-Sep-15 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10720
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Set A Window Icon, internal coded icon image.
« Reply #1 on: 2008-Sep-17 »
tricky idea. Dynamic icon creation.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1635
  • Particle Hawk
    • View Profile
Re: Set A Window Icon, internal coded icon image.
« Reply #2 on: 2008-Sep-17 »
tricky idea. Dynamic icon creation.

It was tricky writing that function, but implementation is easy as 123..

Just add the function to your function list.
Edit colors and pixels to suit.
Then put a call to it at beginning of program.

The default icon is a cyan exclamation point with a red and black glowing background.

I made it animated (moving the pixeled icon over the bacckground color) but it causes a memory leak using that inline code, any ideas?
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10720
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Set A Window Icon, internal coded icon image.
« Reply #3 on: 2008-Sep-17 »
call DestroyIcon API before re-creating it.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1635
  • Particle Hawk
    • View Profile
Re: Set A Window Icon, internal coded icon image.
« Reply #4 on: 2008-Sep-20 »
ah cool will try that one day
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)