Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on C# winForm project where I need to "flash/blink" the form when the form receives an update. Here's the code I got from a VB example that I tried to convert to C# but I'm missing something because it's not flashing when i click the buttion. Can someone please tell/show me what I'm doing wrong here?

Thanks in advance,
-DA

public class Form1 : Form
{
   public Form1()
   {
      Initialize();
   }


   public void button1_Click(object sender, EventArgs e)
   {
      FlashForm.FlashWindow(this, FlashForm.FlashOptions.ALL, true, 5);
   }
}


public struct FlashWindowInfo
{
   public int cbSize;
   public IntPtr hwnd;
   public uint dwFlags;
   public uint uCount;
   public uint dwTimeout;
}

public static class FlashForm
{
   [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

   public static extern bool FlashWindowEx(ref FlashWindowInfo pInfo);

   public enum FlashOptions : uint
   {
      ALL = 0x3,
      CAPTION = 0x1,
      STOP = 0,
      TIMER = 0x4,
      TIMERNOFG = 0xc,
      TRAY = 0x2
   }

   public struct FlashWindowInfo
   {
      public int cbSize;
      public IntPtr hwnd;
      public uint dwFlags;
      public uint uCount;
      public uint dwTimeout;
   }

   public static void FlashWindow(Form FormToFlash, FlashOptions Options, bool FlashIfFocused, uint FlashTimes = 5)
   {
      if (!FlashIfFocused && FormToFlash.ContainsFocus)
          return;

      FlashWindowInfo info = default(FlashWindowInfo);
      var _with1 = info;
      _with1.cbSize = Marshal.SizeOf(info);
      _with1.dwFlags = Convert.ToUInt32(Options);
      _with1.dwTimeout = 0;
      _with1.hwnd = FormToFlash.Handle;
      _with1.uCount = FlashTimes;
      FlashWindowEx(ref info);
   }
}
Posted

1 solution

The struct FlashWindowInfo has value semantics, which means using _with is the problem, because it makes a copy of info. Simplify to:
C#
public static void FlashWindow(Form FormToFlash, FlashOptions Options, bool FlashIfFocused, uint FlashTimes = 5)
{
   if (!FlashIfFocused && FormToFlash.ContainsFocus)
       return;

   FlashWindowInfo info = default(FlashWindowInfo);
   info.cbSize = Marshal.SizeOf(info);
   info.dwFlags = Convert.ToUInt32(Options);
   info.dwTimeout = 0;
   info.hwnd = FormToFlash.Handle;
   info.uCount = FlashTimes;
   FlashWindowEx(ref info);
}

You probably want to add
C#
[StructLayout(LayoutKind.Sequential)]

to the struct FlashWindowInfo declaration.
 
Share this answer
 
v2
Comments
d.allen101 25-Sep-13 20:23pm    
i just realized thanks lol! hey are you the guy who wrote the code form vbCity that i got the example from (Matt's Blog)?
Matt T Heffron 25-Sep-13 20:23pm    
Nope.
Matt T Heffron 25-Sep-13 20:27pm    
Please accept the Solution as the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900