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 più finestre simultaneamente in C++
Forum - C/C++ - Creare più finestre simultaneamente in C++

Avatar
Stromberg (Normal User)
Newbie


Messaggi: 4
Iscritto: 26/12/2011

Segnala al moderatore
Postato alle 20:50
Domenica, 18/03/2012
Ultimamento sto lavorando ad un programma per disegnare delle funzioni. La parte grafica e riuscita senza problemi, ora volevo mettere un pulsante che mi permettesse di scegliere la funzione da rappresentare. Il punto è che vorrei che quando clicco su un pulsante, mi si apre una finestrella in cui posso specificare i parametri della funzione da rappresentare. Come faccio a far creare una finestra alla pressione di un pulsante? Grazie.
Compilatore:MSVisual C++ Express 2010
Sistema operativo:Win 7 Professional
Come libreria uso le api di windows, quindi la Windows.h
////////////////////////////////////////////////////////////////////
Codice sorgente - presumibilmente C#

  1. #include <windows.h>
  2. /*Declare Windows procedure  */
  3. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  4. /*Make the class name into a global variable  */
  5. char szClassName[ ] = "WindowsApp";
  6. int WINAPI WinMain (HINSTANCE hThisInstance,
  7.                     HINSTANCE hPrevInstance,
  8.                     LPSTR lpszArgument,
  9.                     int nFunsterStil)
  10. {
  11.     HWND hwnd;               /* This is the handle for our window */
  12.     MSG messages;            /* Here messages to the application are saved */
  13.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  14.     /* The Window structure */
  15.     wincl.hInstance = hThisInstance;
  16.     wincl.lpszClassName = szClassName;
  17.     wincl.lpfnWndProc = WindowProcedure;  
  18.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  19.     wincl.cbSize = sizeof (WNDCLASSEX);
  20.     /* Use default icon and mouse-pointer */
  21.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  22.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  23.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  24.     wincl.lpszMenuName = NULL;                 /* No menu */
  25.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  26.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  27.     /* Use Windows's default color as the background of the window */
  28.     wincl.hbrBackground = GetSysColorBrush (COLOR_WINDOW);                      //Colore di sfondo bianco
  29.     /* Register the window class, and if it fails quit the program */
  30.     if (!RegisterClassEx (&wincl))
  31.         return 0;
  32.     /* The class is registered, let's create the program*/
  33.     hwnd = CreateWindowEx (
  34.            0,                   /* Extended possibilites for variation */
  35.            szClassName,         /* Classname */
  36.            "Conta click",       /* Title Text */
  37.            WS_OVERLAPPEDWINDOW, /* default window */
  38.            500,       /* Windows decides the position */
  39.            500,       /* where the window ends up on the screen */
  40.            210,                 /* The programs width */
  41.            230,                 /* and height in pixels */
  42.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  43.            NULL,                /* No menu */
  44.            hThisInstance,       /* Program Instance handler */
  45.            NULL                 /* No Window Creation data */
  46.            );
  47.     /* Make the window visible on the screen */
  48.     ShowWindow (hwnd, nFunsterStil);
  49.     /* Run the message loop. It will run until GetMessage() returns 0 */
  50.     while (GetMessage (&messages, NULL, 0, 0))
  51.     {
  52.         /* Translate virtual-key messages into character messages */
  53.         TranslateMessage(&messages);
  54.         /* Send message to WindowProcedure */
  55.         DispatchMessage(&messages);
  56.     }
  57.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  58.     return messages.wParam;
  59. }
  60. /*  This function is called by the Windows function DispatchMessage()  */
  61. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  62. {
  63.     switch (message)                  /* handle the messages */
  64.     {
  65.         case WM_CREATE:
  66.             CreateWindow("Button","Pulsante",WS_VISIBLE | WS_CHILD | WS_BORDER, 0,0,100,100,hwnd,(HMENU) 1,0,0);
  67.             break;
  68.         case WM_COMMAND:
  69.              if (LOWORD(wParam)==1)
  70.             {
  71.                   //CHE CI DEVO METTE QUà PER CREARE UN'ALTRA FINESTRA?
  72.              }
  73.             break;
  74.         case WM_DESTROY:
  75.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  76.             break;
  77.         default:                      /* for messages that we don't deal with */
  78.             return DefWindowProc (hwnd, message, wParam, lParam);
  79.     }
  80.     return 0;
  81. }


Come lo dovrei modificare?:-?

Ultima modifica effettuata da Stromberg il 20/03/2012 alle 14:33
PM
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6380
Iscritto: 03/01/2010

Up
1
Down
V
Segnala al moderatore
Postato alle 21:37
Mercoledì, 21/03/2012
Andando a grandissime linee e con tutti i se e i ma ...

