TIMER (VC++ SANS MFC) (FORUM)

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 13 juil. 2004 à 20:46
ssinfod Messages postés 1 Date d'inscription lundi 31 octobre 2005 Statut Membre Dernière intervention 15 novembre 2005 - 15 nov. 2005 à 15:29
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/24522-timer-vc-sans-mfc-forum

ssinfod Messages postés 1 Date d'inscription lundi 31 octobre 2005 Statut Membre Dernière intervention 15 novembre 2005
15 nov. 2005 à 15:29
Salut,
j'ai fait une version qui compile sur borlandx et devc++, voila tout est dans un fichier..(main.cpp)


#include <windows.h>

#define ZERO 0
#define ONE 1
#define IDT_TIMER1 WM_USER+100

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK BtnStartProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK BtnStopProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

HWND hwndMain, hwndStatic;
HWND hwndBtnStart, hwndBtnStop;
WNDPROC OldBtnStartProc, OldBtnStopProc;

//////////////////////////////////////////////////////////////////////////////////
//WinMain
//////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wcx;

// Fill in the WNDCLASS Struct
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_DBLCLKS;
wcx.lpfnWndProc = MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInst;
wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor (NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "EzTimer";
wcx.hIconSm = NULL;

// Register Class
if (!RegisterClassEx(&wcx))
{
return 0;
}

// Create Main Window
long lSizeX=160;
long lSizeY=70;
hwndMain = CreateWindowEx(0, "EzTimer", "EzTimer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, lSizeX, lSizeY, NULL, NULL, hInst, NULL);

//Buttons
hwndBtnStart = CreateWindowEx(0, "BUTTON", "&Start", WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 0, 0, 50, 50, hwndMain, NULL, hInst, NULL);
hwndBtnStop = CreateWindowEx(0, "BUTTON", "&Stop", WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 0, 0, 50, 50, hwndMain, NULL, hInst, NULL);

// SubClass
OldBtnStartProc = (WNDPROC) SetWindowLong(hwndBtnStart,GWL_WNDPROC,(LONG)BtnStartProc);
OldBtnStopProc = (WNDPROC) SetWindowLong(hwndBtnStop,GWL_WNDPROC,(LONG)BtnStopProc);

// Make Window visible
ShowWindow(hwndMain,nCmdShow);
UpdateWindow(hwndMain);

// Change the default police
SendMessage(hwndBtnStart, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
SendMessage(hwndBtnStop, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
UpdateWindow(hwndMain);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

////////////////////////////////////////////////////////////////////////////
// Callback function for the Main Window class
////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_SIZE:

if (wParam != SIZE_MINIMIZED)
{
long lX=0, lY=0, lSizeX=0, lSizeY=0;
GetClientRect(hwndMain, &rc);

//Resize Button Read
lX=10, lY=10, lSizeX=60, lSizeY=20;
MoveWindow(hwndBtnStart, lX, lY, lSizeX, lSizeY, TRUE); //(X, Y, SIZE X, SIZE Y) //Read
lX=80, lY=10, lSizeX=60, lSizeY=20;
MoveWindow(hwndBtnStop, lX, lY, lSizeX, lSizeY, TRUE); //(X, Y, SIZE X, SIZE Y) //Clear
}
break;

// On traite l'evenement du TIMER
case WM_TIMER:
switch(wParam)
{
case IDT_TIMER1:
MessageBox(0,"Ce message s'affiche toutes les 3 secondes.","TIMER",MB_ICONINFORMATION);
break;
}
break;

default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}

return 0;
}

////////////////////////////////////////////////////////////////////////////////////
//Callback of the Button
////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK BtnStartProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc; HDC hdc; HPEN hpen; HBRUSH hbr; HGDIOBJ oldpen, oldbr; LOGBRUSH logbrush;

switch(msg)
{

case WM_PAINT:
// Draw Original Button
CallWindowProc(OldBtnStartProc, hwnd, msg, wParam, lParam);
break;

case WM_LBUTTONDOWN:
MessageBox(hwnd,"Le Timer va être lancé!","Message OK",MB_OK|MB_ICONINFORMATION);
// On mance le timer. Le temps est indiqué en milliseconde. 3000ms = 3s
SetTimer(hwndMain,IDT_TIMER1,3000,(TIMERPROC)NULL); //Watch out !. We need to use the mainHandle
break;

break;

default:
return CallWindowProc(OldBtnStartProc, hwnd, msg, wParam, lParam);
}

return 0;
}


////////////////////////////////////////////////////////////////////////////////////
//Callback of the Button Clear
////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK BtnStopProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc; HDC hdc; HPEN hpen; HBRUSH hbr; HGDIOBJ oldpen, oldbr; LOGBRUSH logbrush;

switch(msg)
{

case WM_PAINT:
// Draw Original Button
CallWindowProc(OldBtnStopProc, hwnd, msg, wParam, lParam);
break;

case WM_LBUTTONDOWN:
KillTimer(hwndMain,IDT_TIMER1); //Watch out !. We need to use the mainHandle
MessageBox(hwnd,"Le Timer a été arreté!","Message OK",MB_OK|MB_ICONINFORMATION);
break;

default:
return CallWindowProc(OldBtnStopProc, hwnd, msg, wParam, lParam);
}

return 0;
}
DeAtHCrAsH Messages postés 2670 Date d'inscription vendredi 25 janvier 2002 Statut Membre Dernière intervention 6 février 2013
13 juil. 2004 à 21:13
Ca y est j'ai revu la source. Ca se voit que c'est les vacances j'avais fait le code vraiment a l'arrache!

Tout est correcte maintenant.

Puis aussi quand vous mettez des notes laissez un commentaire avec de quoi expliquer, meme si je sais pourquoi j'ai eu le 1/10 :)

Shell
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
13 juil. 2004 à 20:46
ben alors tu arretes un timer qui n'existe pas et on en a l'annonce en plus.
Faut prendre le temps de coder correctement svp, on est pas aux pieces.
Rejoignez-nous