954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Destroy/Close the window

Hi all,

I've create a window using CreateWindowEx windows API and do some work on it. Thats fine. But for the safer operation at the end of all the processing I want to close/destroy the window.

I've try this,

BOOL clsWin = DestroyWindow(m_hwnd_RTFBox);
	if(clsWin != 0)
	{
		printf("SS");
	}


Here m_hwnd_RTFBox is the handle to the window. On the above code there is no compile error, but gives a runtime error and saying there is a unhandled exception on the first line. Where I'm going wrong. I found this from the MSDN, but it wont work.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

Hi,
Put all of you code here.

kv79
Posting Whiz in Training
292 posts since Nov 2007
Reputation Points: 30
Solved Threads: 7
 
// Create window
	try
	{
		m_hwnd_RTFBox = CreateWindowEx(
							WS_EX_APPWINDOW,
							RICHEDIT_CLASS,
							"RichTextWindow",
							WS_BORDER|ES_MULTILINE,
							0,
							0,
							100,
							100,
							::GetConsoleWindow(),
							NULL,
							0,
							NULL);
	}
	catch(...)
	{
		std::cerr << "Can't create the window handler." << std::endl;
	}
// Processing done here
	::SetWindowText(m_hwnd_RTFBox, rtf.c_str());
	int recCount = GetWindowText(m_hwnd_RTFBox, pBuffer, 1024);
	
	if(recCount > 0)
	{
		string strPlr(pBuffer, recCount);
		cout << strPlr;
	}

// Want to destroy the window here
eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

I think The window must have WindowProcedure ,and then a message system control to DestroyWindow.

kv79
Posting Whiz in Training
292 posts since Nov 2007
Reputation Points: 30
Solved Threads: 7
 

But I can't use WindowProc, because I use windows API here in my code.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

CreateWindowEx does not throw an exception on error so there is no point to the try/catch block. If the function fails then you have to call GetLastError() to find out the error number, and you can pass that to FormatMessage() to get a human-readable error message.

I don't think you want to use the console's window handle as the parent of a Windows GUI program. Instead, pass NULL as that parameter.

>>But I can't use WindowProc, because I use windows API here in my code
That's what the WindowProc function is for. Establish the WindowProc when you call RegisterClass before CreateWindowEx()

If you want to write win32 api programs then don't start out with a console program. Set this tutorial for the basics.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
CreateWindowEx does not throw an exception on error so there is no point to the try/catch block. If the function fails then you have to call GetLastError() to find out the error number, and you can pass that to FormatMessage() to get a human-readable error message.

Yes there is no exception thrown. I've just use it for my clear work out, and also I've never use the GetLastError() because few time I'm confused that at which place should I use it.I don't think you want to use the console's window handle as the parent of a Windows GUI program. Instead, pass NULL as that parameter.

Used the console window as the main window because my application also console based application.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

Now ,i do not uderstand anytohing what you wrote.


eranga262154 wrote
>But I can't use WindowProc, because I use windows API here in my code.

Ancient
>>I don't think you want to use the console's window handle as the parent of a Windows GUI program.


He use API i tought that is for windows application.And must have WinProc.

kv79
Posting Whiz in Training
292 posts since Nov 2007
Reputation Points: 30
Solved Threads: 7
 
He use API i tought that is for windows application.

Yes I use windows APIAnd must have WinProc.

In API I can't use it, isn't it?

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

>>Used the console window as the main window because my application also console based application.

CreateWindowsEx REQUIRES a win32 api program because it is a GUI. You can't create one with a console program. Period.

>>He use API i tought that is for windows application.And must have WinProc.
I agree. Which is why his program crashes. And he doesn't have a message pump so the window will never get any messages even if it were created.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
And he doesn't have a message pump so the window will never get any messages even if it were created.

I can send messages to the window. Send a message to close the window, (WM_CLOSE) and try to destroy the window. But not work. I can send messages to it.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

Obivous is that you can't send the real message,because it is not working.Or i do not know.

kv79
Posting Whiz in Training
292 posts since Nov 2007
Reputation Points: 30
Solved Threads: 7
 
I can send messages to the window. Send a message to close the window, (WM_CLOSE) and try to destroy the window. But not work. I can send messages to it.

You can send all the messages you want, but without a message pump to pass them along to the correct window they will just be tossed into the bit bucket by the Windows operating system. You need to read and code a tutorial about how to write a windows program.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm not clear what Ancient Dragon says. You says that, according to my code I can't send message to the window. I've try it with PostMessage() with the window handler. So....

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

>>You says that, according to my code I can't send message to the window
Read Very Carefully and Very sloooooooooooowly. I did not say that. Shall I repeat -- I did not say that. What I said was that the window will never receive any of the messages your console program sends it. Calling PostMessage() does not guarentee the receipient will receive the message. When you mail a letter at the post office the person to whom you sent the letter may or may not receive it. If the person lives some place that has no post office then your letter will be sent to the Post Office's dead letter office. Same with Windows messages.

If you will just read this win32 api tutorial you will probably understand more.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

That is really helpful to me. Thanks.

As there example explain, I try to send a message and depend on it destroy the window. But in my case the window is the console. If I get correct in my application I can't destroy the window. Right? So that mean the window destroyed automatically....

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You