Sign in to follow this  
  • entries
    67
  • comments
    82
  • views
    36825

Source code of the Window Manager

Sign in to follow this  

223 views

We just tried to program some of the below illustrated diagram. But have run into some problems. Apparantly the hWnd is not being filled correctly, and its a little confusing on why this error is occuring.

In Window Manager we create a WNDCLASSEX that is send to the Window.C where the Hwnd gets asked to make a CreateWindow out of it. Sadly the hWnd is not working really well atm. But here is the source code thusfar, I hope to get it fixed by the end of this day.

Window_Manager.h

#include
#include "window.h"


class Window_Manager
{
public:
// de Message Queue van Windhoos
void addWindow();

Window_Manager(); // c'tor
virtual ~Window_Manager(void); // d'tor

private:
void* listStart;


};



Window Manager.cpp


#include "Window_Manager.h"


HINSTANCE hInst = 0;

struct WindowList
{
Window* wnd;
WindowList* lNext;
};

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBox(0, TEXT("WM_LBUTTONDOWN message."), TEXT("Msg"), MB_OK);
return 0;
// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
// Forward any other messages we didn't handle to the
// default window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}

Window_Manager::Window_Manager()
{
this->listStart = NULL;
}

void Window_Manager::addWindow()
{
WNDCLASSEX wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = TEXT("Vyper");

RegisterClassEx(&wc);


WindowList* nWnd = new WindowList;
WindowList* sWnd = reinterpret_cast (this->listStart);

nWnd->wnd = new Window(wc, 100, 100, 300, 300);
nWnd->lNext = NULL;

if (this->listStart == NULL)
this->listStart = nWnd;
else if (sWnd->lNext == NULL)
sWnd->lNext = nWnd;
else
{
while (sWnd->lNext != NULL)
sWnd = sWnd->lNext;

sWnd->lNext = nWnd;
}
}

Window_Manager::~Window_Manager()
{

}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
hInst = hInstance;
Window_Manager* wm = new Window_Manager();
for (int i = 0; i < 3; i++)
{
wm->addWindow();
}
}



Window.h

#include

class Window
{
public:
Window(); // default c'tor
Window(WNDCLASSEX wc, int x, int y, int width, int height); // specific c'tor
virtual ~Window(); // d'tor
//bool getFocus();
//void setFocus(bool hasFocus);


private:
HWND hWnd;
WNDCLASSEX wc;
//bool hasFocus;

};



Window.cpp


#include "Window_Manager.h"


Window::Window(WNDCLASSEX wc, int x, int y, int width, int height)
{

this->wc = wc;
this->hWnd = CreateWindow(TEXT("test"), TEXT("Vyper"), WS_OVERLAPPEDWINDOW,
x, y, width, height, NULL, NULL, wc.hInstance, NULL);

ShowWindow(this->hWnd, SW_SHOWDEFAULT);
UpdateWindow(this->hWnd);


}
Window::~Window()
{

}

Sign in to follow this  


3 Comments


Recommended Comments

One thing that occurs, although I don't know how this would relate to the problem, is that every time addWindow is called, the window class is being registered. Then the window class is passed to the window constructor but not actually used to create the window.

It shouldn't actually matter really. The first call to addWindow will register a window class called "Vyper" which is what is then being used to create the window, and I'd guess any subsequent calls to addWindow that try to register the class will fail since a window class called "Vyper" has already been registered.

If you intend to use the same window class for all of your windows, you only need register this once. If you want different window classes for different windows you need to give them unique names and then reference the name from the wc paramter in the call to CreateWindow.

As I say though, I don't see why this would prevent the HWND from being filled in correctly.

Share this comment


Link to comment
Well the idea is basically that the program can create multiple windows, I could ofcourse add a CIN that basically opens one window. But it should write out the first window regardless before crashing or giving an error at the second attempt.

Even my teacher is clueless how to solve the problem. Which is why I posted the code here in hope that someone knows what i am doing wrong as this is damn anoying :(

Share this comment


Link to comment
Further to the forum postings...

Quote:
Original post by wijnand
Even my teacher is clueless how to solve the problem.


Your teacher didn't suggest to you to test the return result from RegisterClassEx. I have deep concerns about your teacher [smile].

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now