Blog Stats
  • Posts - 3
  • Articles - 0
  • Comments - 6
  • Trackbacks - 0

 

Thursday, November 01, 2007

User32's SetForegroundWindow() API in C#

If there occurs a need to check if some process is already running and then bringing that process' main window in front then .NET does not helps 100%.

Using the System.Diagnositics.Process class to find out a already running process is a easy task but to bring that process' main window in front is a difficult goal.

Here's the code to get the already running process (e.g. Notepad) and then bringing it to front, using the native API from User32.dll >> SetForegroundWindow(int WindowHandle)

[System.Runtime.InteropServices.

class Program
{
static void Main(string[] args)
{
 System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
   if (p.Length > 0)
   {
     SetForegroundWindow(p[0].MainWindowHandle);
   }
 }
}

DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

-Vaibhav Gaikwad

Tuesday, March 13, 2007

Flashing your window, getting attention.

This is a simple example of using Win32 api in .NET. I'll use the FlashWindow() API from user32.dll so as to make the window go flashing and get attention.

Take a simple Windows form and add a button to it. Add a reference to System.Runtime.InteropServices namespace. And heres the code.

// --------- code -----------
[DllImport("user32")]
public static extern int FlashWindow(int hWnd, int bInvert);

public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();
   }

   private void button1_Click(object sender, EventArgs e)
   {
      FlashWindow(this.Handle.ToInt32(), 1);
   }
} // -------- code ends

This was nice and simple for the start.

-Vaibhav Gaikwad

Where to start from ?

Thinking of where to start from with millions of topics in mind. I will dedicate this blog to unmanaged code. As a .NET programmer, I will be illustrating use of Win32 native code in managed .NET code.

Right form the start, I was really interested in exploring the Interop framework of .NET.

So this blog will be a mix of both, unmanged in managed world.

-Vaibhav Gaikwad

 

 

Copyright © Vaibhav Gaikwad