Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Creare button con bitmap (Win32)
Forum - C/C++ - Creare button con bitmap (Win32)

Avatar
()
Newbie


Messaggi:
Iscritto:

Segnala al moderatore
Postato alle 1:00
Giovedì, 01/01/1970
Eccomi,
sono esasperato perché non trovo la soluzione al mio dilemma.
Sono arrivato a scrivere un button in un form con le API di Windows.

Codice sorgente - presumibilmente C#

  1. #include <windows.h>
  2.  
  3. LRESULT CALLBACK WndProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam);
  4. int WINAPI WinMain(HINSTANCE instanza, HINSTANCE hPrevInstance, LPSTR argomenti, int stato);
  5.  
  6. HWND button;
  7.  
  8. int WINAPI WinMain(HINSTANCE instanza, HINSTANCE hPrevInstance, LPSTR argomenti, int stato)
  9. {
  10.         WNDCLASS window;
  11.         window.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
  12.         window.hCursor = LoadCursor(NULL, IDC_ARROW);
  13.         window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  14.         window.hInstance = instanza;
  15.         window.lpszClassName = TEXT("Form");
  16.         window.cbClsExtra = 0;
  17.         window.cbWndExtra = 0;
  18.         window.style = CS_HREDRAW | CS_VREDRAW;
  19.         window.lpszMenuName = 0;
  20.         window.lpfnWndProc = WndProc;
  21.         RegisterClass(&window);
  22.         HWND windowHandle = CreateWindow(TEXT("Form"), TEXT("Darken Rahl's Form"), WS_OVERLAPPEDWINDOW, 10, 10, 300, 300, NULL, NULL, instanza, NULL);
  23.         ShowWindow(windowHandle, stato);
  24.         UpdateWindow(windowHandle);
  25.  
  26.         MSG messaggio;
  27.  
  28.         while(GetMessage(&messaggio, NULL, 0, 0))
  29.         {
  30.                 TranslateMessage(&messaggio);
  31.                 DispatchMessage(&messaggio);
  32.         }
  33.  
  34.         return messaggio.wParam;
  35. }
  36.  
  37. LRESULT CALLBACK WndProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
  38. {
  39.     switch(message)
  40.     {
  41.                 case WM_CREATE:
  42.                         button = CreateWindow("Button", "Clicca",  WS_VISIBLE | WS_CHILD, 10, 10, 50, 50, handle, (HMENU)10, (HINSTANCE)GetWindowLong(handle, GWL_HINSTANCE), NULL);
  43.                         return 0;
  44.                         break;
  45.                 case WM_PAINT:
  46.                         HDC hdc;
  47.             PAINTSTRUCT ps;
  48.             hdc = BeginPaint(handle, &ps);
  49.                         EndPaint(handle, &ps);
  50.         return 0;
  51.                 break;
  52.         case WM_COMMAND:
  53.                 if (LOWORD(wparam) == 10)
  54.                         MessageBox(handle, "Hai cliccato il bottone", ":O", MB_OK);
  55.                 return 0;
  56.                 break;
  57.     case WM_DESTROY:
  58.         PostQuitMessage( 0 ) ;
  59.         return 0;
  60.                 break;
  61.         }
  62.     return DefWindowProc(handle, message, wparam, lparam);
  63. }



Ma la mia domanda più importante è : "Come faccio ad inserire un'immagine da un file nel button?"

Vi prego di aiutarmi, voglio conoscere e capire (possibilmente) come si fa.

PM
Avatar
mattia1481 (Member)
Pro


Messaggi: 84
Iscritto: 03/11/2008

Up
0
Down
V
Segnala al moderatore
Postato alle 8:46
Lunedì, 09/01/2012
Devi associare (come hai fatto per la finestra del tuo programma) alla classe button una WndProc, e nel blocco WM_PAINT devi inserire il codice per il disegno della bitmap che desideri venga visualizzata sul tuo pulsante.

Buon lavoro.

PM
Usa i commenti per chiedere spiegazioni o ringraziare le risposte.