Click here to Skip to main content
15,892,809 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Heap corruption. Pin
Polite Programmer3-Jun-07 1:47
Polite Programmer3-Jun-07 1:47 
GeneralRe: Heap corruption. Pin
Polite Programmer3-Jun-07 1:57
Polite Programmer3-Jun-07 1:57 
GeneralRe: Heap corruption. Pin
Gary R. Wheeler3-Jun-07 1:59
Gary R. Wheeler3-Jun-07 1:59 
GeneralRe: Heap corruption. Pin
CPallini3-Jun-07 3:47
mveCPallini3-Jun-07 3:47 
AnswerRe: Heap corruption. Pin
Matthew Faithfull3-Jun-07 3:08
Matthew Faithfull3-Jun-07 3:08 
AnswerRe: Heap corruption. Pin
CPallini3-Jun-07 3:48
mveCPallini3-Jun-07 3:48 
AnswerRe: Heap corruption. Pin
Stephen Hewitt3-Jun-07 14:08
Stephen Hewitt3-Jun-07 14:08 
GeneralRe: Heap corruption. Pin
Polite Programmer3-Jun-07 20:00
Polite Programmer3-Jun-07 20:00 
Reason for using malloc() stuff is that I want to be pure in C.

What I am doing?

1.... I want to write a pure C control to teach myself internals of Win32 Message Architecture.
2... When done with this, I will burry that control (a simple button control yet...) into a DLL.
3... When done with the DLL, I will try to write a COM wrapper around it that too in pure C to check whether it can be used with VB, and .NET.

Friends one more thing... my every malloc() is having a free() but still facing problem. And one of our felllows is very much right, the problem only occurs in Debug sessions.

Here is the source code.

Please consider the following functions specially.... Are they correct?

a) AllocateButtonInfoFor()
b) DeleteButtonInfo()
c) OnNCCreate()
d) OnSetText()

First is the ButtonControl.h
============================

