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
| #include <windows.h>
LRESULT CALLBACK processmainmess( HWND, UINT, WPARAM, LPARAM);
HINSTANCE n0instance;
MSG message;
OPENFILENAME ofn;
int APIENTRY WinMain( HINSTANCE W_n0inst, HINSTANCE W_n0precinst, LPTSTR W_CmdLine, int W_cdeaffich) // entier signé (32 bits)
{
n0instance = W_n0inst;
WNDCLASS winclassmain;
winclassmain.hInstance = n0instance;
winclassmain.lpszMenuName = NULL;
winclassmain.lpszClassName = "Essai";
winclassmain.hIcon = NULL;
winclassmain.hCursor = LoadCursor(NULL,IDC_ARROW);
winclassmain.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
winclassmain.style = CS_VREDRAW | CS_HREDRAW;
winclassmain.lpfnWndProc = (WNDPROC)processmainmess;
winclassmain.cbWndExtra = 0;
winclassmain.cbClsExtra = 0;
if ( !RegisterClass( &winclassmain ) )
return( FALSE );
//
HWND Hdlgmain = CreateWindow("Essai", "test", WS_CAPTION | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 250, 100, NULL, NULL, n0instance, NULL);
if ( !Hdlgmain ) return( FALSE );
ShowWindow( Hdlgmain, W_cdeaffich ); // lancement manuel
UpdateWindow( Hdlgmain ); //
while(GetMessage( &message, NULL, 0, 0))
{
TranslateMessage( &message );
DispatchMessage( &message );
}
return message.wParam;
}
LRESULT CALLBACK processmainmess( HWND winmainkey, UINT IDMsg, WPARAM wParam, LPARAM lParam )
{
static HANDLE keyfile;
CHAR cheminfile[256];
static BOOL fileexist;
DWORD nbbytetoread=1024;
DWORD nbbyteread;
static CHAR Bloc[1025];
switch(IDMsg)
{
case WM_CREATE :
CreateWindow("BUTTON", "Ouvrir", WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 100, 35, winmainkey, (HMENU)1, n0instance, NULL);
CreateWindow("BUTTON", "Fermer", WS_CHILD | WS_VISIBLE | WS_BORDER, 110, 0, 100, 35, winmainkey, (HMENU)2, n0instance, NULL);
fileexist=false;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 1: // clic sur bouton ouvrir un fichier
if(fileexist) {MessageBox(winmainkey, "Fichier déjà ouvert", "test", MB_OK); break;}
// *** choix du Fichier à ouvrir
ZeroMemory(&ofn, sizeof(OPENFILENAME));
*cheminfile=0x0;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = winmainkey;
ofn.lpstrFile = cheminfile;
ofn.nMaxFile = 256;
ofn.lpstrFilter = "Tous les fichiers (*.*)\0*.*\0\0";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
//
MessageBox(winmainkey, "1", "je passe", MB_OK); // ************************************************ piège message 1
if (GetOpenFileName(&ofn)==false) break;
MessageBox(winmainkey, "2", "je passe", MB_OK); // ************************************************ piège message 2
//
keyfile = CreateFile(cheminfile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
fileexist=true;
ReadFile(keyfile, Bloc, nbbytetoread, &nbbyteread, NULL);
MessageBox(winmainkey, "Fichier ouvert et 1er bloc lu OK", "test", MB_OK);
break;
case 2: // clic sur bouton fermer le fichier déjà ouvert
if(!fileexist) {MessageBox(winmainkey, "Il n'y a pas de fichier ouvert", "test", MB_OK); break;}
CloseHandle(keyfile);
fileexist=false;
MessageBox(winmainkey, "Fichier fermé OK", "test", MB_OK);
break;
}
break;
case WM_CLOSE :
DestroyWindow( winmainkey );
break;
case WM_DESTROY :
if(fileexist) CloseHandle(keyfile);
PostQuitMessage(0);
break;
case WM_QUERYENDSESSION :
DestroyWindow( winmainkey );
break;
default :
return DefWindowProc( winmainkey, IDMsg, wParam, lParam );
};
return 0;
} |
Partager