|
Hi,
For the graphics, I need to draw a button in the Non client area. I can get the handle of the non client area. I want to draw a button using the ControlPaint.DrawButton method wich need a Graphics as input.
ok for the dll import
There is no spoon.
|
|
|
|
|
When you receive the WM_NCPAINT notification message I mentioned to you before, the Message.WParam contains the HRGN (handle to a region) to be painted. You P/Invoke GetDCEx passing your window handle (HWND - the Handle property on all Control s), the HRGN mentioned above, and DCX_WINDOW|DCX_INTERSECTRGN ( =0x81 ). This is covered in the Platform SDK for the WM_NCPAINT message. IntelliSense will not help. You must read about the notification messages and APIs when working with this type of stuff.
The WM_NCPAINT documentation in the Platform SDK even includes a sample of calling GetDCEx .
The return value from GetDCEx is the HDC (handle to a device context) from which you create a Graphics object. Use the static Graphics.FromHdc method to get a Graphics instance for the non-client region.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
I have done what you said, but there is nothing. Maybe I have wrongly declared the things...
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, long flags);
in the WndProc method, I trap the WN_NCPAINT
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
ControlPaint.DrawButton(gr, 50, 1, 5, 5, ButtonState.Normal);
nothing happens. it should draw a button 5x5 at pos (50, 1) in the title bar, but I see nothing. Maybe I don't use it well...
There is no spoon.
|
|
|
|
|
A DWORD translates to an int (or uint ), not a long . The value of DCX_WINDOW|DCX_INTERSECTRGN wouldn't, therefore, by read from the execution stack when calling GetDCEx - only 0 would be read and that wouldn't produce the results you want.
Also, don't bunch your method calls together so much. Try debugging that and you'll see that you can't check return values so it will be hard to know why a particular method failed.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, I have changed the long to an int
but I still have the error "An unhandled exception of type 'System.OutOfMemoryException' occurred in system.drawing.dll
Additional information: Out of memory."
when I try to do:
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
I have now declared the functions like this:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
and the constants:
private const int DCX_WINDOW=0x0001;
private const int DCX_INTERSECTRGN=0x0080;
while these constants are declared as long in the WinUser.h from the Platform SDK.
Also, Platform SDK declares the GetDCEx function like this:
HDC GetDCEx(
HWND hWnd,
HRGN hrgnClip,
DWORD flags
);
Maybe I'm wrong somewher in the C# translation...
There is no spoon.
|
|
|
|
|
A native long is 32 bits. A managed long is 64 bits (System.Int64 ). This is documented in the Platform SDK (and I know from many years of experience). If you want further proof, read Platform Invoke Data Types[^] in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok for the length between Win32 and .NET.
I have written the declarations as you advised me, but I get an error when I execute the line
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
There is no spoon.
|
|
|
|
|
It would help to know on which operation, which means - as I said before - you have to split them up.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have declared the following:
private const int DCX_WINDOW=0x0001;
private const int DCX_INTERSECTRGN=0x0080;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
I have trapped the WM_NCPAINT in the WndProc method of my System.Windows.Forms.Form derived window:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT:
{
Debug.WriteLine("WM_NCPAINT");
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
ControlPaint.DrawButton(gr, 60, 1, 5, 5, ButtonState.Normal);
break;
}
}
base.WndProc(ref m);
}
and I have the message "An unhandled exception of type 'System.OutOfMemoryException' occurred in system.drawing.dll
Additional information: Out of memory.", at the line
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
There is no spoon.
|
|
|
|
|
As I said before, split the line:
IntPtr hdc = GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN);
Graphics g = Graphics.FromHdc(hdc); This is the very problem I mentioned earlier about combing such complex, compound statements together. Now which lines does the exception get thrown?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I still get the error
it seems the handle is null... When I call the GetDCEx I have 0x0 as result.
There is no spoon.
|
|
|
|
|
bouli wrote:
When I call the GetDCEx I have 0x0 as result
Check to make sure you have a valid handle to the window when you call GetDCEx .
- Nick Parker My Blog | My Articles
|
|
|
|
|
I always get a null handle
There is no spoon.
|
|
|
|
|
And for the 3rd time, on which line do you get the error after splitting the two methods on two separate lines?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I get the error because the Handle returned by GetDCEx is null.
I can understand that the handle is null once (at initialization time), but it's always null
there is maybe something wrong in my declaration?
There is no spoon.
|
|
|
|
|
So the exception is being thrown on the second line? How hard is that question to answer?
You need to debug your code and find out this type of information. Is the Message.HWnd or Message.WParam NULL (IntPtr.Zero ) before being passed to GetDCEx ?
I never said splitting the lines would make it work - that doesn't matter when the compiler produces the IL. I said it would make things easier to debug. Put a break-point in there and step through your code to see what the inputs and outputs of the methods are. And always perform checks (like making sure that GetDCEx doesn't return IntPtr.Zero before passing it to Graphics.FromHdc ) - never assume something just works (especially when dealing with interop since the callee is unmanaged by the CLR).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
ok, I'm going to dig deeper.
but it was easier to draw in the NC area with MFC
.NET is very different... I need to check my marks.
Thanks for the help
There is no spoon.
|
|
|
|
|
Yes, of course it's very different. .NET is referred to as a "managed environment" because the Common Language Runtime (CLR) manages memory, provides garbage collection (no explicit destruction of objects like in C/C++/MFC), and many other things.
The important thing to remember is that Windows Forms controls encapsulates the Windows APIs, so everything possible in MFC would be possible in .NET, but you have to P/Invoke native APIs (and don't P/Invoke those that are already covered in .NET APIs), define structs and consts, etc. You can't simply call native APIs like you could in MFC, unless you were to use a Managed C++ assembly in a mixed-mode compilation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how to get all TimeZone and send it to Time Server, recieved Time Server reply and add it to database
thanks.
Nho'c Ti`
|
|
|
|
|
What??????????????????????????????
This doesn't make any sense whatsoever. What do you mean by "get all TimeZone" and "send it to Time Server"? What are you trying to do with this information? Are you trying to get a Time value from a server localized to your time zone?
And what's with the database?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
Hi,
I've searched everywhere! Does anyone here know how to call a function from C# from an IntPtr? I know how to obtain the function pointer:
IntPtr ptr = (typeof(SOMECLASS).GetMethod("SomeMethodName").MethodHandle.GetFunctionPointer())
Now I just need a way to call it ..
The reason I need this is because in MSHTML, there is an interface called IHTMLElementRender, and the function 'DrawToDC' (useful for capturing HTML documents as an image) takes the argument HDC, but is implemented to take a reference variable of an unrelated structure. Therefore I need to call the function by the pointer, as in C++.
Thanks!
- Joe Esposito
|
|
|
|
|
You've run into one of the limitations of IDL: it was originally for describing structures to be marshalled across processes, but some things we'd like to describe in IDL - handles to GDI and USER objects - are process-relative and have no meaning in another process. So the IDL contains a fake definition.
I've browsed Google Groups for _RemotableHandle . The recommended answer is apparently to use ILDASM to generate IL code for the interface, change the declaration to take an IntPtr , then recompile it. Not nice.
I'd actually recommend using Managed C++ to wrap the interface as declared in mshtml.h.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Instead of wrapping this with an MC++ assembly as the first reply states, just interop the interface yourself. Such it's such a simple interface, it won't be hard:
[ComImport, Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IHTMLElementRender
{
void DrawToDc(IntPtr hdc);
void SetDocumentPrinter(
[MarshalAs(UnmanagedType.BStr)] string printerName, IntPtr hdc);
} You could then use Marshal.GetComInterfaceForObject to get a pointer to this interface. You then call Marshal.GetObjectForIUnknown and cast to the interface like so:
IntPtr ptr = Marshal.GetComInterfaceForObject(element,
typeof(IHTMLElementRender));
IHTMLElementRender render = (IHTMLElementRender)Marshal.GetObjectForIUnknown(
ptr);
render.DrawToDc(hdc);
Marshal.Release(ptr); Please note that this is untested but I've used similar code with different interfaces before with success.
I hope this helps.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks! That did exactly what I needed it to!
I did consider using this method, however all of my attempts failed and left me clueless.
It seems that this attribute really makes the difference:
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
Without it, the program simply crashes when you try to use it.
Also, I have been using type-casts where QueryInterface would be required. It also worked in this case:
IHTMLElementRender render = (IHTMLElementRender)element;
render.DrawToDc(hdc);
Thanks again for your reply
- Joe
|
|
|
|