#ifndef __BUTTON_CONTROL_H__<br />
#define __BUTTON_CONTROL_H__<br />
#include "stdafx.h"<br />
<br />
#define BUTTON_CONTROL_CLASS			TEXT("BUTTON_CONTROL_safsf")<br />
<br />
<br />
#ifdef __INCLUDE_BUTTONCONTROL_PRIVATES__<br />
<br />
/* Depicts the state of the button */<br />
typedef enum tagBUTTONSTATE<br />
{<br />
	NORMAL = 2,<br />
	HOVER,<br />
	PRESSED,<br />
	DISABLED<br />
<br />
} BUTTONSTATE;<br />
<br />
/* Structure to hold the information of the button*/<br />
typedef struct tagBUTTONINFO<br />
{<br />
//	HWND hWnd;							/* Handle of the button itself */<br />
	TCHAR * szText;						/* The caption of button */<br />
	int nTextLength;					/* Length of caption */<br />
	HPEN borderColor;					/* The outline color */<br />
	COLORREF textColor;					/* The Text Color of the button */<br />
	HBRUSH backColor;					/* The background color of the button */<br />
	HFONT font;							/* The font of the text */<br />
	HPEN borderColorPressed;			/* The border color in pressed form*/<br />
	COLORREF textColorPressed;			/* The text color in pressed form */<br />
	HBRUSH backColorPressed;			/* The back color in pressed form */<br />
	HFONT fontPressed;					/* The font in pressed form */<br />
	HPEN borderColorHover;				/* The border color in hover form*/<br />
	COLORREF textColorHover;			/* The text color in hover form */<br />
	HBRUSH backColorHover;				/* The back color in hover form */<br />
	HFONT fontHover;					/* The font in hover form */<br />
<br />
	BUTTONSTATE state;					/* The state of the button */<br />
<br />
} BUTTONINFO, * PBUTTONINFO;<br />
<br />
/* ButtonInfo Allocation */<br />
//HANDLE	CreateHeapIfNeeded();<br />
//BOOL	DestroyHeapIfNeeded(HANDLE);<br />
<br />
PBUTTONINFO AllocateButtonInfoFor(HWND hWnd);<br />
PBUTTONINFO PickButtonInfo(HWND hWnd);<br />
BOOL DropButtonInfo(HWND hWnd);<br />
void DeleteButtonInfo(HWND hWnd);<br />
void SetButtonDefaults(HWND hWnd, PBUTTONINFO pButtonInfo);<br />
<br />
void DrawButton(HWND hWnd, PBUTTONINFO pButtonInfo, HDC hdc);<br />
<br />
/* The window procedure. */<br />
LRESULT CALLBACK ButtonProc(HWND, UINT, WPARAM, LPARAM);<br />
<br />
/* The Button Event Handlers */<br />
<br />
<br />
BOOL	OnNCCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);<br />
void	OnNCDestroy(HWND);<br />
void	OnPaint(HWND);<br />
void	OnInitButton(HWND hWnd);<br />
void	OnMouseButtonDown(HWND hWnd, int x, int y);<br />
void	OnMouseButtonUp(HWND hWnd, int x, int y);<br />
INT		OnGetText(HWND, int, LPTSTR);<br />
INT		OnGetTextLength(HWND);<br />
BOOL	OnSetText(HWND hWnd, LPCTSTR);<br />
<br />
//void	OnDestroy(HWND);<br />
/*<br />
BOOL	OnCreate(HWND, LPCREATESTRUCT);<br />
<br />
<br />
void	OnEnable(HWND, BOOL);<br />
<br />
<br />
<br />
HFONT	OnGetFont(HWND);<br />
<br />
void	OnSetFont(HWND hWnd, HFONT hFont, BOOL fRedraw);<br />
void	OnMouseButtonDown(HWND, int nMask, int x, int y);<br />
void	OnMouseButtonUp(HWND, int nMask, int x, int y);<br />
*/<br />
#endif /* __INCLUDE_BUTTONCONTROL_PRIVATES__ */<br />
<br />
// The Button Messages used to set and get properties.<br />
<br />
#define BCM_SETBORDERPEN				WM_USER + 10	/* WPARAM	= HPEN			*/<br />
#define BCM_GETBORDERPEN				WM_USER + 11	/* Return	= HPEN			*/<br />
#define BCM_SETCAPTIONCOLOR				WM_USER + 12	/* WPARAM	= COLORREF		*/<br />
#define BCM_GETCAPTIONCOLOR				WM_USER + 13	/* Return	= COLORREF		*/<br />
#define BCM_SETBACKGNDBRUSH				WM_USER + 14	/* WPARAM	= HBRUSH		*/<br />
#define BCM_GETBACKGNDBRUSH				WM_USER + 15	/* Return	= HBRUSH		*/<br />
#define BCM_SETBUTTONSTATE				WM_USER + 16	/* WPARAM	= BUTTONSTATE	*/<br />
#define BCM_GETBUTTONSTATE				WM_USER + 17	/* Return	= BUTTONSTATE	*/<br />
#define BCM_INITBUTTON					WM_USER + 18	/* NULL */<br />
<br />
// Initializes the button control<br />
HRESULT InitButtonControl(HINSTANCE hInstance);<br />
#endif /*__BUTTON_CONTROL_H__*/



Second is ButtonControl.cpp
===========================

