Click here to Skip to main content
15,907,905 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIn eVC how to make FullScreen in PocketPC ? Pin
subang1-Jan-02 20:51
subang1-Jan-02 20:51 
AnswerRe: In eVC how to make FullScreen in PocketPC ? Pin
NormDroid1-Jan-02 21:15
professionalNormDroid1-Jan-02 21:15 
GeneralMaking an Image Transparent Pin
clemence1-Jan-02 19:52
clemence1-Jan-02 19:52 
GeneralRe: Making an Image Transparent Pin
Christian Graus2-Jan-02 0:19
protectorChristian Graus2-Jan-02 0:19 
GeneralOwner drawn button flicker hell Pin
Christian Graus1-Jan-02 18:19
protectorChristian Graus1-Jan-02 18:19 
GeneralRe: Owner drawn button flicker hell Pin
Rick Dangerous2-Jan-02 0:30
Rick Dangerous2-Jan-02 0:30 
GeneralRe: Owner drawn button flicker hell Pin
Christian Graus2-Jan-02 0:52
protectorChristian Graus2-Jan-02 0:52 
Generalmalloc() question Pin
1-Jan-02 17:26
suss1-Jan-02 17:26 
Hello fellow CPians. Happy New Year!

On my break from school, I thought I'd torture myself be reading Charles Petzold's Programming Windows. It's actually been a great read until now. Cry | :((

One of the sample programs declares a PMSG variable. The code goes on to allocate enough memory to store the appropriate amount of space using;

pmsg = malloc (cLinesMax * sizeof (MSG));


This code does not compile is VC6, SP5. I get the following error;

error C2440: '=' : cannot convert from 'void *' to 'struct tagMSG *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast


I've tried casting the return value to PMSG which solve the compile problem but the program bombs! Frown | :( I'm in way over my head on this one... Confused | :confused:

Any help that anyone can provide is greatly appreciated.

Sorry for the size of the post, but here's the complete code.

/*------------------------------------------------------------------------
	keyview1.cpp -- Displays keyboard and character messages
                        Paul Lyons, December 30, 2001
                       (C) Charles Petzold, Programming Windows
  ----------------------------------------------------------------------*/

/*
 *	Inculdes
 */
#include <windows.h>

/*
 *	Function Prototypes
 */
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

/*
 *	Program entry point
 */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{
	/*
	 *	Variable declarations
	 */
	static TCHAR szAppName[] = TEXT("KeyView1");
	HWND         hwnd;
	MSG          msg;
	WNDCLASS     wndclass;

	/*
	 *	Define wndclass
	 */
	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wndclass.lpszMenuName  = NULL;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszClassName = szAppName;

	/*
	 *	Register window class
	 */
	if (!RegisterClass (&wndclass))
	{
		MessageBox(NULL, TEXT("This application requires WinNT to run."),
                             szAppName, MB_ICONERROR);
		return 0;
	}

	/*
	 *	Create window
	 */
	hwnd = CreateWindow(szAppName, TEXT("Keyboard Message Viewer #1"),
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, CW_USEDEFAULT,
                             CW_USEDEFAULT, CW_USEDEFAULT,
                             NULL, NULL, hInstance, NULL);

	/*
	 *	Display window
	 */
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	/*
	 *	Message loop
	 */
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

/************************************************************************/
/*                            WndProc                                   */
/************************************************************************/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                         WPARAM wParam, LPARAM lParam)
{
	static int   cxClientMax, cyClientMax;
	static int   cxClient, cyClient;
	static int   cxChar, cyChar;
	static int   cLinesMax, cLines;
	static PMSG  pmsg;
	static RECT  rectScroll;
	static TCHAR szTop[] = TEXT("Message            Key         Char     ")
                                 TEXT("    Repeat Scan Ext ALT  Prev  Tran");
	static TCHAR szUnd[] = TEXT("-------            ---         ----     ")
                               TEXT("    ------ ---- --- ---  ----  ----");

	static TCHAR* szFormat[2] = {

		TEXT("%-13s %3d %-15s%c%6u %4d %3s %3s %4s %4s"),
		TEXT("%-13s            0x%04x%1s%c %6u %4d %3s %3s %4s %4s")	};
	
	static TCHAR* szYes  = TEXT("Yes");
	static TCHAR* szNo   = TEXT("No");
	static TCHAR* szDown = TEXT("Down");
	static TCHAR* szUp   = TEXT("Up");

	static TCHAR* szMessage[] = {TEXT("WM_KEYDOWN"),    TEXT("WM_KEYUP"),
                                      TEXT("WM_CHAR"),       TEXT("WM_DEADCHAR"),
                                      TEXT("WM_SYSKEYDOWN"), TEXT("WM_SYSKEYUP"),
                                      TEXT("WM_SYSCHAR"),    TEXT("WM_SYSDEADCHAR") };


	HDC         hdc;
	int         i, iType;
	PAINTSTRUCT ps;
	TCHAR       szBuffer[128], szKeyName[32];
	TEXTMETRIC  tm;

	switch (message)
	{
	case WM_CREATE:
	case WM_DISPLAYCHANGE:

	        // Get the maximum size of the client area
	        cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
	        cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);

	        // Get character size for fixed pitch font
	        hdc = GetDC(hwnd);

	        SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
		GetTextMetrics(hdc, &tm);
		cxChar = tm.tmAveCharWidth;
		cyChar = tm.tmHeight;

		ReleaseDC(hwnd, hdc);

		// Allocate memory for display lines
		if (pmsg)
			free (pmsg);

		cLinesMax = cyClientMax / cyChar;
		pmsg = malloc (cLinesMax * sizeof (MSG));
		cLines = 0;

		// Fall through...

	case WM_SIZE:
		if (message == WM_SIZE)
		{
			cxClient = LOWORD(lParam);
			cyClient = HIWORD(lParam);
		}

		// Calculate the scrolling rectangle
		rectScroll.left = 0;
		rectScroll.right = cxClient;
		rectScroll.top = cyChar;
		rectScroll.bottom = cyChar * (cyClient / cyChar);

		InvalidateRect(hwnd, NULL, TRUE);
		return 0;

	case WM_KEYDOWN:
	case WM_KEYUP:
	case WM_CHAR:
	case WM_DEADCHAR:
	case WM_SYSKEYDOWN:
	case WM_SYSKEYUP:
	case WM_SYSCHAR:
	case WM_SYSDEADCHAR:

		// Rearrange the storage array
		for (i = cLinesMax - 1; i > 0; i--)
		{
			pmsg[i] = pmsg[i - 1];
		}

		// Store new message
		pmsg[0].hwnd	 = hwnd;
		pmsg[0].message = message;
		pmsg[0].wParam  = wParam;
		pmsg[0].lParam  = lParam;

		cLines = min(cLines + 1, cLinesMax);

		// Scroll up the display
		ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);

		// Call the DefWindowProc so system messages work
		break;


	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
		SetBkMode(hdc, TRANSPARENT);
		TextOut(hdc, 0, 0, szTop, lstrlen(szTop));
		TextOut(hdc, 0, 10, szUnd, lstrlen(szUnd));

		for (i = 0; i < min(cLines, cyClient / cyChar - 1); i++)
		{
			iType = pmsg[i].message == WM_CHAR ||
				 pmsg[i].message == WM_SYSCHAR ||
				 pmsg[i].message == WM_DEADCHAR ||
				 pmsg[i].message == WM_SYSDEADCHAR;

			GetKeyNameText(pmsg[i].lParam, szKeyName, sizeof(szKeyName) / sizeof(TCHAR));

			TextOut(hdc, 0, (cyClient / cyChar - 1 - i) * cyChar, szBuffer,
			wsprintf(szBuffer, szFormat[iType],
                         szMessage[pmsg[i].message - WM_KEYFIRST],
                         (PSTR) (iType ? TEXT(" ") : szKeyName),
                         (TCHAR) (iType ? pmsg[i].wParam : ' '),
                         LOWORD (pmsg[i].lParam),
                         HIWORD (pmsg[i].lParam) & 0xFF,
                         0x10000000 & pmsg[i].lParam ? szYes : szNo,
                         0x20000000 & pmsg[i].lParam ? szYes : szNo,
                         0x40000000 & pmsg[i].lParam ? szDown : szUp,
                         0x80000000 & pmsg[i].lParam ? szUp : szDown));
		}
		EndPaint(hwnd, &ps);
		return 0;


	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}


