Fix resource leaks

svn path=/trunk/; revision=21237
This commit is contained in:
Ged Murphy 2006-03-06 00:41:05 +00:00
parent e8d6081de4
commit 15e1ae419b
2 changed files with 25 additions and 23 deletions

View file

@ -5,9 +5,11 @@
#define TWOPI (2 * 3.14159) #define TWOPI (2 * 3.14159)
static const TCHAR szClockWndClass[] = TEXT("ClockWndClass"); static const TCHAR szClockWndClass[] = TEXT("ClockWndClass");
static HBRUSH hGreyBrush = NULL;
static HPEN hGreyPen = NULL;
static VOID
VOID SetIsotropic(HDC hdc, INT cxClient, INT cyClient) SetIsotropic(HDC hdc, INT cxClient, INT cyClient)
{ {
/* set isotropic mode */ /* set isotropic mode */
SetMapMode(hdc, MM_ISOTROPIC); SetMapMode(hdc, MM_ISOTROPIC);
@ -15,7 +17,8 @@ VOID SetIsotropic(HDC hdc, INT cxClient, INT cyClient)
SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL); SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
} }
VOID RotatePoint(POINT pt[], INT iNum, INT iAngle) static VOID
RotatePoint(POINT pt[], INT iNum, INT iAngle)
{ {
INT i; INT i;
POINT ptTemp; POINT ptTemp;
@ -32,16 +35,18 @@ VOID RotatePoint(POINT pt[], INT iNum, INT iAngle)
} }
} }
VOID DrawClock(HDC hdc) static VOID
DrawClock(HDC hdc)
{ {
INT iAngle; INT iAngle;
POINT pt[3]; POINT pt[3];
HBRUSH hBrush, hBrushOld; HBRUSH hBrushOld;
HPEN hPen = NULL, hPenOld = NULL; HPEN hPenOld = NULL;
/* grey brush to fill the dots */ /* grey brush to fill the dots */
hBrush = CreateSolidBrush(RGB(128, 128, 128)); hBrushOld = SelectObject(hdc, hGreyBrush);
hBrushOld = SelectObject(hdc, hBrush);
hPenOld = GetCurrentObject(hdc, OBJ_PEN);
for(iAngle = 0; iAngle < 360; iAngle += 6) for(iAngle = 0; iAngle < 360; iAngle += 6)
{ {
@ -56,10 +61,8 @@ VOID DrawClock(HDC hdc)
* i.e. 1-4 or 5, 6-9 or 10, 11-14 or 15 */ * i.e. 1-4 or 5, 6-9 or 10, 11-14 or 15 */
if (iAngle % 5) if (iAngle % 5)
{ {
pt[2].x = pt[2].y = 7; pt[2].x = pt[2].y = 7;
hPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128)); SelectObject(hdc, hGreyPen);
hPenOld = SelectObject(hdc, hPen);
} }
else else
{ {
@ -76,14 +79,13 @@ VOID DrawClock(HDC hdc)
Ellipse(hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y); Ellipse(hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
} }
SelectObject(hdc, hPenOld);
SelectObject(hdc, hBrushOld); SelectObject(hdc, hBrushOld);
DeleteObject(hBrush); SelectObject(hdc, hPenOld);
DeleteObject(hPen);
} }
VOID DrawHands(HDC hdc, SYSTEMTIME * pst, BOOL fChange) static VOID
DrawHands(HDC hdc, SYSTEMTIME * pst, BOOL fChange)
{ {
static POINT pt[3][5] = { {{0, -30}, {20, 0}, {0, 100}, {-20, 0}, {0, -30}}, static POINT pt[3][5] = { {{0, -30}, {20, 0}, {0, 100}, {-20, 0}, {0, -30}},
{{0, -40}, {10, 0}, {0, 160}, {-10, 0}, {0, -40}}, {{0, -40}, {10, 0}, {0, 160}, {-10, 0}, {0, -40}},
@ -116,6 +118,7 @@ ClockWndProc(HWND hwnd,
WPARAM wParam, WPARAM wParam,
LPARAM lParam) LPARAM lParam)
{ {
static INT cxClient, cyClient; static INT cxClient, cyClient;
static SYSTEMTIME stPrevious; static SYSTEMTIME stPrevious;
HDC hdc; HDC hdc;
@ -125,6 +128,8 @@ ClockWndProc(HWND hwnd,
switch (uMsg) switch (uMsg)
{ {
case WM_CREATE: case WM_CREATE:
hGreyPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
hGreyBrush = CreateSolidBrush(RGB(128, 128, 128));
SetTimer(hwnd, ID_TIMER, 1000, NULL); SetTimer(hwnd, ID_TIMER, 1000, NULL);
GetLocalTime(&st); GetLocalTime(&st);
stPrevious = st; stPrevious = st;
@ -155,6 +160,8 @@ ClockWndProc(HWND hwnd,
break; break;
case WM_DESTROY: case WM_DESTROY:
DeleteObject(hGreyPen);
DeleteObject(hGreyBrush);
KillTimer(hwnd, ID_TIMER); KillTimer(hwnd, ID_TIMER);
break; break;

View file

@ -32,11 +32,6 @@ VOID DestroyConnection(VOID);
BOOL SendData(VOID); BOOL SendData(VOID);
BOOL RecieveData(CHAR *); BOOL RecieveData(CHAR *);
VOID SetIsotropic (HDC hdc, INT cxClient, INT cyClient);
VOID RotatePoint (POINT pt[], INT iNum, INT iAngle);
VOID DrawClock (HDC hdc);
VOID DrawHands (HDC hdc, SYSTEMTIME * pst, BOOL fChange);
#endif /* __CPL_SAMPLE_H */ #endif /* __CPL_SAMPLE_H */
/* EOF */ /* EOF */