#include "stdafx.h"<br />
#define __INCLUDE_BUTTONCONTROL_PRIVATES__		/* So that we may have private functions. */<br />
#include "ButtonControl.h"<br />
<br />
//static ULONG gButtonCount = 0;<br />
//static HANDLE ghHeap = NULL;<br />
<br />
/***********************************************************************\<br />
FUNCTION:			InitButtonControl<br />
PURPOSE:			Registers the button class<br />
ARGS:				HINSTANCE hInstance<br />
RETURN:				The result of the registration<br />
\***********************************************************************/<br />
HRESULT InitButtonControl(HINSTANCE hInstance)<br />
{<br />
	WNDCLASSEX wc;<br />
<br />
	wc.cbClsExtra			= 0;<br />
	wc.cbSize				= sizeof(WNDCLASSEX);<br />
	wc.cbWndExtra			= 0;<br />
	wc.hbrBackground		= (HBRUSH)(COLOR_WINDOW + 1);<br />
	wc.hCursor				= LoadCursor(NULL, IDC_ARROW);<br />
	wc.hIcon				= LoadIcon(NULL, IDI_APPLICATION);<br />
	wc.hIconSm				= LoadIcon(NULL, IDI_APPLICATION);<br />
	wc.hInstance			= hInstance;<br />
	wc.lpfnWndProc			= ButtonProc;<br />
	wc.lpszClassName		= BUTTON_CONTROL_CLASS;<br />
	wc.lpszMenuName			= NULL;<br />
	wc.style				= CS_HREDRAW | CS_VREDRAW |<br />
								CS_GLOBALCLASS;<br />
	<br />
	//HeapCreate(HC<br />
	<br />
	//DWORD dwErr = GetLastError();<br />
	return RegisterClassEx(&wc);<br />
}<br />
<br />
<br />
<br />
/***********************************************************************\<br />
FUNCTION:			AllocateButtonInfoFor<br />
PURPOSE:			Allocate and attaches the BUTTONINFO structure<br />
ARGS:				Handle to the button<br />
RETURN:				Pointer to allocated BUTTONINFO structure<br />
\***********************************************************************/<br />
PBUTTONINFO AllocateButtonInfoFor(HWND hWnd)<br />
{<br />
	if(!hWnd)<br />
		return NULL;<br />
<br />
	//PBUTTONINFO hMem = (PBUTTONINFO) GlobalAlloc(GMEM_FIXED, sizeof(BUTTONINFO));<br />
	PBUTTONINFO hMem = (PBUTTONINFO) malloc(sizeof(BUTTONINFO));<br />
	//PBUTTONINFO hMem = new BUTTONINFO();<br />
<br />
	if(!hMem)<br />
		return NULL;<br />
<br />
	SetWindowLong(hWnd, GWL_USERDATA, (LONG)hMem);<br />
<br />
	SetButtonDefaults(hWnd, hMem);<br />
<br />
	return hMem;<br />
<br />
<br />
}<br />
<br />
<br />
/***********************************************************************\<br />
FUNCTION:			PickButtonInfo<br />
PURPOSE:			Extracts the BUTTONINFO structure from the button<br />
ARGS:				Handle to the button<br />
RETURN:				Pointer to the extracted BUTTONINFO Structure<br />
\***********************************************************************/<br />
PBUTTONINFO PickButtonInfo(HWND hWnd)<br />
{<br />
	if(!hWnd)<br />
		return NULL;<br />
<br />
	PBUTTONINFO hMem = (PBUTTONINFO) GetWindowLong(hWnd, GWL_USERDATA);<br />
<br />
	if(!hMem)<br />
		return NULL;<br />
<br />
	//PBUTTONINFO pButtonInfo = (PBUTTONINFO) GlobalLock(hMem);<br />
<br />
	DWORD dw = GetLastError();<br />
<br />
	return hMem;<br />
<br />
<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			DropButtonInfo<br />
PURPOSE:			Locks the BUTTONINFO structure of the given control<br />
ARGS:				Handle to the control<br />
RETURN:				Succuss status of the operation<br />
\***********************************************************************/<br />
BOOL DropButtonInfo(HWND hWnd)<br />
{<br />
	if(!hWnd)<br />
		return FALSE;<br />
<br />
	PBUTTONINFO hMem = (PBUTTONINFO)GetWindowLong(hWnd, GWL_USERDATA);<br />
<br />
	if(!hMem)<br />
		return FALSE;<br />
<br />
	return TRUE;<br />
<br />
<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			DeleteButtonInfo<br />
PURPOSE:			Deletes the BUTTONINFO structure<br />
ARGS:				Handle to the button structure<br />
RETURN:				None<br />
\***********************************************************************/<br />
void DeleteButtonInfo(HWND hWnd)<br />
{<br />
	if(!hWnd)<br />
		return;<br />
<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
	<br />
	if(!pButtonInfo)<br />
		return;<br />
<br />
	DeleteObject((HGDIOBJ)pButtonInfo->backColor);<br />
	DeleteObject((HGDIOBJ)pButtonInfo->borderColor);<br />
	DeleteObject((HGDIOBJ)pButtonInfo->font);<br />
	//GlobalFree((HGLOBAL)pButtonInfo->szText);<br />
	//_free_dbg((void *) pButtonInfo->szText, _NORMAL_BLOCK);<br />
	//delete [] pButtonInfo->szText;<br />
<br />
	if(pButtonInfo->szText != NULL)<br />
		free((void *)pButtonInfo->szText);<br />
	<br />
<br />
	DeleteObject((HGDIOBJ)pButtonInfo->backColorPressed);<br />
	DeleteObject((HGDIOBJ)pButtonInfo->borderColorPressed);<br />
	DeleteObject((HGDIOBJ)pButtonInfo->fontPressed);<br />
<br />
<br />
	//GlobalFree((HGLOBAL)GetWindowLong(hWnd, GWL_USERDATA));<br />
	void * hMem = (void *) GetWindowLong(hWnd, GWL_USERDATA);<br />
<br />
	if(hMem != NULL)<br />
		free((void *)GetWindowLong(hWnd, GWL_USERDATA));<br />
	//delete (PBUTTONINFO)GetWindowLong(hWnd, GWL_USERDATA);<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			SetButtonDefaults<br />
PURPOSE:			Sets the default values for the button<br />
ARGS:				Handle to button<br />
RETURN:				None<br />
\***********************************************************************/<br />
void SetButtonDefaults(HWND hWnd, PBUTTONINFO pButtonInfo)<br />
{<br />
	//PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	if(!pButtonInfo)<br />
		return;<br />
<br />
	//HWND hWnd = pButtonInfo->hWnd;<br />
<br />
	ZeroMemory((PVOID)pButtonInfo, sizeof(BUTTONINFO));<br />
<br />
	pButtonInfo->backColor		= (HBRUSH) CreateSolidBrush(RGB(123,123,123));<br />
	pButtonInfo->borderColor	= (HPEN) CreatePen(PS_SOLID, 1, RGB(200,10,19));<br />
	pButtonInfo->font			= (HFONT) GetStockFont(ANSI_VAR_FONT);<br />
	pButtonInfo->textColor		= RGB(255, 255, 255);<br />
	pButtonInfo->state			= NORMAL;<br />
	pButtonInfo->szText			= NULL;<br />
	//lstrcpy(pButtonInfo->szText, TEXT("Flat Button"));<br />
	pButtonInfo->nTextLength	= 0;<br />
<br />
	pButtonInfo->backColorPressed	= CreateSolidBrush(RGB(200,180,150));<br />
	pButtonInfo->borderColorPressed	= (HPEN) GetStockObject(BLACK_PEN);<br />
	pButtonInfo->fontPressed		= (HFONT)GetStockObject(ANSI_VAR_FONT);<br />
	pButtonInfo->textColorPressed	= RGB(0,0,0);<br />
	<br />
<br />
	/* Set the background color */<br />
//	pButtonInfo->hWnd;<br />
	//SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)pButtonInfo->backColor);<br />
<br />
	//DropButtonInfo(hWnd);<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			DrawButton<br />
PURPOSE:			Draws the button according to the button status<br />
ARGS:				HWND: Handle to the button<br />
					HDC Device Context. If NULL, obtained automatically.<br />
					But in case of WM_PAINT, it should not be null.<br />
RETURN:				None<br />
\***********************************************************************/<br />
void DrawButton(HWND hWnd, PBUTTONINFO pButtonInfo, HDC hdc)<br />
{<br />
	static TCHAR szBuffer[256];<br />
<br />
	if(!pButtonInfo)<br />
		return;<br />
<br />
	//HWND hWnd = pButtonInfo->hWnd;<br />
<br />
	if(!IsWindow(hWnd))<br />
		return;<br />
<br />
	<br />
	HDC hDC = NULL;<br />
<br />
	if(hdc == NULL)<br />
		hDC = GetDC(hWnd);<br />
	else<br />
		hDC = hdc;<br />
<br />
	// PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	RECT rect;<br />
<br />
	GetClientRect(hWnd, &rect);<br />
	<br />
	if(pButtonInfo->state == NORMAL)<br />
	{<br />
		/* Draw Normal */<br />
			/* Draw Hover State */<br />
		<br />
		SelectObject(hDC, (HGDIOBJ)pButtonInfo->backColor);<br />
		SelectObject(hDC, (HGDIOBJ)pButtonInfo->borderColor);<br />
		SetTextColor(hDC, pButtonInfo->textColor);<br />
		SetBkMode(hDC, TRANSPARENT);<br />
<br />
		/* Fill the rectangle with the background color */<br />
		FillRect(hDC, &rect, pButtonInfo->backColor);<br />
<br />
<br />
	}<br />
<br />
	if(pButtonInfo->state == HOVER)<br />
	{<br />
	<br />
	}<br />
<br />
	if(pButtonInfo->state == PRESSED)<br />
	{<br />
		// Draw Pressed<br />
		SelectObject(hDC, (HGDIOBJ)pButtonInfo->backColorPressed);<br />
		SelectObject(hDC, (HGDIOBJ)pButtonInfo->borderColorPressed);<br />
		SetTextColor(hDC, pButtonInfo->textColorPressed);<br />
		SetBkMode(hDC, TRANSPARENT);<br />
<br />
		FillRect(hDC, &rect, pButtonInfo->backColorPressed);<br />
	}<br />
<br />
	if(pButtonInfo->state == DISABLED)<br />
	{<br />
		// Draw Disabled<br />
	}<br />
<br />
		<br />
<br />
		Rectangle(hDC, rect.left, rect.top, rect.right, rect.bottom);<br />
		<br />
		<br />
		//int i = GetWindowText(hWnd, szBuffer, 256);<br />
<br />
		<br />
		DrawText(hDC, pButtonInfo->szText,<br />
					  pButtonInfo->nTextLength,<br />
					  &rect,<br />
					  DT_SINGLELINE | DT_VCENTER | DT_CENTER);<br />
<br />
		<br />
<br />
		/*<br />
		DrawText(hDC, szBuffer,<br />
					  i,<br />
					  &rect,<br />
					  DT_SINGLELINE | DT_VCENTER | DT_CENTER);<br />
	   */<br />
<br />
<br />
<br />
	if(hdc == NULL)<br />
		ReleaseDC(hWnd, hDC);<br />
<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			ButtonProc<br />
PURPOSE:			Handles the messages for the button<br />
ARGS:				Same as to WndProc<br />
<br />
HANDLES:<br />
WM_PAINT<br />
<br />
<br />
WM_ENABLE<br />
<br />
WM_CREATE<br />
WM_NCCREATE<br />
WM_NCDESTROY<br />
<br />
WM_SIZE<br />
WM_MOVE<br />
WM_MOVING<br />
WM_WINDOWPOSCHANGED<br />
WM_WINDOWPOSCHANGING<br />
<br />
WM_LBUTTONDOWN<br />
WM_LBUTTONUP<br />
<br />
WM_GETTEXT<br />
WM_SETTEXT<br />
WM_GETTEXTLENGH<br />
WM_SETFONT<br />
WM_GETFONT<br />
WM_GETMINMAXINFO<br />
<br />
BCM_SETBORDERPEN<br />
BCM_GETBORDERPEN<br />
BCM_SETCAPTIONCOLOR<br />
BCM_GETCAPTIONCOLOR<br />
BCM_SETBACKGNDBRUSH<br />
BCM_GETBACKGNDBRUSH<br />
BCM_SETBUTTONSTATE<br />
BCM_GETBUTTONSTATE<br />
<br />
\***********************************************************************/<br />
LRESULT CALLBACK ButtonProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)<br />
{<br />
	switch(msg)<br />
	{<br />
		//HANDLE_WM_NCCREATE(hWnd, wParam, lParam, OnNCCreate);<br />
	/*=======================================================<br />
			WM_NCCREATE<br />
	  ======================================================*/<br />
	case WM_NCCREATE:<br />
		{<br />
			return (LRESULT) OnNCCreate(hWnd, (LPCREATESTRUCT)lParam);<br />
		}<br />
<br />
	case WM_CREATE:<br />
		{<br />
			PostMessage(hWnd, BCM_INITBUTTON, 0, 0);<br />
			return TRUE;<br />
		}<br />
<br />
	case BCM_INITBUTTON:<br />
		{<br />
			//OnInitButton(hWnd);<br />
			return 0;<br />
		}<br />
	/*=======================================================<br />
			WM_NCDESTROY<br />
	  ======================================================*/<br />
	case WM_DESTROY:<br />
		{<br />
			OnNCDestroy(hWnd);<br />
			return 0;<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_PAINT<br />
	 ======================================================*/<br />
	case WM_PAINT:<br />
		{<br />
			OnPaint(hWnd);<br />
			/*<br />
			PAINTSTRUCT ps;<br />
			HDC hdc = BeginPaint(hWnd, &ps);<br />
			RECT rect;<br />
			GetClientRect(hWnd, &rect);<br />
			Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);<br />
			EndPaint(hWnd, &ps);<br />
			*/<br />
			return 0;<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_LBUTTONDOWN<br />
	 ======================================================*/<br />
	case WM_LBUTTONDOWN:<br />
	case WM_RBUTTONDOWN:<br />
		{<br />
			OnMouseButtonDown(hWnd, LOWORD(lParam), HIWORD(lParam));<br />
			return 0;<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_LBUTTONUP<br />
	 ======================================================*/<br />
	case WM_LBUTTONUP:<br />
	case WM_RBUTTONUP:<br />
		{<br />
			OnMouseButtonUp(hWnd, LOWORD(lParam), HIWORD(lParam));<br />
			return 0;<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_GETTEXT<br />
	 ======================================================*/<br />
	case WM_GETTEXT:<br />
		{<br />
			return OnGetText(hWnd, (int)wParam, (LPTSTR)lParam);<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_GETTEXTLENGTH<br />
	 ======================================================*/<br />
	case WM_GETTEXTLENGTH:<br />
		{<br />
			return OnGetTextLength(hWnd);<br />
		}<br />
<br />
	/*=======================================================<br />
		WM_SETTEXT<br />
	 ======================================================*/<br />
	case WM_SETTEXT:<br />
		{<br />
			return OnSetText(hWnd, (LPCTSTR)lParam);<br />
		}<br />
<br />
	<br />
/*		<br />
		HANDLE_WM_CREATE(hWnd, wParam, lParam, OnCreate);<br />
		HANDLE_WM_DESTROY(hWnd, wParam, lParam, OnDestroy);<br />
		HANDLE_WM_NCDESTROY(hWnd, wParam, lParam, OnNCDestroy);<br />
<br />
		HANDLE_WM_PAINT(hWnd, wParam, lParam, OnPaint);<br />
<br />
		HANDLE_WM_ENABLE(hWnd, wParam,lParam, OnEnable);<br />
<br />
		<br />
		HANDLE_WM_GETTEXT(hWnd, wParam, lParam, OnGetText);<br />
		HANDLE_WM_GETTEXTLENGTH(hWnd, wParam, lParam, OnGetTextLength);<br />
		HANDLE_WM_GETFONT(hWnd, wParam, lParam, OnGetFont);<br />
<br />
		HANDLE_WM_SETTEXT(hWnd, wParam, lParam, OnSetText);<br />
		HANDLE_WM_SETFONT(hWnd, wParam, lParam, OnSetFont);<br />
<br />
		//HANDLE_WM_LBUTTONDOWN(hWnd, wParam, lParam, OnMouseButtonDown);<br />
		//HANDLE_WM_LBUTTONUP(hWnd, wParam, lParam, OnMouseButtonUp);<br />
		*/<br />
<br />
	}<br />
	<br />
	return DefWindowProc(hWnd, msg, wParam, lParam);<br />
}<br />
<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnNCCreate<br />
PURPOSE:			Intializes the control for the first time<br />
ARGS:				HWND: Handle to the button<br />
					LPCREATESTRUCT pointer<br />
RETURN:				TRUE if succuss FALSE otherwise.<br />
\***********************************************************************/<br />
BOOL OnNCCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)<br />
{<br />
	PBUTTONINFO pButtonInfo = AllocateButtonInfoFor(hWnd);<br />
	<br />
	if(!pButtonInfo)<br />
		return FALSE;<br />
<br />
	int iLen = lstrlen(lpCreateStruct->lpszName);<br />
<br />
	if(iLen > pButtonInfo->nTextLength)<br />
	{<br />
		if(pButtonInfo->szText != NULL)<br />
			free((void *)pButtonInfo->szText);<br />
		pButtonInfo->szText = (TCHAR *) malloc(iLen + 2);<br />
		//pButtonInfo->szText = new TCHAR[iLen + 1];<br />
		<br />
		if(pButtonInfo->szText == NULL)<br />
			return FALSE;<br />
	}<br />
<br />
	pButtonInfo->nTextLength = iLen;<br />
	ZeroMemory((LPVOID)pButtonInfo->szText, pButtonInfo->nTextLength);<br />
	lstrcpy(pButtonInfo->szText, lpCreateStruct->lpszName);<br />
<br />
	<br />
<br />
	return TRUE;<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnNCDestroy<br />
PURPOSE:			De-allocates the BUTTONINFO struct<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
void OnNCDestroy(HWND hWnd)<br />
{<br />
	DeleteButtonInfo(hWnd);<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnPaint<br />
PURPOSE:			Paints the control<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
void OnPaint(HWND hWnd)<br />
{<br />
	PAINTSTRUCT ps;<br />
<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
	HDC hdc = BeginPaint(hWnd, &ps);<br />
	<br />
	//pButtonInfo->state = (BUTTONSTATE)2;<br />
	DrawButton(hWnd, pButtonInfo, hdc);<br />
	EndPaint(hWnd, &ps);<br />
<br />
	//DropButtonInfo(hWnd);<br />
	<br />
}<br />
<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnMouseButtonDown<br />
PURPOSE:			Changes the button state<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
void OnMouseButtonDown(HWND hWnd, int x, int y)<br />
{<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	if(!pButtonInfo)<br />
		return;<br />
<br />
	pButtonInfo->state = PRESSED;<br />
<br />
	InvalidateRect(hWnd, NULL, TRUE);<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnMouseButtonUp<br />
PURPOSE:			Changes the state of the button<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
void OnMouseButtonUp(HWND hWnd, int x, int y)<br />
{<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	if(!pButtonInfo)<br />
		return;<br />
<br />
	pButtonInfo->state = NORMAL;<br />
	InvalidateRect(hWnd, NULL, TRUE);<br />
	<br />
	HWND hParent = GetParent(hWnd);<br />
<br />
	if(!hParent)<br />
		return;<br />
<br />
	UINT nID = GetWindowLong(hWnd, GWL_ID);<br />
<br />
	SendMessage(hParent, WM_COMMAND, MAKEWPARAM(nID, BN_CLICKED), (LONG)hWnd);<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnGetText<br />
PURPOSE:			Gets the text of the control<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
INT OnGetText(HWND hWnd, int nMaxLen, LPTSTR lpszBuffer)<br />
{<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	if(!pButtonInfo)<br />
		return -1;<br />
<br />
	<br />
	int i = 0;<br />
<br />
//	while(i < nMaxLen && i < pButtonInfo->nTextLength)<br />
//		lpszBuffer[i] = pButtonInfo->szText[i++];<br />
<br />
	lstrcpy(lpszBuffer, pButtonInfo->szText);<br />
//	lpszBuffer[i] = '\0';<br />
<br />
	return i;<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnGetTextLength<br />
PURPOSE:			Gets the length of text of the control<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
INT OnGetTextLength(HWND hWnd)<br />
{<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	if(!pButtonInfo)<br />
		return -1;<br />
<br />
	return pButtonInfo->nTextLength;<br />
}<br />
<br />
/***********************************************************************\<br />
FUNCTION:			OnSetText<br />
PURPOSE:			Sets the of text of the control<br />
ARGS:				HWND: Handle to the button<br />
RETURN:				None<br />
\***********************************************************************/<br />
<br />
BOOL OnSetText(HWND hWnd, LPCTSTR lpszText)<br />
{<br />
	PBUTTONINFO pButtonInfo = PickButtonInfo(hWnd);<br />
<br />
	int iLen = lstrlen(lpszText);<br />
<br />
	if(iLen > pButtonInfo->nTextLength)<br />
	{<br />
		if(pButtonInfo->szText != NULL)<br />
			free((void *)pButtonInfo->szText);<br />
<br />
		pButtonInfo->szText = (TCHAR *) malloc(iLen + 2);<br />
<br />
		//pButtonInfo->szText = new TCHAR[iLen + 1];<br />
<br />
		if(pButtonInfo->szText == NULL)<br />
			return FALSE;<br />
	}<br />
<br />
	pButtonInfo->nTextLength = iLen;<br />
	ZeroMemory((LPVOID)pButtonInfo->szText, pButtonInfo->nTextLength);<br />
	lstrcpy(pButtonInfo->szText, lpszText);<br />
	<br />
	InvalidateRect(hWnd, NULL, TRUE);<br />
	return TRUE;<br />
<br />
}


Polite Programmer


More Object Oriented then C#

GeneralRe: Heap corruption. Pin
Polite Programmer3-Jun-07 20:01
Polite Programmer3-Jun-07 20:01 
GeneralRe: Heap corruption. Pin
Stephen Hewitt3-Jun-07 20:05
Stephen Hewitt3-Jun-07 20:05 
Questionhelp with fstream Pin
anbluemoon3-Jun-07 1:24
anbluemoon3-Jun-07 1:24 
AnswerRe: help with fstream Pin
CPallini3-Jun-07 4:02
mveCPallini3-Jun-07 4:02 
QuestionDLL and EXE files Pin
prithaa3-Jun-07 0:43
prithaa3-Jun-07 0:43 
AnswerRe: DLL and EXE files Pin
Christian Graus3-Jun-07 1:14
protectorChristian Graus3-Jun-07 1:14 
GeneralRe: DLL and EXE files Pin
Rajesh R Subramanian3-Jun-07 20:43
professionalRajesh R Subramanian3-Jun-07 20:43 
QuestionRe: DLL and EXE files Pin
Matthew Faithfull3-Jun-07 1:18
Matthew Faithfull3-Jun-07 1:18 
AnswerRe: DLL and EXE files Pin
prithaa3-Jun-07 1:42
prithaa3-Jun-07 1:42 
GeneralRe: DLL and EXE files Pin
Matthew Faithfull3-Jun-07 2:59
Matthew Faithfull3-Jun-07 2:59 
AnswerRe: DLL and EXE files Pin
Hamid_RT3-Jun-07 7:33
Hamid_RT3-Jun-07 7:33 
QuestionHow do I make a child dialog draw on a different HDC Pin
KellyR2-Jun-07 17:51
KellyR2-Jun-07 17:51 
AnswerRe: How do I make a child dialog draw on a different HDC Pin
Naveen3-Jun-07 16:17
Naveen3-Jun-07 16:17 
Questionagain for InterlockedIncrement() Pin
includeh102-Jun-07 16:52
includeh102-Jun-07 16:52 
AnswerRe: again for InterlockedIncrement() Pin
Matthew Faithfull3-Jun-07 1:07
Matthew Faithfull3-Jun-07 1:07 
GeneralRe: again for InterlockedIncrement() Pin
Mark Salsbery4-Jun-07 5:40
Mark Salsbery4-Jun-07 5:40 
AnswerRe: again for InterlockedIncrement() Pin
Mark Salsbery4-Jun-07 5:45
Mark Salsbery4-Jun-07 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.