(andrebbe rivisto il codice ed organizzato seriamente in funzioni ...)

Codice sorgente - presumibilmente C#

  1. #include <windows.h>
  2. /*Declare Windows procedure  */
  3. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  4. LRESULT CALLBACK WindowProcedure2 (HWND, UINT, WPARAM, LPARAM);
  5.  
  6. /*Make the class name into a global variable  */
  7. char szClassName[ ] = "WindowsApp";
  8. char szClassName2[ ] = "WindowsApp2";
  9. HINSTANCE hinst;
  10.  
  11. int WINAPI WinMain (HINSTANCE hThisInstance,
  12.                     HINSTANCE hPrevInstance,
  13.                     LPSTR lpszArgument,
  14.                     int nFunsterStil)
  15. {
  16.     HWND hwnd;               /* This is the handle for our window */
  17.        
  18.     MSG messages;            /* Here messages to the application are saved */
  19.         WNDCLASSEX wincl;        /* Data structure for the windowclass */
  20.         WNDCLASSEX wincl2;
  21.        
  22.         hinst = hThisInstance;
  23.  
  24.         /* The Window structure */
  25.     wincl.hInstance = hinst;
  26.     wincl.lpszClassName = szClassName;
  27.     wincl.lpfnWndProc = WindowProcedure;  
  28.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  29.     wincl.cbSize = sizeof (WNDCLASSEX);
  30.     /* Use default icon and mouse-pointer */
  31.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  33.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  34.     wincl.lpszMenuName = NULL;                 /* No menu */
  35.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  36.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  37.     /* Use Windows's default color as the background of the window */
  38.     wincl.hbrBackground = GetSysColorBrush (COLOR_WINDOW);                      //Colore di sfondo bianco
  39.     /* Register the window class, and if it fails quit the program */
  40.     if (!RegisterClassEx (&wincl))
  41.         return 0;
  42.  
  43.         /* The Window structure */
  44.     wincl2.hInstance = hThisInstance;
  45.     wincl2.lpszClassName = szClassName2;
  46.     wincl2.lpfnWndProc = WindowProcedure2;  
  47.     wincl2.style = CS_DBLCLKS;                 /* Catch double-clicks */
  48.     wincl2.cbSize = sizeof (WNDCLASSEX);
  49.     /* Use default icon and mouse-pointer */
  50.     wincl2.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  51.     wincl2.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  52.     wincl2.hCursor = LoadCursor (NULL, IDC_ARROW);
  53.     wincl2.lpszMenuName = NULL;                 /* No menu */
  54.     wincl2.cbClsExtra = 0;                      /* No extra bytes after the window class */
  55.     wincl2.cbWndExtra = 0;                      /* structure or the window instance */
  56.     /* Use Windows's default color as the background of the window */
  57.     wincl2.hbrBackground = GetSysColorBrush (COLOR_WINDOW);                      //Colore di sfondo bianco
  58.     /* Register the window class, and if it fails quit the program */
  59.     if (!RegisterClassEx (&wincl2))
  60.         return 0;
  61.  
  62.  
  63.     /* The class is registered, let's create the program*/
  64.     hwnd = CreateWindowEx (
  65.            0,                   /* Extended possibilites for variation */
  66.            szClassName,         /* Classname */
  67.            "Conta click",       /* Title Text */
  68.            WS_OVERLAPPEDWINDOW, /* default window */
  69.            500,       /* Windows decides the position */
  70.            500,       /* where the window ends up on the screen */
  71.            210,                 /* The programs width */
  72.            230,                 /* and height in pixels */
  73.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  74.            NULL,                /* No menu */
  75.            hinst,       /* Program Instance handler */
  76.            NULL                 /* No Window Creation data */
  77.            );
  78.     /* Make the window visible on the screen */
  79.     ShowWindow (hwnd, nFunsterStil);
  80.     /* Run the message loop. It will run until GetMessage() returns 0 */
  81.     while (GetMessage (&messages, NULL, 0, 0))
  82.     {
  83.         /* Translate virtual-key messages into character messages */
  84.         TranslateMessage(&messages);
  85.         /* Send message to WindowProcedure */
  86.         DispatchMessage(&messages);
  87.     }
  88.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  89.     return messages.wParam;
  90. }
  91.  
  92. /*  This function is called by the Windows function DispatchMessage()  */
  93. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  94. {
  95.     switch (message)                  /* handle the messages */
  96.     {
  97.         case WM_CREATE:
  98.             CreateWindow("Button","Pulsante",WS_VISIBLE | WS_CHILD | WS_BORDER, 0,0,100,100,hwnd,(HMENU) 1,0,0);
  99.             break;
  100.         case WM_COMMAND:
  101.              if (LOWORD(wParam)==1)
  102.              {
  103.                                 MSG messages2;
  104.                                 HWND hwnd2 = CreateWindowEx (
  105.                                            0,                   /* Extended possibilites for variation */
  106.                                            szClassName2,         /* Classname */
  107.                                            "Nuova finestra",       /* Title Text */
  108.                                            WS_OVERLAPPEDWINDOW, /* default window */
  109.                                            600,       /* Windows decides the position */
  110.                                            600,       /* where the window ends up on the screen */
  111.                                            300,                 /* The programs width */
  112.                                            300,                 /* and height in pixels */
  113.                                            hwnd,        
  114.                                            NULL,                /* No menu */
  115.                                            hinst,       /* Program Instance handler */
  116.                                            NULL                 /* No Window Creation data */
  117.                                            );
  118.  
  119.                                 /* Make the window visible on the screen */
  120.                                 ShowWindow (hwnd2, 1);
  121.  
  122.                                 /* Run the message loop. It will run until GetMessage() returns 0 */
  123.                                 while (GetMessage (&messages2, NULL, 0, 0))
  124.                                 {
  125.                                         /* Translate virtual-key messages into character messages */
  126.                                         TranslateMessage(&messages2);
  127.                                         /* Send message to WindowProcedure */
  128.                                         DispatchMessage(&messages2);
  129.                                 }
  130.              }
  131.             break;
  132.         case WM_DESTROY:
  133.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  134.             break;
  135.         default:                      /* for messages that we don't deal with */
  136.             return DefWindowProc (hwnd, message, wParam, lParam);
  137.     }
  138.     return 0;
  139. }
  140.  
  141.  
  142. LRESULT CALLBACK WindowProcedure2 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  143. {
  144.     switch (message)                  
  145.     {
  146.         case WM_CREATE:
  147.             break;
  148.         case WM_DESTROY:
  149.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  150.             break;
  151.         default:                      /* for messages that we don't deal with */
  152.             return DefWindowProc (hwnd, message, wParam, lParam);
  153.     }
  154.     return 0;
  155. }


