In our previous Article “C# – Graphics – Display an Image“, we have discussed displaying an image on the Application window. As we have seen flickering when drawing the image, we have discussed eliminating the flickering in the Article “C# – Graphics – Double buffering“. I recommend you to read these Articles before you start reading this article.
In this article, I will explain how to display an Image on another Application window; for example, the Windows Calculator window.
How does it possible? We have control over our Application window; hence we are able to display the image on the Application window. But how does it possible to display the image on another Application window; for example, the Calculator window?
Before answering your query, first, you need to understand how .Net will display the drawing or graphics on the Application window. As I mentioned in my previous Article “C# – Graphics – Display an Image“, there is a canvas required to draw and we call this canvas, the Device Context. A device context is associated with a window. If we get the window object or handle, we can get the Device Context from it, and then we can draw on the device context; which means, drawing on the window. Lets’ discuss how to get this and draw an image on it.
I have tested this application on Windows 7 Operating System.
Step 1. We need to get the Application window handle where we would like to draw an image. We have the Application windows, for GUI-based Applications to interact with them. Each Application window will display a window title or caption and it has an associated windows class; which tells the characteristics of the window.
We need to use the Win32 API FindWindow
function to get the window handle. The syntax of this function looks like below:
Syntax: HWND FindWindow(LPCSTR lpClassName, LPCSTR lpWindowName)
Where lpClassName
is the window class name. And lpWindowName
is the window title or caption. Both these are string arguments.
Upon success, this function returns a window handle. Otherwise, it returns a NULL
value.
But how do we know the class name? I have used Visual Studio “Spy++” Tool to get the window’s class name & its’ title. Once we have these details, we can pass them in
FindWindow
function to get the window handle.
Step 2. We must declare the syntax of the Win32 API function in C# style; in our Project. Otherwise, the C# compiler won’t understand the types mentioned in the above syntax. So, the C# style syntax of FindWindow
function looks like below: And also, to use Win32 API in our Project; we must import “user32.dll” library using DllImport
statement.
[DllImport(“user32.dll”)]
private static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
Step 3. And we must use the InteropServices
namespace (which is in System.Runtime namespace) , in order the compiler to recognize DllImport
statement.
Step 4. Once we have the window handle, we will create the Graphics
object from the window handle. Graphics class provides a method FromHwnd
to create the Graphics
object from the window handle. As discussed in our previous Article, the Graphics class has methods to deal with drawing 2D graphics on the canvas.
Graphics g = Graphics.FromHwnd(hWnd);
Step 5. Now we have Graphics
object. Using it, we can draw an image on the Calculator window. How do we get the dimension of the Calculator window? We can use Graphics
object’s VisibleClipBounds
property to get the visible clip region of the window.
g.DrawImage(img, 0, 0, g.VisibleClipBounds.Width, g.VisibleClipBounds.Height);
Step 6. Don’t forget to dispose of the Graphics
object using the below statement.
g.Dispose();
Step 7. Putting it all together, the complete code looks like below:
Step 8. Once the changes are done, Build & Run the Program. If you do not see any Errors; it will display the Application window with the “Show” button displayed on it.
Now you open Windows Calculator. And from our Application window, click on the Show button. Surprised? You will see an image drawn on the Windows Calculator window.
We discuss more on C# in upcoming Articles.
(Raju)