首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Visual C# 任務列通知視窗(三):不規則視窗 Region、ShowWindow 無焦點顯示與拖曳模擬

     編輯:關於C語言

無焦點顯示視窗:ShowWindow API

.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
}

不規則視窗 Region 建立

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;
}

視窗拖曳模擬:SendMessage WM_NCLBUTTONDOWN

[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);

雙 Timer 控制升起/停留/隱藏動畫

  • 升起:Top 遞減至工作區上方
  • 停留:CurrentState = 2,計時器控制停留時間
  • 隱藏:Top 遞增超出螢幕底部
  • SetBounds 同步更新位置與尺寸

雙重緩衝繪圖 (OnPaintBackground)

先繪製至記憶體位圖,再一次性繪至螢幕,消除閃爍。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved