| 特性 | WINDOW | PAD |
| 尺寸限制 | 不超過物理屏幕 | 可任意大,不受終端可視區限制 |
| 位置綁定 | 固定在屏幕座標 | 無固定位置,需指定顯示區域 |
| 子窗口 | subwin/derwin | subpad |
| 刷新方式 | wrefresh/wnoutrefresh | prefetch/pnoutrefresh |
| 與標準屏關聯 | 直接關聯 | 無直接關聯 |
核心 API
| 函數 | 原型 | 關鍵參數說明 |
newpad | WINDOW* newpad(int lines, int cols) | 創建指定行列數的 pad,無定位參數 |
subpad | WINDOW* subpad(WINDOW* pad, int lines, int cols, int begin_y, int begin_x) | 在父 pad 內創建子 pad,座標相對父 pad |
prefresh | int prefresh(WINDOW* pad, int py, int px, int sy1, int sx1, int sy2, int sx2) | 核心刷新函數:
py,px:pad 內待顯示區域左上角
sy1,sx1,sy2,sx2:標準屏幕顯示矩形區域 |
刷新參數幾何關係圖解
Pad 內存座標系 標準屏幕座標系
+--------------------------+ +--------------------------+
| (py,px) | | (sy1,sx1) |
| ┌───────────┐ | | ┌───────────┐ |
| │ 顯示區域 │ | ───> | │ 可視窗口 │ |
| │ (sy2-sy1+1 │ | | │ (sy2-sy1+1 │ |
| │ 行, sx2-sx1+1列) | | │ 行, sx2-sx1+1列) |
| └───────────┘ | | └───────────┘ |
+--------------------------+ +--------------------------+
典型使用流程
// 1. 創建 1000x200 的巨大 pad
WINDOW* bigpad = newpad(1000, 200);
// 2. 向 pad 寫入大量數據(可超出屏幕)
for (int i = 0; i < 1000; i++) {
wprintw(bigpad, "Line %d: Data...\n", i);
}
// 3. 顯示 pad 中第 100-120 行,第 0-79 列到屏幕 (0,0) 到 (20,79)
prefresh(bigpad, 100, 0, 0, 0, 20, 79);
// 4. 實現滾動:循環調用 prefresh 修改 py 參數
// 可配合鍵盤輸入實現類似 less 的翻頁效果
高級用法:雙緩衝刷新
pnoutrefresh(pad1, py1, px1, sy1, sx1, ey1, ex1);
pnoutrefresh(pad2, py2, px2, sy2, sx2, ey2, ex2);
doupdate(); // 統一物理刷新
本文整理自「暗日」博客,原文地址:https://darksun.blog.51cto.com/3874064/1297302
- 上一篇文章: NCurses 庫窗口編程完全指南:創建、操作、刷新與屬性控制
- 下一篇文章: 單次掃描三路快排分區:負數/零/正數原地分類算法
Copyright ©
程式碼之家 All Rights Reserved