CreateGrayscaleIcon and setSubFieldsEasy

new topic     » topic index » view thread      » older message » newer message

Here are two very useful functions:
First is to set statusbar parts easy.
Second is to make grayscale icon from colored icon.
I have one question: when I disable button on flattoolbar with setEnable()
it doesnt get disabled look, with that gray color. How do I do that?



First setSubFieldsEasy:
------------------------------------------------------------------------

--/*
-- setSubFieldsEasy [Created on 26. July 2002, 18:25]
-- The 'setSubFieldsEasy' procedure set win32lib
-- statusbar fields widths.
-- You give it strings which you expect to be in
-- each statusbar part and it will caclulate and set
-- parts widths for you.
--
-- PARAMETERS
-- 'statusbar'
--    Win32lib id of statusbar.
-- 'texts'
--    Sequence with strings, expected strings in statusbar parts.
--*/
global procedure setSubFieldsEasy (integer statusbar, sequence texts)
    sequence arg_seq
    integer prev_wid_sum, str_wid
    arg_seq = repeat (0, length (texts + 1))
    prev_wid_sum = 0
    for i = 1 to length (texts) do
        str_wid = getTextWidth (statusbar, texts [i] & ",,")
        arg_seq [i] = prev_wid_sum + str_wid
        prev_wid_sum += str_wid
    end for
    arg_seq [length (arg_seq)] = -1
    setSubFields (statusbar, arg_seq)
end procedure




Now CreateGrayscaleIcon:
----------------------------------------------------------------------------
-


global constant xGetIconInfo = registerw32Function (user32, "GetIconInfo",
{C_LONG, C_POINTER}, C_INT)
global constant xCreateIconIndirect = registerw32Function (user32,
"CreateIconIndirect", {C_POINTER}, C_LONG)

--// ICONINFO struct
    ICONINFO_fIcon    = allot (DWord),
    ICONINFO_xHotspot = allot (DWord),
    ICONINFO_yHotspot = allot (DWord),
    ICONINFO_hbmMask  = allot (Long),
    ICONINFO_hbmColor = allot (Long),
    SIZEOF_ICONINFO   = allotted_size (),

--// Equal to C's function.
global function GetRValue (atom rgbval)
    return remainder (rgbval, 256)
end function

--// Equal to C's function.
global function GetGValue (atom rgbval)
    --// return shift_right (rgbval, 8)
    return floor (remainder (rgbval, 256 * 256) / 256)
end function

--// Equal to C's function.
global function GetBValue (atom rgbval)
    return floor (rgbval / (256 * 256))
end function

global function RGB( atom r, atom g, atom b )

    -- return RGB value of triad
    return r + ( g * 256 ) + ( b * 256 * 256 )

end function


