Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I wrote the UnregisterClass() function into the Window Class Destructor, and a random thought struck me. Should I also include the DestroyWindow(); WindowHandle = Null; in the destructor? More than likely the window will be destroyed through the Window Procedure, but is it a good idea to include it in the Destructor on the off chance the class is destroyed while the window is open?

C++
Window::~Window(){
	DestroyWindow(WinHandle);
	WinHandle = NULL;
	UnregisterClass(ClsName.c_str(), MyApp->GetInstance());
}


Window Wrapper Class
C++
class Window{
public:
	Window();
	~Window();

	bool DefineClass(int ClassName, WNDPROC WindowProcedure);
	bool DefineClass(int ClassName, WNDPROC WindowProcedure, int Menu);

	bool CreateNewWindow(int ClassName, int WinTitle, DWORD Style, DWORD ExStyle,  RECT WindowSize, bool Centered, HWND Parent, LPVOID lpParam);

	bool CreateNewWindow(int ClassName, int WinTitle, DWORD Style, DWORD ExStyle, RECT WindowSize, bool Centered);

	void Show(void);
	void Show(int nCmdShow);

	HWND GetWinHandle(void);

private:
	HWND WinHandle;
	std::wstring ClsName;

};


The AppClass Creates the Message Window and provides functions to pass information to the seperate windows...

C++
class AppClass{
public:
	AppClass(HINSTANCE hInstance, POINT MinScreenResolution);
	~AppClass();

	bool CreateMsgWindow(void);

	int Error(int ErrorID);

	std::wstring SetText(int TextID);

	int Run(void);
	int Run(int AcceleratorID);


	void QueryScreenResolution(void);
	
	bool CreateTrayIcon(void);
	void KillTrayIcon(void);


	HINSTANCE GetInstance(void);
	POINT GetScreenResolution(void);
	RECT GetMaxWindowResolution(void);
	HWND GetMsgWindowHandle(void);
	HWND GetWindowHandle(int ClassName, int WindowTitle);
	
private:
	HINSTANCE AppInstance;
	NOTIFYICONDATA NID;

	RECT MaxWindowResolution;  
	POINT MinScreenResolution;
	POINT ScreenResolution;
	Window *MsgWindow;

	int InitializeProgram(void);
};


Also, in case I failed to mention, just a hobby. This application is just a excercise in the Windows API plus I wanted to test if there is any benefit to a message window. And I am testing the effectiveness of encapsulating the Window Procedure Versus having it run outside of any specific class. I have some theories on the added overhead of encapsulating the procedure... So basically just a learning program.
Posted
Updated 26-Jan-15 15:42pm
v2

1 solution

No, there is absolutely no need to do it. Just think about it: you have done proper side effects of destruction. And your WinHandle is nothing but a field which lost its connection to OS already. You won't access it anyway, because your instance is already destroyed. So, it does not matter what is that.

If it wasn't a destructor, you could think of some use if this field. If you wanted to creates some wrapper which disconnects from the Windows window object and later possibly reused for some other window (I'm not saying this would be a good idea, just speculating), then, for example, you could use null to indicate that the field is not an OS window at the moment. In your case, the instance of your wrapper class cannot be used at all.

—SA
 
Share this answer
 
Comments
drkwlf 26-Jan-15 14:18pm    
Just for clarification destroying the window doesn't cause the instance to end. (If we are both using the word instance to refer to the program instance). The Window wrapper class is a simple wrapper class that registers, creates and destroys windows. There is an underlying message window that keeps the application active while the different windows perform different tasks. There are 20 ID's in the String Table set aside for Window Titles and Class Names. That the Message Window( Not a real message window, simply a window that is never shown ) can use to interact with the different Windows... For instance I have one window that tracks mouse movement while in the client area and draws the mouse path onto a different window. I don't know if this changes your opinion at all but after rereading the question I thought it important to be more specific
Sergey Alexandrovich Kryukov 26-Jan-15 16:04pm    
Of course, destruction of the window doesn't, but destruction of a wrapper certainly puts the use of this wrapper to end. This is the whole point. I understand the purpose of you wrapper and think it is reasonable (well, depending on what else you do with it). I think the fact that you are showing a destructor tells the tail, no more clarification is need. (And, but the way, thank you for correct formulation of the question; unfortunately most other questions here are not formulated to be answerable.)
—SA
drkwlf 26-Jan-15 21:12pm    
I see your point I think... After the Window has ran its course and is deleted there is no reason to clear the Window Handle as Windows has already forgotten about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900