Paul Lyons

Do not go where the path may lead, go instead where there is no path and leave a trail.
- Ralph Waldo Emerson
GeneralRe: malloc() question Pin
Philip Patrick1-Jan-02 19:14
professionalPhilip Patrick1-Jan-02 19:14 
GeneralRe: malloc() question Pin
1-Jan-02 19:15
suss1-Jan-02 19:15 
GeneralRe: malloc() question Pin
Philip Patrick1-Jan-02 19:23
professionalPhilip Patrick1-Jan-02 19:23 
Generalremove popup menu arrow Pin
1-Jan-02 15:35
suss1-Jan-02 15:35 
GeneralProblem with Multithreads Pin
Mustafa Demirhan1-Jan-02 6:06
Mustafa Demirhan1-Jan-02 6:06 
GeneralRe: Problem with Multithreads Pin
Mustafa Demirhan1-Jan-02 6:12
Mustafa Demirhan1-Jan-02 6:12 
GeneralRe: Problem with Multithreads Pin
1-Jan-02 6:32
suss1-Jan-02 6:32 
GeneralRe: Problem with Multithreads Pin
1-Jan-02 6:56
suss1-Jan-02 6:56 
GeneralRe: Problem with Multithreads Pin
Todd Smith1-Jan-02 7:35
Todd Smith1-Jan-02 7:35 
GeneralRe: Problem with Multithreads Pin
Mustafa Demirhan1-Jan-02 8:59
Mustafa Demirhan1-Jan-02 8:59 
GeneralRe: Problem with Multithreads Pin
Michael Dunn1-Jan-02 9:30
sitebuilderMichael Dunn1-Jan-02 9:30 
GeneralRe: Problem with Multithreads Pin
Mustafa Demirhan2-Jan-02 0:33
Mustafa Demirhan2-Jan-02 0:33 
GeneralCompatibility Question Pin
1-Jan-02 3:37
suss1-Jan-02 3:37 
GeneralRe: Compatibility Question Pin
Philip Patrick1-Jan-02 3:50
professionalPhilip Patrick1-Jan-02 3:50 
GeneralHi, Philip ,look here... Pin
1-Jan-02 5:42
suss1-Jan-02 5:42 
GeneralRe: Hi, Philip ,look here... Pin
Philip Patrick1-Jan-02 6:02
professionalPhilip Patrick1-Jan-02 6:02 
GeneralWM_ Pin
The_Server1-Jan-02 2:41
The_Server1-Jan-02 2:41 

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.