.NET Form.Show() 會搶焦點,需呼叫 Win32 ShowWindow(hWnd, SW_SHOWNOACTIVATE=4)。
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public void ShowForm(...)
{
// ... 設定位置、尺寸、矩形區域 ...
ShowWindow(this.Handle, 4); // SW_SHOWNOACTIVATE
}
public void SetBackgroundBitmap(Image image, Color transparentColor)
{
BackgroundBitmap = new Bitmap(image);
Width = BackgroundBitmap.Width;
Height = BackgroundBitmap.Height;
Region = BitmapToRegion(BackgroundBitmap, transparentColor);
}
public Region BitmapToRegion(Bitmap bitmap, Color transparentColor)
{
if (bitmap == null) throw new ArgumentNullException("bitmap");
GraphicsPath path = new GraphicsPath();
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
if (bitmap.GetPixel(x, y) == transparentColor) continue;
int x0 = x;
while (x < bitmap.Width && bitmap.GetPixel(x, y) != transparentColor)
x++;
path.AddRectangle(new Rectangle(x0, y, x - x0, 1));
}
}
Region region = new Region(path);
path.Dispose();
return region;
}
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 2;
// 滑鼠按下標題區域時呼叫
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
Top 遞減至工作區上方CurrentState = 2,計時器控制停留時間Top 遞增超出螢幕底部SetBounds 同步更新位置與尺寸先繪製至記憶體位圖,再一次性繪至螢幕,消除閃爍。