在之前的一章里我们使用InvalidateRect函数,生成窗口重绘消息进行重绘,但是并没有在处理滚动条消息时直接绘制,这样的代码效率并不高。

这里作者使用了UpdateWindow函数,直接进行窗口的重绘。同时使用新的滚动条函数 SetScrollInfo 和 GetScrollInfo。

这两个函数不仅包括上一篇中使用的四个函数,而且还增加了两个新功能:调整滑块大小、指定滑块位置。

int SetScrollInfo(
__in HWND hwnd,
__in int fnBar,
__in LPCSCROLLINFO lpsi,
__in BOOL fRedraw
);
BOOL GetScrollInfo(
__in HWND hwnd,
__in int fnBar,
__inout LPSCROLLINFO lpsi
);

第二个参数是指定类型的滚动条,可以是下面的值:

Value Meaning
SB_CTL

Retrieves the parameters for a scroll bar control. The hwnd parameter must be the handle to the scroll bar control.

SB_HORZ

Retrieves the parameters for the window's standard horizontal scroll bar.

SB_VERT

Retrieves the parameters for the window's standard vertical scroll bar.

第三个参数是一个结构体

typedef struct tagSCROLLINFO {
UINT cbSize;
UINT fMask;
int nMin;
int nMax;
UINT nPage;
int nPos;
int nTrackPos;
} SCROLLINFO, **LPCSCROLLINFO;

第四个参数是bool型,表示是否需要根据新的信息重绘滚动条。

下面是代码:

#define WINVER 0x0500
#include <windows.h>
#include "sysmets.h" LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SysMets3") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return ;
} hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 3"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
HDC hdc ;
int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
PAINTSTRUCT ps ;
SCROLLINFO si ;
TCHAR szBuffer[] ;
TEXTMETRIC tm ; switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ; GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & ? : ) * cxChar / ;
cyChar = tm.tmHeight + tm.tmExternalLeading ; ReleaseDC (hwnd, hdc) ; // Save the width of the three columns iMaxWidth = * cxChar + * cxCaps ;
return ; case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ; // Set vertical scroll bar range and page size si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = ;
si.nMax = NUMLINES - ;
si.nPage = cyClient / cyChar ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ; // Set horizontal scroll bar range and page size si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = ;
si.nMax = + iMaxWidth / cxChar ;
si.nPage = cxClient / cxChar ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
return ; case WM_VSCROLL:
// Get all the vertial scroll bar information si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si) ; // Save the position for comparison later on iVertPos = si.nPos ; switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ; case SB_BOTTOM:
si.nPos = si.nMax ;
break ; case SB_LINEUP:
si.nPos -= ;
break ; case SB_LINEDOWN:
si.nPos += ;
break ; case SB_PAGEUP:
si.nPos -= si.nPage ;
break ; case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ; case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ; default:
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set. si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hwnd, SB_VERT, &si) ; // If the position has changed, scroll the window and update it if (si.nPos != iVertPos)
{
ScrollWindow (hwnd, , cyChar * (iVertPos - si.nPos),
NULL, NULL) ;
UpdateWindow (hwnd) ;
}
return ; case WM_HSCROLL:
// Get all the vertial scroll bar information si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ; // Save the position for comparison later on GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ; switch (LOWORD (wParam))
{
case SB_LINELEFT:
si.nPos -= ;
break ; case SB_LINERIGHT:
si.nPos += ;
break ; case SB_PAGELEFT:
si.nPos -= si.nPage ;
break ; case SB_PAGERIGHT:
si.nPos += si.nPage ;
break ; case SB_THUMBPOSITION:
si.nPos = si.nTrackPos ;
break ; default :
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set. si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
GetScrollInfo (hwnd, SB_HORZ, &si) ; // If the position has changed, scroll the window if (si.nPos != iHorzPos)
{
ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), ,
NULL, NULL) ;
}
return ; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ; // Get vertical scroll bar position si.cbSize = sizeof (si) ;
si.fMask = SIF_POS ;
GetScrollInfo (hwnd, SB_VERT, &si) ;
iVertPos = si.nPos ; // Get horizontal scroll bar position GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ; // Find painting limits iPaintBeg = max (, iVertPos + ps.rcPaint.top / cyChar) ;
iPaintEnd = min (NUMLINES - ,
iVertPos + ps.rcPaint.bottom / cyChar) ; for (i = iPaintBeg ; i <= iPaintEnd ; i++)
{
x = cxChar * ( - iHorzPos) ;
y = cyChar * (i - iVertPos) ; TextOut (hdc, x, y,
sysmetrics[i].szLabel,
lstrlen (sysmetrics[i].szLabel)) ; TextOut (hdc, x + * cxCaps, y,
sysmetrics[i].szDesc,
lstrlen (sysmetrics[i].szDesc)) ; SetTextAlign (hdc, TA_RIGHT | TA_TOP) ; TextOut (hdc, x + * cxCaps + * cxChar, y, szBuffer,
wsprintf (szBuffer, TEXT ("%5d"),
GetSystemMetrics (sysmetrics[i].iIndex))) ; SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
} EndPaint (hwnd, &ps) ;
return ; case WM_DESTROY :
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

windows程序设计读书笔记4——字符显示3的更多相关文章

  1. windows程序设计读书笔记2——字符显示1

    本程序使用GetSystemMetrics获取windows各种图像选项,并输出字符到窗口中. #define WINVER 0x0500 #include <windows.h> #in ...

  2. windows程序设计读书笔记3——字符显示2

    由于显示的字符可能会不全,我们很容易想到的一个解决办法是使用滚动条. 先看一下代码,再进行分析: /*------------------------------------------------- ...

  3. windows程序设计读书笔记1——创建窗口

    第一个win32程序,简单的创建窗口: #include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ...

  4. Windows内核读书笔记——Windows异常分发处理机制

    本篇读书笔记主要参考自<深入解析Windows操作系统>和<软件调试>这两本书. IDT是处理异常,实现操作系统与CPU的交互的关口. 系统在初始化阶段会去填写这个结构. ID ...

  5. Windows程序设计学习笔记(一)Windows内存管理初步

    学习Windows程序设计也有一些时间了,为了记录自己的学习成果,以便以后查看,我希望自己能够坚持写下一系列的学习心得,对自己学习的内容进行总结,同时与大家交流.因为刚学习所以可能有的地方写不不正确, ...

  6. javascript高级程序设计读书笔记-事件(一)

    读书笔记,写的很乱   事件处理程序   事件处理程序分为三种: 1.html事件2. DOM0级,3,DOM2级别  没有DOM1 同样的事件 DOM0会顶掉html事件   因为他们都是属性  而 ...

  7. Windows程序设计学习笔记(1):一个简单的windows程序

    <Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...

  8. AngularJS高级程序设计读书笔记 -- 大纲篇

    零. 初衷 现在 AngularJS 4 已经发布了, 楼主还停留在 1.x 的阶段, 深感自卑. 学习 AngularJS 的初衷是因为, 去年楼主开始尝试使用 Flask 开发自动化程序, 需要用 ...

  9. 关于Lua程序设计{读书笔记}

    1.lua中的标识符可以是由任意字母.数字和下划线构成的字符串,但不能以数字开头.2.lua将通常类似"_VALUE"的标识符作为保留标识符3.lua的保留字 and break ...

随机推荐

  1. codeforces #330 div2

    A: #include<cstdio> #include<algorithm> #include<cmath> #include<map> #inclu ...

  2. Android布局控件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  3. mysql数据库数据恢复方案概括总结

    方案一:(传统方案) 备份+binlog日志增量: 方案二:(针对update.delete语句忘加where的情况) Binlog日志文件中保存有错误操作之前和之后的两组数据,将错误操作之前的数据修 ...

  4. 记微信开发(自定义回复&关注回复)

    记微信开发(自定义回复&关注回复) 记微信开发(自定义回复&关注回复) code: <?php/** * wechat php test *///define your toke ...

  5. window.location.href("url") 无法在chrome和Firefoxz中使用

    今天在js代码中加了一句window.location.href(‘url’)希望实现页面的跳转,IE中可以正常使用,但是Firefox却提示window.location is not a func ...

  6. ruby和Python简单对比

    前不久学了ruby,发现ruby和Python非常像,于是自个测试对比了下,测完了才知道网上有现成的……下面是测试结果   序列(包括列表和元组等)有分片的特点:可能会比较方便的提取其中特定元素,暂时 ...

  7. SQL Server 内存管理在64位时代的改变

    64位机上  地址空间比以前大了去了.它引起的改变多了去了 1.MemToLeave这个词不存在了.因为SQL Server以不再做这种预留空间的事了,也就是说multiple page 想用多少就用 ...

  8. Azure File SMB3.0文件共享服务(5)

      使用Java管理Azure文件共享服务   Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Java来访问A ...

  9. COB Epoxy灌膠時氣泡產生的原因與解決方法

    COB的黑膠 (Epoxy)有氣泡通常是不被允許的,因為外部氣孔不但會影響到外觀,內部氣孔更有可能會破壞 Wire bonding 的鋁線穩定度.既使在COB製程剛完成的時候沒有通過功能測試,也不代表 ...

  10. Linux基本命令(开发常用的、电脑常用的)

    一.开发常用的 ###转自:http://www.weixuehao.com/archives/25#usergroup 转自:http://blog.csdn.net/ljianhui/articl ...