Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing a new mail screen

Status
Not open for further replies.

sjakiePP

Programmer
Apr 6, 2004
104
NL
Hello all,

I have an outlook component to send a sms. When opening a new mail window, you are able to click a button to send the message. After the message is sent, I want to close the window. How can I do that?

Untill now I found it has something to do with a WindowHandler. How does it work?

Thanks in advance.

Sjakie

---------------------------------------------
Yes, the world is full of strange people.
 
If you started the mail process using a Process object then you can retrieve the handle to that window using MainWindowHandle() property of the Process object.
Once you have that IntPtr , you can call SendMessage API to close that window:
Code:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
IntPtr hWnd = myProcess.MainWindowHandle();
if (IntPtr !=0) 
{
 SendMessage(hWnd, 0x0010, 0, 0);
}
If the mail program is started using ShellExecute() then the above property is not available and you should use others API to find the handle of that window, such WindowFromPoint(), GetWindow(), FindWindow() ...
obislavu
 
The window is opened by clicking on the new mail button. How is it started? My guess is ShellExecute().

Do you have an example?

---------------------------------------------
Yes, the world is full of strange people.
 
This is what I have so far:

Code:
private void CloseSendWindow() {
	int check = DestroyWindow(0); //windowhandler???
			
	if (check == 0) {	//close succeeded?
		System.Diagnostics.Debug.WriteLine("close failed");
	}
}

How do I get the windowhandler?

---------------------------------------------
Yes, the world is full of strange people.
 
As I explained, to send a message to another application you need its handle.
Here are two methods, one using FindWindow() and another by scanning the running processes.
I prefer the 2nd because FindWindow() requires the window title which can change.
Code:
[DllImport("user32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string _ClassName, string _WindowName);

//1st methods: find by window name
string strTargetWindowName = "TARGET WINDOW";
int h  = FindWindow(null, strTargetWindowName);
IntPtr hWnd = new IntPtr(h);
//..
//2nd method: find by process name
string strProcessName = "Outllok";
IntPtr hWnd = FindProcessByName(strProcessName);
//..
if(hWnd.ToInt32() != 0)
{
    SendMessage(hWnd, 0x0010, 0, 0); // WM_CLOSE 

}
			
public IntPtr FindProcessByName(string strProcessName)
{
	IntPtr handle = IntPtr.Zero;
	Process [] arrProcess = Process.GetProcesses();
	foreach(Process p in arrProcess)
	{
		if(p.ProcessName.ToUpper() == strProcessName)
		{
			handle = p.MainWindowHandle;
			break;
		}
	}
	return handle;
}
obislavu
 
That did the trick. Thnx very much!

---------------------------------------------
Yes, the world is full of strange people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top