当前位置:C++技术网 > 资讯 > Win32创建一个状态栏源码分享

Win32创建一个状态栏源码分享

更新时间:2016-09-25 23:20:20浏览次数:1+次

先看运行效果:

Win32创建一个状态栏

代码:


#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#define WINDOW_CLASS_NAME _T("CJJJS.COM")
LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch(msg)
    {
    case WM_CREATE:
            return 0;
    case WM_PAINT:
            hdc=BeginPaint(hwnd,&ps);
            TextOut(hdc,50,50,_T("www.cjjjs.com"),lstrlen(_T("www.cjjjs.com")));
            EndPaint(hwnd,&ps);
            return 0;
    case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    default:
        return DefWindowProc( hwnd, msg, wparam, lparam );
    }
}

HWND CreateStatusBar(HWND hwndParent,int nStatusID,HINSTANCE hinst,int nParts)       
{
    //nParts:状态栏分为的份数
    HWND hwndStatus;
    RECT rcClient;
    HLOCAL hloc;
    LPINT lpParts;
    int i, nWidth;

    //创建状态栏
    hwndStatus = CreateWindowEx(0,STATUSCLASSNAME,(LPCTSTR) NULL,SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE,
        0, 0, 0, 0,hwndParent,(HMENU) nStatusID,hinst,NULL);

    GetClientRect(hwndParent, &rcClient);
    hloc = LocalAlloc(LHND, sizeof(int) * nParts);
    lpParts = (int*)LocalLock(hloc);

    //计算状态栏中每部分的宽度
    nWidth = rcClient.right / nParts;
    for (i = 0; i < nParts; i++)
    {
        lpParts[i] = nWidth;
        nWidth += nWidth;
    }

    //把状态栏分为几部分
    SendMessage(hwndStatus, SB_SETPARTS, (WPARAM) nParts,(LPARAM) lpParts);
    LocalUnlock(hloc);
    LocalFree(hloc);
    //返回状态栏的句柄
    return hwndStatus;
}
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
    WNDCLASSEX  winclass;
    HWND        hwnd;
    MSG         msg;
    winclass.cbSize     =sizeof(WNDCLASSEX);
    winclass.style      =CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
    winclass.lpfnWndProc=WindowProc;
    winclass.cbClsExtra =0;
    winclass.cbWndExtra =0;
    winclass.hInstance  =hinstance;
    winclass.hIcon      =LoadIcon(NULL,IDI_APPLICATION);
    winclass.hCursor     =LoadCursor(NULL,IDC_ARROW);
    winclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    winclass.lpszMenuName =NULL;
    winclass.lpszClassName=WINDOW_CLASS_NAME;
    winclass.hIconSm      =LoadIcon(NULL,IDI_APPLICATION);

    if(!RegisterClassEx(&winclass))
        return(0);
    if(!(hwnd=CreateWindowEx(NULL,WINDOW_CLASS_NAME,_T("cjjjs.com"),
        WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,650,600,NULL,NULL,hinstance,NULL)))
        return 0;
    //创建状态栏
    CreateStatusBar(hwnd,60105,hinstance,4);
    ShowWindow(hwnd, SW_SHOWDEFAULT);
    UpdateWindow(hwnd);
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return(msg.wParam);
}
    创建状态栏就是一个函数CreateStatusBar。然后一个关键的窗口类名就是STATUSCLASSNAME,这也就是状态栏的类名。状态栏就和标准控件一样,可以通过窗口类直接创建出来。然后就是状态栏有几个分隔,一个状态栏就出来了。状态栏的文字设置,查询状态栏窗口的一些API函数就可以了。