Window wrapper class, messages not working

Started by
1 comment, last by Scet 18 years, 4 months ago
I'm trying to create a window wrapper class. I've read various articles on the subject, but they seem too complex for what should be a simple task. What I want to be able to do is handle the messages outside of WinProc, however I'm only able to capture a few basic ones.

std::ofstream fout("C:/log.txt");
std::ofstream fout2("C:/log2.txt");
unsigned long LastMessage;

class Window{
private:
HWND WndHandle;
MSG WndMsg;
WNDCLASSEX WndClass;
static LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
 LastMessage=message;
 fout << message << "\n";
 return DefWindowProc(hwnd,message,wParam,lParam);}
public:
unsigned long Message();
HWND Handle();
bool Create(std::string Title,long Width,long Height,unsigned long Style,unsigned long ExStyle);
void Show(unsigned long Flags);
void Update();
void Poll();
void Destroy();
void Close();
};

void Window::Destroy(){
 UnregisterClass(WndClass.lpszClassName,NULL);
 DestroyWindow(WndHandle);
return;};

unsigned long Window::Message(){return WndMsg.message;}

HWND Window::Handle(){return WndHandle;}

void Window::Show(unsigned long Flags){ShowWindow(WndHandle,Flags);return;}

void Window::Update(){UpdateWindow(WndHandle);return;}

void Window::Close(){CloseWindow(WndHandle);return;}

void Window::Poll(){
 if(PeekMessage(&WndMsg,WndHandle,0U,0U,PM_REMOVE)){
  TranslateMessage(&WndMsg);
  DispatchMessage(&WndMsg);}
 fout2 << LastMessage << "\n";
return;}


bool Window::Create(std::string Title,long Width,long Height,unsigned long Style,unsigned long ExStyle){
 WndClass.cbSize=sizeof(WNDCLASSEX);
 WndClass.style=CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
 WndClass.lpfnWndProc=WindowProcedure;
 WndClass.cbClsExtra=0L;
 WndClass.cbWndExtra=0L;
 WndClass.hInstance=GetModuleHandle(NULL);
 WndClass.hIcon=NULL;
 WndClass.hCursor=NULL;
 WndClass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
 WndClass.lpszMenuName=NULL;
 WndClass.lpszClassName=Title.c_str();
 WndClass.hIconSm=NULL;
 if(!RegisterClassEx(&WndClass)){return 0;}
 WndHandle=CreateWindowEx(ExStyle,Title.c_str(),Title.c_str(),Style,CW_USEDEFAULT,CW_USEDEFAULT,Width,Height,NULL,NULL,GetModuleHandle(NULL),NULL);
 if(!WndHandle){return 0;}
return 1;}



int WINAPI WinMain(HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil){
			
      Window W;
	W.Create("Bob",200,200,WS_OVERLAPPEDWINDOW,0);
         
      W.Show(SW_SHOWDEFAULT);

				
	do{	

      W.Poll();
  
      }while(W.Message()!=WM_DESTROY && W.Message()!=WM_QUIT);
      
	W.Destroy();
												
  return 0;
}



Now the problem is that I can never seem to get WM_DESTORY or WM_QUIT detected. log.txt contains all the actual messages passed to WinProc, but log2.txt only contains some mouse messages. I figured adding LastMessage would solve it but even that didn't work. Anyone know how to solve this? I'd like to stick with Win32, but it's being a real bitch. [Edited by - Scet on November 26, 2005 2:30:46 PM]
Advertisement
The problem is likely your logging functions. Add a flush after each line of logging, and you'll probably see WM_DESTROY in the log.

Also, you probably want "while(PeekMessage..." rather than "if(" in your Poll() function.

Last, the best way to debug these things is to use the (gasp!) debugger! You can put a conditional breakpoint inside your window message handler, and make it break only on WM_DESTROY. It'll be pretty clear whether you actually get that message or not once the breakpoint is hit :-)
enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
The problem is likely your logging functions. Add a flush after each line of logging, and you'll probably see WM_DESTROY in the log.

Also, you probably want "while(PeekMessage..." rather than "if(" in your Poll() function.


I added the flush and while, but I'm getting the same results. I can see WM_DESTROY in log.txt, but not log2.txt like I want.

Quote:Original post by hplus0603
Last, the best way to debug these things is to use the (gasp!) debugger! You can put a conditional breakpoint inside your window message handler, and make it break only on WM_DESTROY. It'll be pretty clear whether you actually get that message or not once the breakpoint is hit :-)


Yeah I know that, I'm not some dumbass. WM_DESTORY does take place in WinProc, but I want to detect it outside of WinProc.

[Edited by - Scet on November 26, 2005 2:55:55 PM]

This topic is closed to new replies.

Advertisement