--/*
-- CreateGrayscaleIcon [Created on 31. July 2002, 15:59]
-- The 'CreateGrayscaleIcon' function
-- creates a grayscale icon starting
-- from a given icon. The resulting icon will have
-- the same size of the original one.
--
-- By Davide Calabro, I translated it from C++.
--
-- PARAMETERS
-- 'hIcon'
--    Handle to the original icon.
--
-- RETURN VALUES
-- If the function succeeds, the return value is
-- the handle to the newly created grayscale icon.
-- If the function fails, the return value is NULL.
--*/
global function CreateGrayscaleIcon (atom hIcon)
    atom hGrayIcon
    atom hMainDC
    atom hMemDC1
    atom hMemDC2
    atom bmp
    atom hOldBmp1
    atom hOldBmp2
    atom csII, csGrayII
    integer bRetValue
    atom hWnd
    atom crPixel
    atom byNewPixel
    integer maxx, maxy

    hGrayIcon = NULL
    hMainDC = NULL
    hMemDC1 = NULL
    hMemDC2 = NULL
    hOldBmp1 = NULL
    hOldBmp2 = NULL
    bRetValue = FALSE
    hWnd = 0 --// getHandle (window)
    hWnd = 0
    crPixel = 0
    byNewPixel = 0

    csII = acquire_mem (0, SIZEOF_ICONINFO)
    csGrayII = acquire_mem (0, SIZEOF_ICONINFO)
    bRetValue = w32Func (xGetIconInfo, {hIcon, csII})
    if (bRetValue = FALSE) then
        return NULL
    end if

    hMainDC = w32Func (xGetDC, {hWnd})
    hMemDC1 = w32Func (xCreateCompatibleDC, {hMainDC})
    hMemDC2 = w32Func (xCreateCompatibleDC, {hMainDC})
    if (hMainDC = NULL or
        hMemDC1 = NULL or
        hMemDC2 = NULL) then
        return NULL
    end if

    bmp = acquire_mem (0, SIZEOF_BITMAP)
    if (w32Func (xGetObject, {tk_mem:fetch (csII, ICONINFO_hbmColor),
                SIZEOF_BITMAP, bmp})) then

        store (csGrayII, ICONINFO_hbmColor,
             w32Func (xCreateBitmap, {tk_mem:fetch (csII,
ICONINFO_xHotspot)*2,
                            tk_mem:fetch (csII, ICONINFO_yHotspot)*2,
                            tk_mem:fetch (bmp, bmPlanes),
                            tk_mem:fetch (bmp, bmBitsPixel),
                            NULL}))
        if (tk_mem:fetch (csGrayII, ICONINFO_hbmColor)) then

          hOldBmp1 = w32Func (xSelectObject, {hMemDC1,
                                     tk_mem:fetch (csII,ICONINFO_hbmColor)})
          hOldBmp2 = w32Func (xSelectObject, {hMemDC2,
                                     tk_mem:fetch (csGrayII,
ICONINFO_hbmColor)})

          Void = w32Func (xBitBlt, {hMemDC2, 0, 0, tk_mem:fetch (csII,
ICONINFO_xHotspot)*2,
                   tk_mem:fetch (csII, ICONINFO_yHotspot)*2, hMemDC1, 0, 0,
                   SRCCOPY})

          maxy = tk_mem:fetch (csII, ICONINFO_yHotspot)*2
          maxx = tk_mem:fetch (csII, ICONINFO_xHotspot)*2
          for dwLoopY = 0 to maxy do
            for dwLoopX = 0 to maxx do
              crPixel = w32Func (xGetPixel,{hMemDC2, dwLoopX, dwLoopY})

                byNewPixel = floor (((GetRValue(crPixel) * 0.299) +
                   (GetGValue(crPixel) * 0.587) +
                   (GetBValue(crPixel) * 0.114)))
                --// byNewPixel = floor ((((GetRValue(crPixel)) +
                   --// (GetGValue(crPixel)) +
                   --// (GetBValue(crPixel)))) / 3)
              if (crPixel) then
                Void = w32Func (xSetPixel, {hMemDC2,
                                      dwLoopX,
                                      dwLoopY,
                                      RGB(byNewPixel,
                                      byNewPixel,
                                      byNewPixel)})
              end if
            end for
          end for

          Void = w32Func (xSelectObject, {hMemDC1, hOldBmp1})
          Void = w32Func (xSelectObject, {hMemDC2, hOldBmp2})

          store (csGrayII, ICONINFO_hbmMask, tk_mem:fetch (csII,
ICONINFO_hbmMask))
          store (csGrayII, ICONINFO_fIcon, TRUE)
          hGrayIcon = w32Func (xCreateIconIndirect, {csGrayII})
        end if

        Void = w32Func (xDeleteObject, {tk_mem:fetch (csGrayII,
ICONINFO_hbmColor)})
        --////::DeleteObject(csGrayII.hbmMask)
    end if

    Void = w32Func (xDeleteObject, {tk_mem:fetch (csII, ICONINFO_hbmColor)})
    Void = w32Func (xDeleteObject, {tk_mem:fetch (csII, ICONINFO_hbmMask)})
    Void = w32Func (xDeleteDC, {hMemDC1})
    Void = w32Func (xDeleteDC, {hMemDC2})
    Void = w32Func (xReleaseDC, {hWnd, hMainDC})

    release_mem (csII)
    release_mem (csGrayII)
    release_mem (bmp)
    return hGrayIcon
end function

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu