|
-
December 8th, 2002, 10:56 PM
#1
FromHandle Permission problem (maybe)
Hi,
Can anyone figure out why this returns null?
//----------------------------------------------------------------------------
//UIPermission uip = new UIPermission(UIPermissionWindow.AllWindows);
//uip.Assert();
IntPtr pi = new IntPtr(GetDesktopWindow());
Control c = Control.FromChildHandle(pi);
if(c == null) MessageBox.Show("Didn't work");
//-----------------------------------------------------------------------------
I think it may have to do with security permission. I know for a fact the Hwnd to the desktop is correct. If I use this line I can create a control...
Control c = Control.FromChildHandle(this.Handle);
Although, I don't want that particular control; I want the desktop. I have also tried Control.FromHandle, Form.FromHandle, and Form.FromChildHandle without any better luck.
Can anyone please help? Thanks!
That which does not kill us, only makes us stronger.
MCSD .NET
-
December 9th, 2002, 05:19 AM
#2
1.) desktop is not a control!
2.) FromChildHandle() method retrieves the control that contains the specific handle. So when you call that method with HWND of desktop window, you are expect to get the parent window of the desktop window. Try to use FromHandle method to do this.
However, due to the point 1, be really carefull when you are dealing with desktop window as control. It is not the control...
Martin
-
December 9th, 2002, 01:44 PM
#3
Thanks for your help.
I was first using Form.FromHandle without any luck. So since form inherits control, I tried using it instead... but that didn't work either.
I was reading the SDK and it stated that FromChildHandle was more reliable than FromHandle. It stated that it searched up the handle chain until it found the control. Maybe I'm using it incorrectly, but I have tried both methods and have not been able get an object representing the window I want from a handle.
I was just testing on the desktop window. I really want a reference to another window. I'm tring to build a preview for a screen saver. Durring preview, Windows passes a Hwnd on a command line. That Hwnd needs to be the parent of my form.
I want to set it to be my form's parent, but I can't get an object to set. All I can get is the Hwnd, and I can't set the parent equal to a hWnd.
With this new informtion, can you give any further advise?
Thanks!
That which does not kill us, only makes us stronger.
MCSD .NET
-
December 9th, 2002, 02:05 PM
#4
Code:
using System.Runtime.InteropServices;
class SomeClass
{
// ...
[DllImport("user32.dll")]
public static int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
// ...
public viod someFunc(...)
{
// ...
IntPtr pi = new IntPtr(GetDesktopWindow());
SetParent(theWindowYouWantToSetParentFor.Handle, pi);
// ...
}
}
Martin
-
December 9th, 2002, 03:07 PM
#5
I tried that, too, but it didn't update all the framework objects. They mentioned something about the Sendmessage API to refresh the objects; which I haven't got the message worked out yet?
If I reference the parent, it thinks it's null. For example the following blows up -- this.Parent.size
Every road I take has some kind of obstacle.
Thanks for your help!
That which does not kill us, only makes us stronger.
MCSD .NET
-
December 9th, 2002, 06:04 PM
#6
Well I got around the problem, but I had to use more APIs to do it.
So much for the FromHandle method.
If someone does know why that doesn't seem to be working, I would still be interested in hearing why.
Thanks for the help.
That which does not kill us, only makes us stronger.
MCSD .NET
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|