visual c++中如何用CreateWindow函数创建状态栏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了visual c++中如何用CreateWindow函数创建状态栏相关的知识,希望对你有一定的参考价值。

vs2010编译环境我看了一下commctrl的定义,其中有statusbar但不知道如何创建

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>

#define WINDOW_CLASS_NAME _T("WINCLASS1")

LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)

PAINTSTRUCT ps;
HDC hdc;
switch(msg)

case WM_CREATE:

return(0);

break;
case WM_PAINT:

hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);

break;
case WM_DESTROY:

PostQuitMessage(0);
return(0);

break;
default:
return( DefWindowProc( hwnd, msg, wparam, lparam ));



HWND DoCreateStatusBar(HWND hwndParent,//主窗口句柄
int nStatusID, //状态栏标识符
HINSTANCE hinst,//应用程序实例
int nParts //状态栏分为的份数
)

HWND hwndStatus;
RECT rcClient;
HLOCAL hloc;
LPINT lpParts;
int i, nWidth;

//加载动态连接库
InitCommonControls();
//创建状态栏
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(BLACK_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("Basic Window"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0,
400,400,
NULL,
NULL,
hinstance,
NULL)))
return(0);

DoCreateStatusBar(hwnd,60105,hinstance,4);
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))

TranslateMessage(&msg);
DispatchMessage(&msg);

return(msg.wParam);


用windows api创建状态栏
项目,属性,链接器,输入,附加依赖项把comctl32.lib加进去。
参考技术A 状态栏的创建你看下MFC单文档视结构,的FRAM.CPP,有如下程序 if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))

TRACE0("Failed to create status bar\n");
return -1; // fail to create
其中m_wndStatusBar就是statusbar的对象,m_wndStatusBar.Create(this)//意思就是给框架窗创建状态栏m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) //应该设置状态栏大小 具体看MSDN
参考技术B 你新建一个单文档程序,然后在在单文档的程序中有创建状态栏的代码,具体的我忘记了,好像在OnCreate里呢,补充:你可以参考里面的,然后写在你的消息泵中啊 参考技术C m_wndStatusBar.Create(this) ||

以上是关于visual c++中如何用CreateWindow函数创建状态栏的主要内容,如果未能解决你的问题,请参考以下文章

c++中如何用函数传递fstream类型

在C++中如何用schema校验xml文件

在c++中如何用new生成一个构造函数带参数的类数组?

如何用visual studio2012编c++程序?

如何用visual studio 2010 写c++程序

Keil中如何用Keil中如何用汇编调用C函数?