Ultima modifica effettuata da nessuno il 21/03/2012 alle 21:38
CHE DIO TI BENEDICA!!!! AVEVO FATTO CENTINAIA DIPROVE MOLTO SIMILI A QUESTA COMUNQUE GRAZIE INFINITE!!!!! - Stromberg - 21/03/12 21:41
Scusa se approfitto per chiederlo, ma come si caricano su questo sito i programmi? - Stromberg - 21/03/12 21:44
Caricare i programmi in che senso? - nessuno - 21/03/12 23:05
Aggiungere un programma fatto da me al sito 'pierotofy' capito? - Stromberg - 22/03/12 19:15
Manda un messaggio privato a pierotofy - nessuno - 22/03/12 19:24
Ok, grazie per tutto! - Stromberg - 23/03/12 16:19


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti (uno dei padri fondatori del moderno Calcolo delle probabilità) chiamava il gioco del Lotto Tassa sulla stupidità.
PM
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6380
Iscritto: 03/01/2010

Up
0
Down
V
Segnala al moderatore
Postato alle 21:51
Domenica, 18/03/2012
Ma quali strumenti usi? Su che sistema? Quali librerie grafiche?

Scusa, Compilatore:MS Visual C++ 2010 Express - Stromberg - 18/03/12 22:31
Ok, ma che tipo di grafica? Di cosa parliamo ? Win32 ? .NET ... cerca di essere più preciso ... - nessuno - 18/03/12 22:59
Chiedo scusa, comunque Win 32 - Stromberg - 19/03/12 14:02
Scusa Stromberg ... capiamoci ... tu hai parlato di form e di pulsanti ... come pensi di crearle le finestre? - nessuno - 19/03/12 14:23
puoi essere più chiaro? io le finestre le creo con CreateWindowEx, ma il problema è che riesco a crearne solo una, e se ne creo 2 sono identiche...puoi aiutarmi? - Stromberg - 19/03/12 14:35
Ah .. ecco, le crei direttamente con le API senza librerie particolari (non mi pare una grande idea, così impazzisci). Ti consiglio di utilizzare delle librerie apposite ... ne esistono tante che facilitano enormemente il compito. - nessuno - 19/03/12 15:45
Grazie del consiglio ma puoi ripsondere alla mia domanda? - Stromberg - 19/03/12 21:30
Devi usare la CreateWindowEx ... se non mostri il codice che hai scritto non posso dirti dove sbagli - nessuno - 19/03/12 23:08
Modificato! - Stromberg - 20/03/12 14:33
Allora, puoi aiutarmi? - Stromberg - 21/03/12 20:24


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti (uno dei padri fondatori del moderno Calcolo delle probabilità) chiamava il gioco del Lotto Tassa sulla stupidità.
PM