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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <tchar.h>
#define MAX_PORT_NUM (256)
#define MAX_STR_LEN (256*sizeof(TCHAR))
#define ID_B_START 0
#define ID_B_Stop 1
#define ID_COMBOBOX_PORT 4
HINSTANCE instance;
LRESULT CALLBACK procedureFenetrePrincipale(HWND, UINT, WPARAM, LPARAM);
void Open_Com (HANDLE hCom, DCB dcb); //Fonction qui ouvre la communication , lecture , écriture...
void Close_Com (HANDLE hCom, DCB dcb); //Fonction pour fermer le communication
BOOL EnumerateComPortByCreateFile(UINT *pNumber, TCHAR *pPortName, int strMaxLen); //Fonction pour lister les ports Com existants sur la amchine
HWND CreateComboboxPort (HWND Principale)
{
HWND hComboBox;
static TCHAR portName[MAX_PORT_NUM][MAX_STR_LEN];
static UINT i;
static UINT n;
hComboBox=CreateWindow(
L"COMBOBOX",
L"Port",
WS_CHILD|WS_VISIBLE|CBS_DISABLENOSCROLL|CBS_DROPDOWNLIST,
180,10,
180,150,
Principale,
(HMENU)ID_COMBOBOX_PORT,
instance,
NULL);
//Entrer des items dans la Combobox de Ports Comm par la liste des ports existants sur la machine
EnumerateComPortByCreateFile(&n, &portName[0][0], MAX_STR_LEN);
for(i = 0; i< n; i++)
SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)&portName[i][0]);
return hComboBox;
}
HWND CreateButton0 (HWND fenetrePrincipale)
{
HWND bouton;
bouton = CreateWindow(
L"BUTTON",
L"Ouverture",
WS_CHILD | WS_VISIBLE,
5,
85,
383,
30,
fenetrePrincipale,
(HMENU)ID_B_CONFIGURATION,
instance,
NULL);
return bouton;
}
HWND CreateButton1 (HWND fenetrePrincipale)
{
HWND bouton;
bouton = CreateWindow(
L"BUTTON",
L"Fermeture",
WS_CHILD | WS_VISIBLE,
5,
125,
383,
30,
fenetrePrincipale,
(HMENU)ID_B_START,
instance,
NULL);
return bouton;
}
int WinMain (HINSTANCE cetteInstance, HINSTANCE precedenteInstance, LPSTR lignesDeCommande, int modeDAffichage)
{
HWND fenetrePrincipale;
MSG message;
WNDCLASS classeFenetre;
instance = cetteInstance;
classeFenetre.style = 0;
classeFenetre.lpfnWndProc = procedureFenetrePrincipale; //fct callback
classeFenetre.cbClsExtra = 0;
classeFenetre.cbWndExtra = 0;
classeFenetre.hInstance = NULL;
classeFenetre.hIcon = LoadIcon(NULL, IDI_APPLICATION);
classeFenetre.hCursor = LoadCursor(NULL, IDC_ARROW);
classeFenetre.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
classeFenetre.lpszMenuName = NULL;
classeFenetre.lpszClassName = L"classeF";
// test du cas où ça échoue
if(!RegisterClass(&classeFenetre))
return FALSE;
fenetrePrincipale = CreateWindow(
L"classeF",
L"Interface !",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
400,
NULL,
NULL,
cetteInstance,
NULL);
if (!fenetrePrincipale)
return FALSE;
ShowWindow(fenetrePrincipale, modeDAffichage);
UpdateWindow(fenetrePrincipale);
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
LRESULT CALLBACK procedureFenetrePrincipale(HWND fenetrePrincipale, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hComboBoxPort;
static HWND bouton0;
static HWND bouton1;
static DCB dcb;
static HANDLE hCom;
static LRESULT Port; // récupérer le choix de la combobox
switch (message)
{
case WM_CREATE:
hComboBoxPort=CreateComboboxPort (fenetrePrincipale);
bouton0=CreateButton0 (fenetrePrincipale);
bouton1=CreateButton1 (fenetrePrincipale);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
/* là mes deux fonctions Open_Com et Close_Com ne dépendent pas du port choisi. Je l'ai déjà initialisé.
Mais je veux qu'elles prennent le choix du port sélectionnée comme paramètre.
Donc ouvrir ou fermer la communication sur ce port sélectionné.
En gros, je veux envoyer l'item choisi de la combobox qui 'Port' dans mon code aux deux fonction Open et Colse */
Port=SendMessage(hComboboxPort,CB_GETCURSEL,0,0); // le port sélectionné
case ID_B_START :
Open_Com ( hCom, dcb );
break;
case ID_B_STop:
Close_Com (hCom, dcb);
SendMessage(fenetrePrincipale, WM_DESTROY, 0, 0);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(fenetrePrincipale, message, wParam, lParam);
}
} |
Partager