win32 国外大神的状态栏创建

 

步骤1。在OnInitStatusbarDialog函数中,加载并注册StatusBar控件类。
 

// Load and register IPAddress control class
    INITCOMMONCONTROLSEX iccx;
    iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccx.dwICC = ICC_BAR_CLASSES;
    if (!InitCommonControlsEx(&iccx))
        return FALSE;

第2步。创建状态栏控件。(使用CreateWindowEx)

 

HWND hStatusbar = CreateWindowEx(0, STATUSCLASSNAME, 0, 
        SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 
        rc.left, rc.top, rc.right, rc.bottom, 
        hWnd, (HMENU)IDC_STATUSBAR, g_hInst, 0);(需要宏定义#define IDC_STATUSBAR		11990)

第3步。确定状态栏的分区情况,它们的实际尺寸将在父窗口的WM_SIZE 
处理程序中设置。(SB_SETPARTS)

第4步。将一些文本放入状态栏的每个部分并设置其内容。
(SB_SETTEXT)

第5步。在OnStatusbarSize中,对状态栏进行分区以保持
其各部分的大小比例不变。调整状态栏的大小,使其始终与
父级客户区域的宽度相同。(SB_SETPARTS,WM_SIZE)

注意,在微软官方说明中,提及若需要让状态栏的大小随窗口大小动态改变,则需要把WM_SIZE的消息发送给status bar。

完整代码如下:

#pragma region Statusbar
// MSDN: Status Bar
// http://msdn.microsoft.com/en-us/library/bb760726.aspx

#define IDC_STATUSBAR		11990

//
//   FUNCTION: OnStatusbarSize(HWND, UINT, int, int)
//
//   PURPOSE: Process the WM_SIZE message
//
void OnStatusbarSize(HWND hWnd, UINT state, int cx, int cy)
{
    // Get the Statusbar control handle which was previously stored in the 
    // user data associated with the parent window.
    HWND hStatusbar = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);

    // Partition the statusbar here to keep the ratio of the sizes of its 
    // parts constant. Each part is set by specifing the coordinates of the 
    // right edge of each part. -1 signifies the rightmost part of the parent.
    int nHalf = cx / 2;
    int nParts[] = { nHalf, nHalf + nHalf / 3, nHalf + nHalf * 2 / 3, -1 };
    SendMessage(hStatusbar, SB_SETPARTS, 4, (LPARAM)&nParts);

    // Resize statusbar so it's always same width as parent's client area
    SendMessage(hStatusbar, WM_SIZE, 0, 0);
}

//
//   FUNCTION: OnInitStatusbarDialog(HWND, HWND, LPARAM)
//
//   PURPOSE: Process the WM_INITDIALOG message
//
BOOL OnInitStatusbarDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
    // Load and register IPAddress control class
    INITCOMMONCONTROLSEX iccx;
    iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccx.dwICC = ICC_BAR_CLASSES;
    if (!InitCommonControlsEx(&iccx))
        return FALSE;

    // Create the status bar control
    RECT rc = { 0, 0, 0, 0 };
    HWND hStatusbar = CreateWindowEx(0, STATUSCLASSNAME, 0, 
        SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 
        rc.left, rc.top, rc.right, rc.bottom, 
        hWnd, (HMENU)IDC_STATUSBAR, g_hInst, 0);

    // Store the statusbar control handle as the user data associated with 
    // the parent window so that it can be retrieved for later use.
    SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hStatusbar);

    // Establish the number of partitions or 'parts' the status bar will 
    // have, their actual dimensions will be set in the parent window's 
    // WM_SIZE handler.
    GetClientRect(hWnd, &rc);
    int nHalf = rc.right / 2;
    int nParts[4] = { nHalf, nHalf + nHalf / 3, nHalf + nHalf * 2 / 3, -1 };
    SendMessage(hStatusbar, SB_SETPARTS, 4, (LPARAM)&nParts);

    // Put some texts into each part of the status bar and setup each part
    SendMessage(hStatusbar, SB_SETTEXT, 0, (LPARAM)L"Status Bar: Part 1");
    SendMessage(hStatusbar, SB_SETTEXT, 1| SBT_POPOUT, (LPARAM)L"Part 2");
    SendMessage(hStatusbar, SB_SETTEXT, 2| SBT_POPOUT, (LPARAM)L"Part 3");
    SendMessage(hStatusbar, SB_SETTEXT, 3| SBT_POPOUT, (LPARAM)L"Part 4");

    return TRUE;
}

//
//  FUNCTION: StatusbarDlgProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the Statusbar control dialog.
//
//
INT_PTR CALLBACK StatusbarDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        // Handle the WM_INITDIALOG message in OnInitStatusbarDialog
        HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitStatusbarDialog);

        // Handle the WM_CLOSE message in OnClose
        HANDLE_MSG (hWnd, WM_CLOSE, OnClose);

        // Handle the WM_SIZE message in OnStatusbarSize
        HANDLE_MSG (hWnd, WM_SIZE, OnStatusbarSize);

    default:
        return FALSE;	// Let system deal with msg
    }
    return 0;
}

#pragma endregion

StatusbarDlgProc即为主窗口的处理函数。

最终可以创建一个4分栏的状态栏,且其大小可以随着窗口大小所改变

分享一个win32常用控件的demo连接:https://code.msdn.microsoft.com/CppWindowsCommonControls-9ea0de64

非常实用其中的示例控件包括Animation,ComboBoxEx,
Updown,Header,MonthCal,DateTimePick,ListView,TreeView,Tab,Tooltip,IP 
Address,Statusbar,Progress Bar,Toolbar,Trackbar和SysLink。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值