状态栏函数:HWND CreateStatusBar(HWND hWnd,int x,int y,int width,int height)
{
HWND hStatusBar = CreateWindow(
TEXT("msctls_statusbar32"),
NULL,
WS_CHILD | WS_VISIBLE,
x,y,
width,height,
hWnd,
(HMENU)NULL,//没有菜单
hInst,//实例句柄
NULL//没有图标
);
if(!hStatusBar)
{
return NULL;
} ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hStatusBar);
return hStatusBar;
}调用HWND hStatusBar = CreateStatusBar(hWnd,0,0,800,22);
hEdit = CreateEdit(hWnd,0,0,800,400);文本框的有创建出来。但是状态栏的没看见啊。。返回的是NULL。还有有时候可以创建出来有时候不可以。
一样的代码。。为什么会这样呢?
{
HWND hStatusBar = CreateWindow(
TEXT("msctls_statusbar32"),
NULL,
WS_CHILD | WS_VISIBLE,
x,y,
width,height,
hWnd,
(HMENU)NULL,//没有菜单
hInst,//实例句柄
NULL//没有图标
);
if(!hStatusBar)
{
return NULL;
} ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hStatusBar);
return hStatusBar;
}调用HWND hStatusBar = CreateStatusBar(hWnd,0,0,800,22);
hEdit = CreateEdit(hWnd,0,0,800,400);文本框的有创建出来。但是状态栏的没看见啊。。返回的是NULL。还有有时候可以创建出来有时候不可以。
一样的代码。。为什么会这样呢?
init.dwSize = sizeof(INITCOMMONCONTROLSEX);
init.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&init);
HWND hwndBar ;
hwndBar = CreateStatusWindow(WS_CHILD | WS_VISIBLE, TEXT( "0"), hwnd, IDC_STATUS);
SendMessage(hwndBar,SB_SETPARTS,3,(LPARAM)pint);
SendMessage(hwndBar,SB_SETTEXT,1,(LPARAM)TEXT("1"));
SendMessage(hwndBar,SB_SETTEXT,2,(LPARAM)TEXT("2"));
LPCTSTR lpszText,
HWND hwndParent,
UINT wID
);
wID是窗口控件的ID但是我这边用sdk写的那里来的控件ID。
==============
控件ID就是一个常量,如果你后面用不到这个ID,你随便写个数字,比如:12,13之类都行,只要不和别的控件重复。
如果你不想写数字,你可以打开resource.h,在里面加入一行:#define IDS_STATUS 1055
然后用IDS_STATUS作为ID,前面的1055也是随便写的。
//#include "stdafx.h"
#include "NotePad.h"#define MAX_LOADSTRING 100// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HWND CreateEdit(HWND hWnd,int x,int y,int width,int height);
HWND CreateStatusBar(HWND hWnd,int x,int y,int width,int height);
void MoveEdit(HWND hMain,HWND hEdit);
void MoveEditEx(HWND hMain,HWND hEdit,int ShowStatusBar);
void MoveStatusBar(HWND hMain,HWND hEdit);int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
hInst = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable; // 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_NOTEPAD, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NOTEPAD)); // 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return (int) msg.wParam;
}//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NOTEPAD));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_NOTEPAD);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex);
}//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd; hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
} ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); return TRUE;
}//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit = NULL;
static HWND hStatusBar = NULL;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc; switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
//状态栏
case IDM_StatusBar:
{
HMENU hMenu = GetMenu(hWnd);
if(MF_CHECKED & GetMenuState(hMenu,IDM_StatusBar,MF_BYCOMMAND))
{
CheckMenuItem(hMenu,IDM_StatusBar,MF_UNCHECKED);
ShowWindow(hStatusBar,SW_HIDE);//隐藏状态栏
MoveEditEx(hWnd,hEdit,0);
}
else
{
CheckMenuItem(hMenu,IDM_StatusBar,MF_CHECKED);
ShowWindow(hStatusBar,SW_NORMAL);//显示状态栏
MoveEditEx(hWnd,hEdit,1);
}
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
{
hEdit = CreateEdit(hWnd,0,0,800,400);
hStatusBar = CreateStatusBar(hWnd,0,0,800,22);
ShowWindow(hStatusBar,SW_HIDE);//隐藏状态栏
break;
}
case WM_SIZE:
{
MoveEdit(hWnd,hEdit);
MoveStatusBar(hWnd,hStatusBar);
break;
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
// =============================================
// 函数功能: <创建文本框>
// 函数参数: hWnd父窗口句柄,x、y窗口位置
// width、height 窗口大小
// 返回值 : 失败返回NULL,成功返回句柄。
// =============================================
HWND CreateEdit(HWND hWnd,int x,int y,int width,int height)
{
HWND hEdit = CreateWindow(
TEXT("edit"),//文本控件
NULL,//没有标题
WS_CHILDWINDOW|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL//窗口样式
|ES_LEFT|ES_MULTILINE|ES_NOHIDESEL,//文本框样式
x,y,//位置
width,height,//大小
hWnd,//父窗口句柄
(HMENU)NULL,//没有菜单
hInst,//实例句柄
NULL//没有图标
);
if(!hEdit)
{
return NULL;
} ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hEdit);
return hEdit;
}
// =============================================
// 函数功能: <创建状态栏>
// 函数参数: hWnd父窗口句柄,x、y窗口位置
// width、height 窗口大小
// 返回值 : 失败返回NULL,成功返回句柄。
// =============================================
HWND CreateStatusBar(HWND hWnd,int x,int y,int width,int height)
{
HWND hStatusBar = CreateWindow(
TEXT("msctls_statusbar32"),
NULL,
WS_CHILD | WS_VISIBLE,
x,y,
width,height,
hWnd,
(HMENU)NULL,//没有菜单
hInst,//实例句柄
NULL//没有图标
);
if(!hStatusBar)
{
DWORD i = GetLastError();
TCHAR Str[256];
_itow(i,Str,10);
MessageBox(hWnd,Str,NULL,MB_OK);
return NULL;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hStatusBar);
return hStatusBar;
}
//文本框随父窗口缩放
void MoveEdit(HWND hMain,HWND hEdit)
{
RECT rect;
GetClientRect(hMain,&rect);
MoveWindow(hEdit,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE/*是否重绘*/);
}
void MoveEditEx(HWND hMain,HWND hEdit,int ShowStatusBar)
{
RECT rect;
GetClientRect(hMain,&rect);
if(ShowStatusBar)
{
MoveWindow(hEdit,rect.left,rect.top,rect.right-rect.left,(rect.bottom-rect.top)-20,TRUE/*是否重绘*/);
}
else
{
MoveWindow(hEdit,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE/*是否重绘*/);
}
}
//状态栏随父窗口缩放
void MoveStatusBar(HWND hMain,HWND hStatusBar)
{
RECT rect;
GetClientRect(hMain,&rect);
MoveWindow(hStatusBar,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE/*是否重绘*/);
}
无法识别的窗口类型,
所以会创建失败
"msctls_statusbar32" 改为 STATUSCLASSNAME
initcommoncontrols();