winapi - Changing taskbar icon programatically (Win32,C++) -


this question has answer here:

i have c++ win32 program, , i'd edit taskbar icon @ runtime display alerts, etc program, i'm not experienced win32 api, , haven't been able find online. closest i've found http://www.windows-tech.info/17/52a5bfc45dac0ade.php tells how load icon off disc @ runtime , change it.

i in question: create icon in memory win32 in python in c++ , without external library

i in question: create icon in memory win32 in python in c++ , without external library

since accepted answer uses wxwidgets library, wrapper of win32 api, solution translates pretty nicely.

all need create bitmap in memory using createcompatiblebitmap function. can draw bitmap using standard gdi drawing functions. finally, create icon using createiconindirect function.

the hardest part keeping track of resources , making sure free them when you're finished prevent memory leaks. it's way better if it's wrapped in library makes use of raii ensure objects freed, if you're writing c code in c++, this:

hicon createsolidcoloricon(colorref iconcolor, int width, int height) {     // obtain handle screen device context.     hdc hdcscreen = getdc(null);      // create memory device context, draw into.     hdc hdcmem = createcompatibledc(hdcscreen);      // create bitmap, , select device context drawing.     hbitmap hbmp = createcompatiblebitmap(hdcscreen, width, height);         hbitmap hbmpold = (hbitmap)selectobject(hdcmem, hbmp);      // draw icon.     //      // simple example, we're drawing solid color rectangle     // in specified color specified dimensions.     hpen hpen        = createpen(ps_solid, 1, iconcolor);     hpen hpenold     = (hpen)selectobject(hdcmem, hpen);     hbrush hbrush    = createsolidbrush(iconcolor);     hbrush hbrushold = (hbrush)selectobject(hdcmem, hbrush);     rectangle(hdcmem, 0, 0, width, height);     selectobject(hdcmem, hbrushold);     selectobject(hdcmem, hpenold);     deleteobject(hbrush);     deleteobject(hpen);      // create icon bitmap.     //      // icons require masks indicate transparent , opaque areas. since     // simple example has no transparent areas, use opaque mask.     hbitmap hbmpmask = createcompatiblebitmap(hdcscreen, width, height);     iconinfo ii;     ii.ficon = true;     ii.hbmmask = hbmpmask;     ii.hbmcolor = hbmp;     hicon hicon = createiconindirect(&ii);     deleteobject(hbmpmask);      // clean-up.     selectobject(hdcmem, hbmpold);     deleteobject(hbmp);     deletedc(hdcmem);     releasedc(null, hdcscreen);      // return icon.     return hicon; } 

adding error checking , code draw interesting onto bitmap left exercise reader.

as said in comment above, once have created icon, can set icon window sending wm_seticon message , passing hicon lparam:

sendmessage(hwnd, wm_seticon, icon_big, (lparam)hicon); 

you can specify icon_small in order set window's small icon. if set big icon, scaled down create small icon automatically. however, if set small icon, window continue use default icon big icon. big icons typically have dimension of 32x32, while small icons typically have dimension of 16x16. not, however, guaranteed, not hardcode these values. if need determine them, call getsystemmetrics function sm_cxicon , sm_cyicon retrieve width , height of big icons, or sm_cxsmicon , sm_cysmicon retrieve width , height of small icons.

a tutorial on drawing in windows using gdi available here. recommend reading thoroughly if first time doing , have no prior experience gdi.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -