在之前的一章里我们使用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. GPS坐标转换

    由于经常涉及到GPS程序的编写,现在貌似这个GPS是越来越火,越来越多的朋友在编写GPS程序,估计是个人都会遇到这个GPS坐标转换的问题,很惭愧的是,作为一个测量专业出身的学生,我还得时不时的要把这些 ...

  2. Apache的httpd命令详解

    Apache的httpd命令详解 来源:全栈开发者 发布时间:2012-01-03 阅读次数:10965 4   httpd.exe为Apache HTTP服务器程序.直接执行程序可启动服务器的服务. ...

  3. 入门指引 - PHP手册笔记

    曾经简单的学习过PHP,看的是<PHP和MySQL Web开发>,还有万能的搜索引擎的帮助.这次准备系统的学习一下,参考资料是PHP Manual. PHP能做什么 PHP主要用于服务端的 ...

  4. Performance of Transport Control Protocol over Dynamic Spectrum Access Links

    2007 IEEE 这篇论文主要就是分析了几个版本的TCP协议在DSA网络中的性能问题. 得出的结论是: 大部分现在在用的几个版本的TCP协议,都是能够较好地适应DSA环境,吞吐率都能达到95%.当然 ...

  5. Oracle EBS-SQL (CST-1):检查BOM历史成本查询(Average Cost).sql

    select  msi1.segment1                   父件编码, msi1.description                  父件描述, msi1.primary_u ...

  6. Oracle EBS-SQL (OM-1):查询订单发货明细.sql

    select mtrh.request_number                  发货单号,       mmt.transaction_date                  发货时间,  ...

  7. AIX安装SSH

    AIX 7.1   进入 https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=aixbp 先下载需要的openssl,op ...

  8. matlab-----均值滤波函数的实现

    均值滤波的原理是对图像以一个区域(方形,圆形)等为模板,对该区域内的数据求平均后赋值给区域的中心 这种滤波方式原理简单,但是在滤波的同时会造成图像模糊. 本文将尝试对matlab中的filter2() ...

  9. Iptabels详解

    http://www.07net01.com/2016/02/1291283.html Iptabels是与linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的 ...

  10. 各种浏览器兼容篡位的css样式写法

    谷歌浏览器的识别 @media screen and (-webkit-min-device-pixel-ratio:0) { height:10px; } IE6特制识别的 *HTML .Searc ...