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

C# 開發固定背景圖片可捲動控件:OnPaintBackground、LockWindowUpdate 與 OnScroll 實作

     編輯:關於C語言

自訂背景繪製 OnPaintBackground

在客戶區右下角繪製 Logo 圖片:


protected override void OnPaintBackground(PaintEventArgs e) {
    base.OnPaintBackground(e);
    if (myLogonImage != null) {
        int x = ClientSize.Width - myLogonImage.Width;
        int y = ClientSize.Height - myLogonImage.Height;
        if (e.ClipRectangle.IntersectsWith(new Rectangle(x, y, myLogonImage.Width, myLogonImage.Height))) {
            e.Graphics.DrawImage(myLogonImage, x, y, myLogonImage.Width, myLogonImage.Height);
        }
    }
}

引入 Win32 API LockWindowUpdate


[DllImport("user32.dll")]
private static extern bool LockWindowUpdate(IntPtr hWnd);

OnScroll 捲動事件處理


protected override void OnScroll(ScrollEventArgs se) {
    if (bolFixedBackground) {
        if (se.Type == ScrollEventType.ThumbTrack) {
            LockWindowUpdate(IntPtr.Zero); // 解鎖
            Refresh(); // 立即重繪
            LockWindowUpdate(this.Handle); // 鎖定
        } else {
            LockWindowUpdate(IntPtr.Zero);
            Invalidate(); // 標記失效,稍後重繪
        }
    }
    base.OnScroll(se);
}

關鍵策略:捲動中 (ThumbTrack) 解鎖→重繪→鎖定,防止系統預設捲動破壞背景;捲動結束 (ThumbPosition 等) 僅標記失效,避免閃爍。

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