0hc.net Menu
Home Overview
About Menu
Impressum

Icons

Let's give our windows nice icons: Haskell: module WindowIcon (getWindowHandles, loadIcon, putWindowIcon) where import Foreign import Foreign.Ptr import Foreign.Storable import Foreign.C.Types import Foreign.C.String import System.Win32.Types import System.IO.Unsafe hiding (unsafePerformIO) import qualified System.IO.Unsafe as IOU import Data.IORef import Control.Monad foreign import stdcall "windows.h GetCurrentProcessId" getCurrentProcessId :: IO DWORD foreign import stdcall "windows.h GetWindowThreadProcessId" getWindowThreadProcessId :: HANDLE -> LPDWORD -> IO DWORD foreign import stdcall "windows.h EnumWindows" enumWindows :: FunPtr (HANDLE -> LPARAM -> BOOL) -> LPARAM -> IO BOOL foreign import ccall "wrapper" wrapCB :: (HANDLE -> LPARAM -> IO BOOL) -> IO (FunPtr (HANDLE -> LPARAM -> BOOL)) foreign import stdcall "windows.h SendMessageA" sendMessage :: HANDLE -> UINT -> WPARAM -> HANDLE -> IO LRESULT foreign import stdcall "windows.h LoadImageA" loadImage :: HANDLE -> CString -> UINT -> CInt -> CInt -> UINT -> IO HANDLE globalAttrH :: IORef [HANDLE] {-# NOINLINE globalAttrH #-} globalAttrH = IOU.unsafePerformIO (newIORef []) globalAttrP :: IORef DWORD {-# NOINLINE globalAttrP #-} globalAttrP = IOU.unsafePerformIO (newIORef 0) enumWindowCallback :: HANDLE -> LPARAM -> IO BOOL enumWindowCallback h p = do fp <- mallocForeignPtr withForeignPtr fp (\pp-> do getWindowThreadProcessId h pp pd <- peek pp cpid <- readIORef globalAttrP when (fromIntegral cpid==pd) (modifyIORef globalAttrH (h:))) return True getWindowHandles = do cpid <- getCurrentProcessId writeIORef globalAttrP cpid ewcb <- wrapCB enumWindowCallback enumWindows ewcb (fromIntegral cpid) readIORef globalAttrH putWindowIcon ih wh = sendMessage wh 0x0080 1 ih loadIcon x = newCString x >>= \s-> loadImage nullPtr s 1 0 0 0x00000050 >>= \i-> free s >> return i Moonscript: ffi = require "ffi" ffi.cdef [[ uint32_t GetCurrentProcessId(void); uint32_t GetWindowThreadProcessId(void*, uint32_t*); typedef int (__stdcall *WNDENUMPROC)(void*, long); int EnumWindows(WNDENUMPROC, long); long SendMessageA(void*, unsigned int, long, void*); void* LoadImageA(void*, char*, unsigned int, int, int, unsigned int); ]] cpid = ffi.C.GetCurrentProcessId! wh={} cb = ffi.cast "WNDENUMPROC", (h, p) -> pp = ffi.new "uint32_t[1]" ffi.C.GetWindowThreadProcessId h, pp if cpid==pp[0] then table.insert wh, h return true ffi.C.EnumWindows cb, cpid cb\free! str = "my.ico" ico = ffi.C.LoadImageA nil, (ffi.new "char[?]", #str+1, str), 1, 0, 0, 0x50 for _,h in ipairs wh ffi.C.SendMessageA h, 0x80, 1, ico io.read!
0hc.net    © 2001-2014 Harald Wolfsgruber