Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Use of IsDialogMessage and TranslateAccelerator.

$
0
0
#include <windows.h>
#include <commdlg.h>
#include <sstream>
#include <string>
#include "resource.h"

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

static HWND  hDlgModeless;
static TCHAR szAppName[] = TEXT("POPPAD");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	MSG       msg;
	HWND      hwnd;
	HACCEL    hAccel;
	WNDCLASS  wndclass;

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

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, NULL,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hInstance, szCmdLine);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	hAccel = LoadAccelerators(hInstance, szAppName);

	if (hAccel == NULL)
	{
		int i = 0;
	}

	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (hDlgModeless == NULL || !IsDialogMessage(hDlgModeless, &msg))
		{
			if (!TranslateAccelerator(hwnd, hAccel, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	int i = 0;
	switch (uMsg)
	{
	case WM_CREATE:
		// Initialize the window. 
		return 0;

	case WM_PAINT:
		// Paint the window's client area. 
		return 0;

	case WM_SIZE:
		// Set the size and position of the window. 
		return 0;

	case WM_DESTROY:
		// Clean up window-specific data objects. 
		return 0;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
			// Process the accelerator and menu commands. 
			case IDM_EDIT_COPY:
				i++;
			case IDM_EDIT_PASTE:
				break;

			default:
				break;
		}
		break;

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

I am going through some Petzold. I have the accelerator table loading and the message handler set up and it is working fine. There is a declaration of:

static HWND  hDlgModeless;

Further in WinMain there is:

if (hDlgModeless == NULL || !IsDialogMessage(hDlgModeless, &msg))

So it looks like hDlgModeless is initialized to NULL at compile. I am reading the documentation for IsDialogMessage:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645498(v=vs.85).aspx

So it tells me if the message has not been processed then execute the code in the "if block". I am wondering why hDlgModeless was chosen for use in the "if block" as opposed to using hwnd which is the handle to out actual window?

Later in WinMain there is:

if (!TranslateAccelerator(hwnd, hAccel, &msg))

I am trying to determine from the documentation why the "!" in "!TranslateAccelerator". That is not really clear to me yet.

The block as whole:

	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (hDlgModeless == NULL || !IsDialogMessage(hDlgModeless, &msg))
		{
			if (!TranslateAccelerator(hwnd, hAccel, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}

I could use some help I think as far as how things are functioning in general.

Thanks...





Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>