• StatusBar 状态栏


    statusbar.hpp
    #ifndef    statusbarH
    #define statusbarH

    #include <Windows.h>
    #include <list>

    namespace NSTS {
    class CStatusBar {
    public:
    CStatusBar (void);
    ~CStatusBar (void);

    public:
    bool Init (HWND hParent, int iSize, int agiParts[], HINSTANCE hInst);
    void Destroy (void);
    bool ResetParts (int iSize, int agiParts[]);
    void Flash (void);

    bool InsertCtrl (int iPos, HWND hwnd);
    bool RemoveCtrl (int iPos);

    bool SetText (int iPos, const TCHAR* pszText);
    bool GetText (int iPos, TCHAR* pszText);

    bool SetBk (COLORREF clrBk);
    COLORREF GetBk (void);

    bool SetIcon (int iPos, HICON hIcon);
    HICON GetIcon (int iPos);
    bool SetIcon (int iPos, UINT uId) {
    HICON hIcon = LoadIcon (m_hInst, MAKEINTRESOURCE (uId));
    bool fRet = SetIcon (iPos, hIcon);
    DestroyIcon (hIcon);
    return fRet;
    };

    bool SetTip (int iPos, const TCHAR* pszText);
    bool GetTip (int iPos, TCHAR* pszText);

    bool SetMinHeight (int iHeight);
    int GetHeight (void);

    bool SetUnicode (BOOL fUnicode);
    bool IsUnicode (void);

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

    private:
    void InsertCtrlCore (int iPos, HWND hwnd);

    private:
    HWND m_hSelf;
    HWND m_hParent;
    int m_iSize;
    HINSTANCE m_hInst;

    COLORREF m_clrBk;

    struct SBControl {
    int iPos;
    HWND hwnd;
    };
    std::list <CStatusBar::SBControl> m_lstCtrls; // used for RemoveCtrl
    };
    }

    #endif // statusbarH
    statusbar.cpp
    #include "statusbar.hpp"
    #include <commctrl.h>

    namespace NSTS {
    CStatusBar::
    CStatusBar (void)
    {
    m_hSelf = m_hParent = NULL;
    m_iSize = 0;
    m_hInst = NULL;
    m_clrBk = CLR_DEFAULT;
    m_lstCtrls.clear ();
    }


    CStatusBar::
    ~CStatusBar (void)
    {
    if (m_hSelf != NULL) {
    if (IsWindow (m_hSelf)) {
    DestroyWindow (m_hSelf);
    }
    m_hSelf = NULL;
    }

    m_hParent = NULL;
    m_hInst = NULL;
    m_clrBk = CLR_DEFAULT;
    m_lstCtrls.clear ();
    }

    bool
    CStatusBar::
    Init (HWND hParent, int iSize, int agiParts[], HINSTANCE hInst)
    {
    if (hParent == NULL || !IsWindow (hParent) ||
    iSize <=0 ||
    agiParts == NULL ||
    hInst == NULL) {
    return false;
    }

    InitCommonControls ();

    m_hInst = hInst;
    m_hSelf = CreateWindowEx(
    0, // no extended styles
    STATUSCLASSNAME, // name of status bar class
    (LPCTSTR) NULL, // no text when first created
    SBT_TOOLTIPS | SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, // creates a child window
    0, 0, 0, 0, // ignores size and position
    hParent, // handle to parent window
    NULL, // child window identifier
    hInst, // handle to application instance
    NULL); // no window creation data

    if (m_hSelf == NULL) {
    return false;
    }

    return ResetParts (iSize, agiParts);
    }

    void
    CStatusBar::
    Destroy (void)
    {
    if (m_hSelf != NULL && IsWindow (m_hSelf)) {
    DestroyWindow (m_hSelf);
    m_hSelf = NULL;
    }
    }

    bool
    CStatusBar::
    ResetParts (int iSize, int agiParts[])
    {
    HLOCAL hloc;
    int* piParts;
    int i;
    std::list <CStatusBar::SBControl>::iterator it;

    if (m_hSelf == NULL || !IsWindow (m_hSelf)) {
    return false;
    }

    hloc = LocalAlloc (LHND, sizeof (int) * iSize);

    if (hloc == NULL) {
    return false;
    }

    piParts = (int*)LocalLock (hloc);

    for (i = 0; i < iSize; ++i) {
    piParts [i] = agiParts [i];
    }

    SendMessage (m_hSelf, SB_SETPARTS, (WPARAM)iSize, (LPARAM)piParts);

    LocalUnlock (hloc);
    LocalFree (hloc);

    m_iSize = iSize;

    for (it = m_lstCtrls.begin (); it != m_lstCtrls.end (); ++it) {
    if (it->hwnd != NULL && IsWindow (it->hwnd)) {
    InsertCtrlCore (it->iPos, it->hwnd);
    }
    }

    return true;
    }


    void
    CStatusBar::
    Flash (void)
    {
    if (m_hSelf != NULL && IsWindow (m_hSelf)) {
    SendMessage (m_hSelf, WM_SIZE, (WPARAM)0, (LPARAM)0);
    }
    }


    void
    CStatusBar::
    InsertCtrlCore (int iPos, HWND hwnd)
    {
    RECT rcPart, rcCtrl;
    int iPartWidth, iCtrlWidth, iPartHeight, iCtrlHeight;
    int iWidth, iHeight, iX, iY;

    /** HWND, draw it */
    SendMessage (m_hSelf, SB_GETRECT, (WPARAM)iPos, (LPARAM)&rcPart);
    GetWindowRect (hwnd, &rcCtrl);

    iPartWidth = rcPart.right - rcPart.left;
    iPartHeight = rcPart.bottom - rcPart.top;
    iCtrlWidth = rcCtrl.right - rcCtrl.left;
    iCtrlHeight = rcCtrl.bottom - rcCtrl.top;

    iX = rcPart.left;

    if (iPartWidth < iCtrlWidth) {
    iWidth = iPartWidth;
    } else {
    iWidth = iCtrlWidth;
    }

    if (iPartHeight <= iCtrlHeight) {
    iHeight = iPartHeight;
    iY = rcPart.top;
    } else {
    iHeight = iCtrlHeight;
    iY = rcPart.top + (iPartHeight - iCtrlHeight) / 2;
    }


    /** set parent */
    SetParent (hwnd, m_hSelf);

    /** move it to the correct pos */
    MoveWindow (
    hwnd,
    iX,
    iY,
    iWidth,
    iHeight,
    FALSE);

    /** show it */
    ShowWindow (hwnd, SW_SHOW);
    }


    bool
    CStatusBar::
    InsertCtrl (int iPos, HWND hwnd)
    {
    CStatusBar::SBControl ctrl = {iPos, hwnd};

    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    hwnd == NULL || !IsWindow (hwnd) ||
    iPos < 0 || iPos >= m_iSize) {
    return false;
    }

    InsertCtrlCore (iPos, hwnd);

    m_lstCtrls.push_back (ctrl);
    return true;
    }


    bool
    CStatusBar::
    RemoveCtrl (int iPos)
    {
    std::list <CStatusBar::SBControl>::iterator it;
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iPos < 0 || iPos >= m_iSize) {
    return false;
    }

    for (it = m_lstCtrls.begin (); it != m_lstCtrls.end (); ++it) {
    if (it->iPos == iPos) {
    if (it->hwnd != NULL && IsWindow (it->hwnd)) {
    DestroyWindow (it->hwnd);
    m_lstCtrls.erase (it);
    return true;
    }
    m_lstCtrls.erase (it);
    return false;
    }
    }

    return false;
    }


    bool
    CStatusBar::
    SetText (int iPos, const TCHAR* pszText)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    pszText == NULL ||
    iPos < 0 || iPos >= m_iSize) {
    return false;
    }

    return SendMessage (m_hSelf, SB_SETTEXT, (WPARAM)iPos, (LPARAM)pszText);
    }

    bool
    CStatusBar::
    GetText (int iPos, TCHAR* pszText)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    pszText == NULL ||
    iPos < 0 || iPos >= m_iSize) {
    return false;
    }

    SendMessage (m_hSelf, SB_GETTEXT, (WPARAM)iPos, (LPARAM)pszText);
    return true;
    }

    bool
    CStatusBar::
    SetBk (COLORREF clrBk)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf)) {
    return false;
    }

    m_clrBk = SendMessage (m_hSelf, SB_SETBKCOLOR, (WPARAM)0, (LPARAM)clrBk);
    return true;
    }

    COLORREF
    CStatusBar::
    GetBk (void)
    {
    return m_clrBk;
    }

    bool
    CStatusBar::
    SetIcon (int iPos, HICON hIcon)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iPos < 0 || iPos >= m_iSize ||
    hIcon == NULL) {
    return false;
    }

    return SendMessage (m_hSelf, SB_SETICON, (WPARAM)iPos, (LPARAM)hIcon);
    }

    HICON
    CStatusBar::
    GetIcon (int iPos)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iPos < 0 || iPos >= m_iSize) {
    return NULL;
    }

    return (HICON)SendMessage (m_hSelf, SB_GETICON, (WPARAM)iPos, (LPARAM)0);
    }

    bool
    CStatusBar::
    SetTip (int iPos, const TCHAR* pszTip)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iPos < 0 || iPos >= m_iSize ||
    pszTip == NULL) {
    return false;
    }

    SendMessage (m_hSelf, SB_SETTIPTEXT, (WPARAM)iPos, (LPARAM)pszTip);
    return true;
    }

    bool
    CStatusBar::
    GetTip (int iPos, TCHAR* pszTip)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iPos < 0 || iPos >= m_iSize ||
    pszTip == NULL) {
    return false;
    }

    SendMessage (m_hSelf, SB_GETTIPTEXT, (WPARAM)iPos, (LPARAM)pszTip);
    return true;
    }

    bool
    CStatusBar::
    SetMinHeight (int iHeight)
    {
    std::list <CStatusBar::SBControl>::iterator it;
    if (m_hSelf == NULL || !IsWindow (m_hSelf) ||
    iHeight < 0) {
    return false;
    }

    SendMessage (m_hSelf, SB_SETMINHEIGHT, (WPARAM)iHeight, (LPARAM)0);
    Flash ();
    for (it = m_lstCtrls.begin (); it != m_lstCtrls.end (); ++it) {
    if (it->hwnd != NULL && IsWindow (it->hwnd)) {
    InsertCtrlCore (it->iPos, it->hwnd);
    }
    }
    return true;
    }


    int
    CStatusBar::
    GetHeight (void)
    {
    RECT rect;
    if (m_hSelf == NULL || !IsWindow (m_hSelf)) {
    return 0;
    }

    if (GetWindowRect (m_hSelf, &rect)) {
    return (rect.bottom - rect.top);
    }

    return 0;
    }

    bool
    CStatusBar::
    SetUnicode (BOOL fUnicode)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf)) {
    return false;
    }

    SendMessage (m_hSelf, SB_SETUNICODEFORMAT, (WPARAM)fUnicode, (LPARAM)0);
    return true;
    }

    bool
    CStatusBar::
    IsUnicode (void)
    {
    if (m_hSelf == NULL || !IsWindow (m_hSelf)) {
    return false;
    }

    return SendMessage (m_hSelf, SB_GETUNICODEFORMAT, (WPARAM)0, (LPARAM)0);
    }

    }

    Usage:

    NSTS::CStatusBar sb;

    extern HWND hParent;
    extern HINSTANCE hInst;

    int i = 3;
    int lens[3] = {100, 100, -1};
    sb.Init (hParent, 3, lens, hInst);

    sb.SetBk (...);
    sb.InsertCtrl (hwnd);
    ...

    ------

    在父窗口中,WM_SIZE:

    sb.Flash ();



     

  • 相关阅读:
    解决js计算0.1+0.2 !==0.3
    webpack 4 移除 CommonsChunkPlugin,取而代之的是两个新的配置项(optimization.splitChunks 和 optimization.runtimeChunk
    jq轮播图插件
    如何在 GitHub 的项目中创建一个分支呢?
    VUE图片剪辑插件 React图片剪辑插件
    前端图片压缩上传
    vue实现rsa加密,数字签名,md5加密等
    vue-class-component使用Mixins
    微信小程序--获取用户地理位置名称(无须用户授权)的方法
    [学习笔记]二进制分组
  • 原文地址:https://www.cnblogs.com/lin1270/p/2317232.html
Copyright © 2020-2023  润新知