Wie speichere ich in windows 11 Icons-/ Iconsets als dll für msys2/gtk?

bluedxca93

Commander
Registriert
Juli 2019
Beiträge
2.236
icon-fix.png

Wie speichere ich unter windows 11 mithilfe von gcc mehrere icons in eine dll? So dass ich diese dann in einer GTK Anwednung benutzen kann, sobald ich diese dll lade? Technisch ist es ja möglich auf icons in windows DLLs zuzugreifen und die icons als gtk item-/bitmab darzustellen. Nur wie packe ich jetzt als png Dateien in dlls?
 
Zuletzt bearbeitet:
Kurzes Googeln brachte diesen Thread bei StackOverflow hervor. Hilft dir das weiter?
Viele Grüße
 
Hi,

das ist genau die Richtung wo ich meinte. Mir geht visual studio auf die Nerven weshalb ich alles mit gcc und msys2 versuche. Aber genau in die Richtung meinte ich, hab es jetzt mit chatgpt und googeln rausbekommen.

Dann werde ich die Tage den Code in die Toolbar einfügen.

Bildschirmfoto zu 2025-08-26 08-21-31.png
cab-shell-gtk.png
 
Hab es fertig ! Thread kann zu! newlogo.png
 
  • Gefällt mir
Reaktionen: ErichH.
pngdll.h
#ifndef PNGDLL_H
#define PNGDLL_H

#include <gdk-pixbuf/gdk-pixbuf.h>

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport)
GdkPixbuf* pngdll_load_pixbuf(const char *name, int width, int height);

#ifdef __cplusplus
}
#endif

// Auto-generated defines (from resources.rc). Included by example_app.
#include "resources.h"

#endif /* PNGDLL_H */

pngdll.c
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdint.h>
#include <string.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "pngdll.h"

// Helper: get HMODULE of this DLL (works even if not linked via import lib)
static HMODULE self_module(void) {
HMODULE hm = NULL;
if (GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)&self_module, &hm)) {
return hm;
}
return NULL;
}

// Load raw bytes from a resource
static gboolean load_resource_bytes(const char *name,
const char *type,
const void **data,
DWORD *size) {
HMODULE mod = self_module();
if (!mod) return FALSE;

HRSRC r = FindResourceA(mod, name, type);
if (!r) return FALSE;

*size = SizeofResource(mod, r);
if (*size == 0) return FALSE;

HGLOBAL h = LoadResource(mod, r);
if (!h) return FALSE;

*data = LockResource(h);
return (*data != NULL);
}

// Exported function: load PNG from resources into a GdkPixbuf
__declspec(dllexport)
GdkPixbuf* pngdll_load_pixbuf(const char *name, int width, int height) {
const void *data = NULL;
DWORD size = 0;

if (!load_resource_bytes(name, "PNG", &data, &size)) {
g_warning("pngdll: could not load resource '%s'", name);
return NULL;
}

// Create pixbuf loader
GError *error = NULL;
GdkPixbufLoader *loader = gdk_pixbuf_loader_new_with_type("png", &error);
if (!loader) {
g_warning("pngdll: gdk_pixbuf_loader_new_with_type failed: %s",
error ? error->message : "unknown error");
if (error) g_error_free(error);
return NULL;
}

// Feed resource data into loader
if (!gdk_pixbuf_loader_write(loader, (const guchar*)data, size, &error)) {
g_warning("pngdll: gdk_pixbuf_loader_write failed: %s",
error ? error->message : "unknown error");
if (error) g_error_free(error);
g_object_unref(loader);
return NULL;
}
gdk_pixbuf_loader_close(loader, NULL);

// Extract pixbuf
GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
if (!pixbuf) {
g_warning("pngdll: no pixbuf from loader");
g_object_unref(loader);
return NULL;
}

// Increase refcount since loader owns one
g_object_ref(pixbuf);
g_object_unref(loader);

// Scale if requested
if (width > 0 && height > 0) {
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pixbuf,
width,
height,
GDK_INTERP_BILINEAR);
g_object_unref(pixbuf);
pixbuf = scaled;
}

return pixbuf;
}

packscript

# Usage: ./pack_pngs_to_dll.sh icons.dll assets/*.png
# Generates resources.rc + resources.h from given PNGs and builds a DLL with helper API.
##bash pack.sh cab-res.dll *.png


if [ "$#" -lt 2 ]; then
echo "Usage: $0 <output.dll> <png...>" >&2
exit 1
fi


OUTDLL=$1; shift
RCFILE=resources.rc
HFILE=resources.h
OBJRC=resources.o
IMPLIB="lib${OUTDLL%.dll}.a"


# Create resources.rc and resources.h
: > "$RCFILE"
: > "$HFILE"
echo "// Auto-generated header for resource IDs" >> "$HFILE"
for f in "$@"; do
base=$(basename "$f")
name=${base%.*}
# Normalize to [A-Za-z0-9_] and uppercase for simple usage
norm=$(echo "$name" | tr '[:lower:]' '[:upper:]' | sed 's/[^A-Za-z0-9_]/_/g')
echo "$norm PNG \"$f\"" >> "$RCFILE"
echo "#define RES_${norm} \"${norm}\"" >> "$HFILE"
echo "added: $norm <- $f"
done


echo "Compiling resources..."
windres "$RCFILE" -O coff -o "$OBJRC"


echo "Building $OUTDLL ..."
# Build the helper DLL that exposes pngdll_load_pixbuf and contains the resources
gcc -shared -o "$OUTDLL" pngdll.c "$OBJRC" \
-Wl,--out-implib,"$IMPLIB" \
pkg-config --cflags --libs gtk+-3.0 gdk-pixbuf-2.0 -lgdi32 -lole32


echo "Built $OUTDLL and $IMPLIB"
Ergänzung ()

Also um es kurz zu fassen mit windres erstellt man eine windows resource und ich speichere die icons hier als png, weil ich das einfacher zu verwalten finde. Da mein Projekt auf gtk3 basiert, habe ich auch die Funktion um die Bilder zu laden in doe dll gepackt. muss nur die .h in meinem Programm einbinden und gegen die dll mit den Bildern drin linken. Geht mit visual C sivher analog.
 
  • Gefällt mir
Reaktionen: IDontWantAName
Zurück
Oben