HeaderCtrl 头控件 ---未完成,请勿使用

headerctrl.hpp
#ifndef headerctrlH
#define headerctrlH

#include <Windows.h>
#include <commctrl.h>
#include <assert.h>

//#pragma comment (lib, "comctl32.lib")


namespace NSTS {

class CHeaderCtrl {
public:
CHeaderCtrl (void) { m_hSelf = NULL; m_hInst = NULL;};
~CHeaderCtrl (void) {};

bool Init (HINSTANCE hInst, HWND hParent, DWORD dwStyle = WS_CHILD | HDS_BUTTONS | HDS_HORZ | HDS_FLAT) {
RECT rcParent;
HDLAYOUT hdl;
WINDOWPOS wp;
INITCOMMONCONTROLSEX iccx;

iccx.dwSize = sizeof (iccx);
iccx.dwICC = ICC_LISTVIEW_CLASSES;

// Ensure that the common control DLL is loaded, and then create
// the header control.
InitCommonControlsEx (&iccx);

if ((m_hSelf = CreateWindowEx(0,
WC_HEADER,
(LPCTSTR) NULL,
dwStyle,
0, 0, 0, 0,
hParent,
(HMENU) NULL,
hInst,
(LPVOID) NULL)) == NULL) {
return false;
}

m_hInst = hInst;
// Retrieve the bounding rectangle of the parent window's
// client area, and then request size and position values
// from the header control.
GetClientRect(hParent, &rcParent);

hdl.prc = &rcParent;
hdl.pwpos = &wp;
if (!SendMessage(m_hSelf, HDM_LAYOUT, 0, (LPARAM) &hdl)) {
Destroy ();
return false;
}

// Set the size, position, and visibility of the header control.
SetWindowPos(m_hSelf,
wp.hwndInsertAfter,
wp.x, wp.y, wp.cx, wp.cy,
wp.flags | SWP_SHOWWINDOW);
return true;
};
void Destroy (void) {
HIMAGELIST himl;
assert (IsWindow(m_hSelf));
himl = Header_GetImageList (m_hSelf);
if (himl) {
ImageList_Destroy (himl);
}

DestroyWindow (m_hSelf);
m_hSelf = NULL;


};
bool Attach (HWND hwnd) {
assert (IsWindow (hwnd));
m_hSelf = hwnd;
return true;
};
void Dettach (void) {
m_hSelf = NULL;
};

DWORD GetStyle (void) const { return GetWindowLong (m_hSelf, GWL_STYLE); };
bool SetStyle (DWORD dwStyle) { return SetWindowLong (m_hSelf, GWL_STYLE, dwStyle); };

HWND GetSelf (void) const { return m_hSelf; };
HWND GetParent (void) const { return ::GetParent (m_hSelf); };

public:
int GetItemCount (void) const {
assert (IsWindow (m_hSelf));
return Header_GetItemCount (m_hSelf);
};
bool InsertItem (LPTSTR lpszText, int iWidth = 100, int iPos = -1, LPARAM lParam = 0) {
HDITEM item;

assert (IsWindow (m_hSelf));
assert (lpszText);

item.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
item.pszText = lpszText;
item.cchTextMax = 64;
item.cxy = iWidth;
item.fmt = HDF_LEFT | HDF_STRING;

item.lParam = lParam;

return (Header_InsertItem (m_hSelf, iPos == -1 ? GetItemCount () : iPos, &item) != -1);
};
bool InsertItem (UINT uBmp, int iWidth = 100, int iPos = -1, LPARAM lParam = 0) {
HBITMAP hbmp;
bool fRet;

assert (m_hInst);

hbmp = LoadBitmap (m_hInst, MAKEINTRESOURCE (uBmp));
if (hbmp == NULL) return false;

fRet = InsertItem (hbmp, iWidth, iPos, lParam);
DeleteObject (hbmp);
return fRet;
};
bool InsertItem (HBITMAP hBmp, int iWidth = 100, int iPos = -1, LPARAM lParam = 0) {
HDITEM item;

assert (IsWindow (m_hSelf));

item.mask = HDI_BITMAP | HDI_WIDTH | HDI_LPARAM | HDI_FORMAT;
item.cxy = iWidth;
item.hbm = hBmp;
item.lParam = lParam;
item.fmt = HDF_BITMAP_ON_RIGHT;

return (Header_InsertItem (m_hSelf, iPos == -1 ? GetItemCount () : iPos, &item) != -1);
};
bool SetItemBmp (int iIndex, UINT uBmp) {
HD_ITEM item;
HBITMAP hbmp;
bool fRet = false;

hbmp = LoadBitmap (m_hInst, MAKEINTRESOURCE (uBmp));
assert (hbmp);

item.mask = HDI_BITMAP | HDI_FORMAT;
if (Header_GetItem (m_hSelf, iIndex, &item)) {
item.fmt |= HDF_BITMAP;
item.hbm = hbmp;
if (Header_SetItem (m_hSelf, iIndex, &item)) {
fRet = true;
}
}

DeleteObject (hbmp);
return fRet;
};
bool RemoveItem (int iPos) {
assert (IsWindow (m_hSelf));
return Header_DeleteItem (m_hSelf, iPos);
};
int GetItemText (TCHAR* pszText, int iLen, int iPos) {
HDITEM item;

assert (IsWindow (m_hSelf));

if (pszText) {
item.mask = HDI_TEXT;
item.pszText = pszText;
item.cchTextMax = iLen;
return Header_GetItem (m_hSelf, iPos, &item);
}
return false;
};
bool SetImageList (HIMAGELIST himl) {
HIMAGELIST himlOld;
assert (IsWindow (m_hSelf));

himlOld = Header_SetImageList (m_hSelf, himl);
if (himlOld != NULL) {
ImageList_Destroy (himlOld);
}
return true;
};
bool SetItemImage (int iIndex, int iPos) {
HDITEM item;
assert (IsWindow (m_hSelf));
item.mask = HDI_IMAGE | HDI_FORMAT;
item.iImage = iIndex;
item.fmt = HDF_IMAGE;

return Header_SetItem (m_hSelf, iPos, &item);
};
bool EnableDragItem (bool fEnable = true) {
if (fEnable) {
return SetStyle (GetStyle () | HDS_DRAGDROP);
} else {
return SetStyle (GetStyle () & ~HDS_DRAGDROP);
}
};
bool ShowSortDownArrow (int iIndex, bool fShow = true) {
HDITEM item;
assert (IsWindow (m_hSelf));
item.mask = HDI_FORMAT;

if (Header_GetItem (m_hSelf, iIndex, &item)) {
//item.fmt &= ~HDF_IMAGE;
//item.fmt &= ~HDF_BITMAP;
//item.fmt &= ~HDF_BITMAP_ON_RIGHT;
item.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
if (fShow) {
item.fmt |= HDF_SORTDOWN;
}
return Header_SetItem (m_hSelf, iIndex, &item);
}
return false;
};
bool ShowSortUpArrow (int iIndex, bool fShow = true) {
HDITEM item;
assert (IsWindow (m_hSelf));
item.mask = HDI_FORMAT;

if (Header_GetItem (m_hSelf, iIndex, &item)) {
//item.fmt &= ~HDF_IMAGE;
//item.fmt &= ~HDF_BITMAP;
//item.fmt &= ~HDF_BITMAP_ON_RIGHT;
if (fShow) {
item.fmt |= HDF_SORTUP;
} else {
item.fmt &= ~HDF_SORTUP;
}
return Header_SetItem (m_hSelf, iIndex, &item);
}
return false;
};
LPARAM GetLParam (int iPos) const {
HDITEM item;
assert (IsWindow (m_hSelf));
item.mask = HDI_LPARAM;

if (Header_GetItem (m_hSelf, iPos, &item)) {
return item.lParam;
}
return (LPARAM)0;
};
bool SetLParam (int iPos, LPARAM lParam) {
HDITEM item;
assert (IsWindow (m_hSelf));
item.mask = HDI_LPARAM;
item.mask = lParam;

return Header_SetItem (m_hSelf, iPos, &item);
};

private:
HWND m_hSelf;
HINSTANCE m_hInst;
};

}

#endif // headerctrlH
posted @ 2012-02-06 09:58  夜雨無聲  阅读(498)  评论(0编辑  收藏  举报