-1

I want to check whether a form has a Form Border by its handle. And, the handle is from the another Application.

How can I handle this? Please help me.. Thanks!

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
jn4kim
  • 399
  • 1
  • 3
  • 10
  • 2
    Windows Forms? Native Win32? Something else? -- Please note that C# as a *language* doesn't know anything about handles and windows etc. so your question must be about a particular UI framework. Please tag accordingly. – stakx - no longer contributing Jul 16 '12 at 21:22
  • 1
    P.S. Win32 does not even distinguish between windows and controls. Technically, they are all "windows", albeit ones with differing attributes. – stakx - no longer contributing Jul 16 '12 at 21:24
  • hmm.. i Just want to know whether it has a FormBorder or not. And the handle is from another Application. – jn4kim Jul 16 '12 at 21:27
  • @jn4kim: Then that should be your question. As stakx said, controls themselves are windows. – Ed S. Jul 16 '12 at 21:32
  • @jn4kim: My question still stands. It seems you're talking about Winforms. But what if that other application is native code, or WPF? The notion of a `FormBorder` *might* be meaningless with these... – stakx - no longer contributing Jul 16 '12 at 21:35

2 Answers2

2
[DllImport("user32.dll")]
extern static int GetWindowLong(IntPtr hWnd, int nIndex);

const int GWL_STYLE = -16;
const int WS_BORDER = 0x00800000;  // thin border
const int WS_THICKFRAME = 0x00040000;  // sizing (thick) border

public static bool NativeWindowHasBorder(IntPtr hWnd)
{
     return (GetWindowLong(hWnd, GWL_STYLE) & (WS_BORDER | WS_THICKFRAME)) != 0;
}
Tergiver
  • 14,171
  • 3
  • 41
  • 68
0

Controls themselves don't actually have a handle. Control.Handle actually returns it's parent window's .Handle.

From MSDN for Control.Handle:

Gets the window handle that the control is bound to.

If you look at the decompiled source for Control, you'll see:

internal IntPtr HandleInternal
{
  get
  {
    return this.window.Handle;
  }
}

Edit

What I've stated above is completely incorrect. I'm leaving it for historical sake.

One can prove this very easily by putting a Button on a Form, and looking at the IntPtr Handle value for them. They are different.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Uhm, is it a good idea / legal to post decompiled framework code...? I don't want to FUD you but this might be worth a thought. PS: That decompiled code is almost meaningless without more knowledge about what `this.window` mean. – stakx - no longer contributing Jul 16 '12 at 21:30
  • Thankyou for your answer but this is not what i want to know, i think i did not write the question correctly – jn4kim Jul 16 '12 at 21:32
  • @stakx I think it's pretty clear that the Control itself doesn't have a handle, and instead, it's returning it's parent window's handle. Just as the MSDN text describes. – Jonathon Reinhart Jul 16 '12 at 21:34
  • 1
    @Jonathon: -1. I believe your answer is incorrect, and I am certain that controls have a handle, too. Your answer actually supports this: Controls are technically also "windows" in Win32 API parlance (and Winforms is based on Win32), so I think you're mistaken when you assume that "window handle" means "parent window handle". Instead the `Control.Handle` "window handle" refers to the internal `HWND` of the control. – stakx - no longer contributing Jul 16 '12 at 21:40
  • @stakx You are absolutely correct. I just did a quick test, and indeed, the values of `IntPtr Handle` are different between a `Form` and a `Button` on that `Form`. Having used Spy++ 100's of times before, I should have thought of this. – Jonathon Reinhart Jul 16 '12 at 21:51