位运算概述:
有些位运算可以关作为闭位的手段,屏蔽掉某些位,如奇偶校验位。有些位运算相反,可以用来置位,直接对结果变量的每一位分别处理,移位运算可以对外部设备(如 D/A转换器)的输入和状态信息进行译码,移运算还可以用于整数的快速乘、除运算。
下表列出了位运算的运算符和其对应的功能,位运算的操作对象主要是 整形数据、字符型数据
下面就具体的针对这些运算符在实际中使用进行举例:
CRect rect;
m_listCtrl.GetClientRect(&rect);
//设置列表控件风格
DWORD dwStyle;
dwStyle=::GetWindowLong(m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
dwStyle=m_listCtrl.GetExtendedStyle();
dwStyle|=LVS_EX_FULLROWSELECT;//LVS_EX_GRIDLINES|
//设置扩展风格
m_listCtrl.SetExtendedStyle(dwStyle);
m_listCtrl.InsertColumn(0, "姓名", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(1, "关系", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(2, "单位", LVCFMT_LEFT, rect.Width());
m_listCtrl.InsertColumn(3, "照片", LVCFMT_LEFT, rect.Width());
//二级类别不用来显示,别有它用所以设为0
m_listCtrl.InsertColumn(4, "二级类别", LVCFMT_LEFT, rect.Width());
我们先看一下,下边几个宏的定义:
#define GWL_WNDPROC (-4)
#define GWL_HINSTANCE (-6)
#define GWL_HWNDPARENT (-8)
#define GWL_STYLE (-16)
#define GWL_EXSTYLE (-20)
#define GWL_USERDATA (-21)
#define GWL_ID (-12)
// begin_r_commctrl
#define LVS_ICON 0x0000
#define LVS_REPORT 0x0001
#define LVS_SMALLICON 0x0002
#define LVS_LIST 0x0003
#define LVS_TYPEMASK 0x0003
#define LVS_SINGLESEL 0x0004
#define LVS_SHOWSELALWAYS 0x0008
#define LVS_SORTASCENDING 0x0010
#define LVS_SORTDESCENDING 0x0020
#define LVS_SHAREIMAGELISTS 0x0040
#define LVS_NOLABELWRAP 0x0080
#define LVS_AUTOARRANGE 0x0100
#define LVS_EDITLABELS 0x0200
#define LVS_EX_GRIDLINES 0x00000001
#define LVS_EX_SUBITEMIMAGES 0x00000002
#define LVS_EX_CHECKBOXES 0x00000004
#define LVS_EX_TRACKSELECT 0x00000008
#define LVS_EX_HEADERDRAGDROP 0x00000010
#define LVS_EX_FULLROWSELECT 0x00000020 // applies to report mode only
#define LVS_EX_ONECLICKACTIVATE 0x00000040
#define LVS_EX_TWOCLICKACTIVATE 0x00000080
#if (_WIN32_IE >= 0x0400)
#define LVS_EX_FLATSB 0x00000100
#define LVS_EX_REGIONAL 0x00000200
#define LVS_EX_INFOTIP 0x00000400 // listview does InfoTips for you
#define LVS_EX_UNDERLINEHOT 0x00000800
#define LVS_EX_UNDERLINECOLD 0x00001000
#define LVS_EX_MULTIWORKAREAS 0x00002000
由此可以看出
dwStyle |= LVS_REPORT | LVS_SHOWSELALWAYS | LVS_EDITLABELS;这句代码是进行了相应的按位或操作。但是为什么要进行对数据的宏定义,然后还要一这种位运算的方式来操作宏呢?代码的实现部分究竟做了些什么?
未完待续