IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

(win32) Modifier un HBITMAP


Sujet :

C

  1. #1
    Membre éclairé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Septembre 2015
    Messages
    204
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Septembre 2015
    Messages : 204
    Points : 839
    Points
    839
    Par défaut (win32) Modifier un HBITMAP
    Bonjour

    J'essaie de modifier un HBITMAP
    J'ai donc un bitmap d'origine sur lequel j'applique une modification et j'affiche le nouveau bitmap

    sauf que j'ai le même texte "Image n°9" sur toutes les images

    Je n'arrive pas à trouver d'où vient mon erreur
    J'ai l'impression qu'il manque aussi un DeleteObject pour la police

    Merci

    voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    HBITMAP Modifier_Bitmap (HWND hWndDlg, HBITMAP Mon_image_init, HFONT hFont, int index )
    {
    	HBITMAP Mon_image;
    	HDC hDC;
    	HDC hdcMem;
    	char Message [20];
     
    	wsprintf (Message, "Image n°%d", index);
     
    	Mon_image = Mon_image_init;
     
    	hDC = GetDC(hWndDlg);
    	hdcMem = CreateCompatibleDC(hDC);
    	SelectObject(hdcMem, Mon_image);
    	SelectObject(hdcMem, hFont);
    	TextOut(hdcMem, 50, 50, Message, lstrlen(Message));
    	DeleteDC(hdcMem);          
    	ReleaseDC(hWndDlg, hDC);
     
    	return Mon_image;
    }
     
     
     
    ...
    HBITMAP Mon_image_init;
    HBITMAP Mon_image;
    HFONT hFont;
     
     
     
    Mon_image_init = (HBITMAP)LoadImage (...);
    hFont = CreateFontIndirect(...);
     
    for (int i = 1; i <= 9; i++)
    {
    	Mon_image = Modifier_Bitmap ( hWndDlg,  Mon_image_init,  hFont,  i );
     
    	hStatic = GetDlgItem(hWndDlg, (5000 + i));
    	SendMessage(hStatic, STM_SETIMAGE ,IMAGE_BITMAP ,(LPARAM)Mon_image);
    }

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    À aucun moment tu ne crées de nouvelle image.
    En ignorant toute gestion d'erreur, tu devrais avoir un truc de ce genre:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    HBITMAP CreateRgbBitmap(LONG width, LONG height, WORD bitCount)
    {
    	BITMAPINFO bmi = {0};
    	LPVOID pBits = NULL;
    	if(bitCount < 16 || bitCount > 32)
    		return NULL;
    	bmi.bmiHeader.cbSize = sizeof(bmi.bmiHeader);
    	bmi.bmiHeader.biWidth = width;
    	bmi.bmiHeader.biHeight = height;
    	bmi.bmiHeader.biPlanes = 1;
    	bmi.bmiHeader.biBitCount = 24;
    	bmi.bmiHeader.biCompression = BI_RGB;
    	return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pBits, NULL, 0);
    }
    HBITMAP CreateSameSizeRgbBitmap(HBITMAP source)
    {
    	BITMAP bmp;
    	GetObject(source, sizeof(bmp), &bmp);
    	return CreateRgbBitmap(bmp.width, bmp.height, max(bmBitsPixel, 16));
    }
     
    HBITMAP Modifier_Bitmap(HBITMAP Mon_image_init, HFONT hFont, int index )
    {
    	HBITMAP Mon_image = CreateSameSizeRgbBitmap(Mon_image_init);
    	HDC hdcSource = CreateCompatibleDC(NULL);
    	HDC hdcDest = CreateCompatibleDC(NULL);
    	TCHAR Message[20] = _T("");
    	BITMAP bmp;
    	wsprintf(Message, _T("Image n°%d"), index);
    	GetObject(Mon_image_init, sizeof(bmp), &bmp);
     
    	{
    		HGDIOBJ oldBmpSource = SelectObject(hdcSource, Mon_image_init);
    		HGDIOBJ oldBmpDest = SelectObject(hdcDest, Mon_image);
    		HGDIOBJ oldFont = SelectObject(hdcDest, hFont);
    		BitBlt(hdcDest, 0, 0, bmp.width, bmp.height, hdcSource, 0, 0, SRCCOPY);
    		TextOut(hdcDest, 50, 50, Message, lstrlen(Message));
    		SelectObject(hdcSource, oldBmpSource);
    		SelectObject(hdcDest, oldBmpDest);
    		SelectObject(hdcDest, oldFont);
    	}
    	DeleteDC(hdcDest);
    	DeleteDC(hdcSource);
     
    	return Mon_image;
    }
    Note: je n'ai pas testé, je n'ai même pas essayé de compiler. Mais c'est l'idée générale.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre éclairé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Septembre 2015
    Messages
    204
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Septembre 2015
    Messages : 204
    Points : 839
    Points
    839
    Par défaut
    Merci ton code fonctionne (en corrigeant les légères erreurs )

    entre temps, j'avais trouvé la fonction CopyImage qui fonctionne aussi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. modifier le thème d'une fenêtre avec l'API win32
    Par d'Oursse dans le forum Windows
    Réponses: 9
    Dernier message: 19/02/2012, 16h03
  2. Réponses: 10
    Dernier message: 15/09/2010, 12h55
  3. [ Win32 GDI ] récupérer le HBITMAP d'un HDC
    Par cyber_N dans le forum Windows
    Réponses: 2
    Dernier message: 20/10/2005, 21h18
  4. utiliser les tag [MFC] [Win32] [.NET] [C++/CLI]
    Par hiko-seijuro dans le forum Visual C++
    Réponses: 8
    Dernier message: 08/06/2005, 15h57
  5. Réponses: 5
    Dernier message: 06/08/2002, 20h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo