Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Andreas C

Win32 - getting hold of the messages

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. This should go on a Win32 forum I guess, but it''s all about game programming (or hrm... general), baby... So here goes anyways. I''ve just begun work on a little GL wrapper of mine, but so far I''ve not even touched upon the GL code, only Win32, which is what''s bugging me. I thought I''d make the wrapper general and all, and wrap the GL window + the Win32 code controlling it, in a nice little class (GLWindow) So basically I''ve got the class and everything working, except for some key parts. In GLWindow''s ctot, if it''s the first time it''s called, I fill in a Window Class ("WndClass") and register it. And right after that I create a window, by a call to CreateWindowEx. The window handler is stored in GLWindow::hWnd. That + ShowWindow, UpdateWindow is what''s going on in the ctor. It does exactly what I want; it creates a window (or as many as I want) using the Window Class "WndClass" as its base. The GLWindow class has a static WndProc, which is working the way I want. Problem, though, is that I''ve also got a functor (GLWindow:)) which you''ve got to call in your main loop to update the window, because inside of the functor I''m peeking for messages and sending ''em away to the message handler. After a call to PeekMessage(&msg, this->theWindowHandler, 0, 0, PM_REMOVE); the msg structure should contain the WM_messages waiting in the queue, for the _specific_ window handler I sent to PeekMessage... (this->theWindowHandler in other words)... Though, I don''t receieve any messages. Funny thing is that the translation and dispatching of the message, to the wndproc, seem to work perfectly anyways; just that I can''t get the message into msg.message. At least the messages that I want to be able to grab. I''ve resorted to making a console application, because it''s easy to output realtime debug data. So I can see the WM_messages being handled, but the ones I''m after (WM_CLOSE, WM_SIZE, etc) aren''t grabbed inside the functor. Seems a bit odd to be. Perhaps there''s something fundamentally wrong with the approach. When I''m not specifying the hWnd when calling PeekMessage, and just passing NULL instead, I''m able to grab messages like WM_SIZE, WM_CLOSE, etc. Which I find very weird. The hWnd has the correct window handle stored. Else it would totally freak out when calling PeekMessage. It wouldn''t act upon the correct message queues at all; which it does, because I''m able to close the windows and everything. Or rather the WndProc is. Here''s the main cpp file:
  
#include "GLWindow.h"
#include <iostream>

int main(int argc, char* argv[])
{
	GLWindow window("First window");
	GLWindow window2("Second window");
	
	while (int(window()) | int(window2()))
		;
	
	std::cout << "All windows closed..." << std::endl;
	return 0;
}
  
Here''s the wrapper class declaration:
  
#pragma once

#define WINDOWS_LEAN_AND_MEAN	
#include <windows.h>

#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

class GLWindow
{
public:
	GLWindow(const char*);
	~GLWindow();
	bool operator()();
private:
	static LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	static int wndsAlive;
	HWND hWnd;
	bool alive;
};
  
And finally, here''s the wrapper class definition:
  

#include <iostream>
#include "GLWindow.h"

using namespace std;
int GLWindow::wndsAlive = 0;

bool GLWindow::operator()()
{
  if (!alive)
    return (false);
	
  MSG msg;
  if (PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
  {
    GetMessage(&msg, hWnd, 0, 0);
    cout << "Message (" << hWnd << "): " << hex << int     (msg.message) << endl;
   if (msg.message == WM_CLOSE)
      MessageBox(hWnd, "Window:", "!", MB_OK);
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
  return (alive);
}

GLWindow::GLWindow(const char* title) : 
	alive(false)
{
	static bool registerWndClass = true;
	if (registerWndClass)
	{
		WNDCLASSEX wndc;
		wndc.cbClsExtra = 0;
		wndc.cbSize = sizeof(WNDCLASSEX);
		wndc.cbWndExtra = 0;
		wndc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
		wndc.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
		wndc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
		wndc.hInstance = GetModuleHandle(0);
		wndc.lpfnWndProc = WinProc;
		wndc.lpszClassName = "WindowClass";
		wndc.lpszMenuName = NULL;
		wndc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

		// Register the Window Class we just filled in...

		if (!RegisterClassEx(&wndc))
			cout << "_Couldn''t register window class" << endl;
		cout << "_Window Class registered" << endl;
		registerWndClass = false;
	}


	// Now let''s create our window of fun! 

	hWnd = CreateWindowEx(0,
		"WindowClass",
		title,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		0, 0, GetModuleHandle(0), NULL);
	if (!hWnd)
		MessageBox(NULL, "Couldn''t create the Window", "Fatal Error", MB_OK);
	
	// Show off our work

	cout << " _Creating Window #" << ++wndsAlive << " (\"" << title << "\")" << endl;
	alive = true;
	ShowWindow(hWnd, SW_NORMAL);
	UpdateWindow(hWnd);
}

GLWindow::~GLWindow()
{
	cout << wndsAlive << endl;
}


LRESULT CALLBACK GLWindow::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_CLOSE:
			cout << "closing handle: " << hWnd << endl;
			DestroyWindow(hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_SIZE:
			//width = LOWORD(lParam);

			//height = HIWORD(lParam);

			break;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

  
Any ideas, hints, to what could be the problem would be welcome. Thanks

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!