win32下给状态栏分三栏,只能见到第一栏

wencan2010 2010-09-26 10:38:54
效果如下:

代码如下:
int xPos;
int yPos;
char str[50];
RECT rectClient,rectWindow;
int *pInt=new int[3];
……
case WM_CREATE:
//-------创建主状态栏
//hWndStatus=CreateStatusWindow(WS_CHILD|WS_VISIBLE|SBS_SIZEGRIP,NULL,hWnd,IDC_STATUS);
InitCommonControls();
hWndStatus=CreateWindowEx(
0, // no extended styles
STATUSCLASSNAME, // name of status bar class
(PCTSTR) NULL, // no text when first created
SBARS_SIZEGRIP | // includes a sizing grip
WS_CHILD | WS_VISIBLE, // creates a visible child window
0, 0, 0, 0, // ignores size and position
hWnd, // handle to parent window
(HMENU)IDC_STATUS, // child window identifier
hInst, // handle to application instance
NULL); // no window creation data
break;
case WM_SIZE:
::GetClientRect(hWnd,&rectClient);
for(int itemWidth=(rectClient.right-rectClient.left)/3,i=0;i<3;i++,pInt[0],pInt[1],pInt[2])
pInt[i]=itemWidth;
SendMessage(hWndStatus, SB_SETPARTS,3,(LPARAM)pInt);
::MoveWindow(hWndStatus,0,0,0,0,TRUE);
break;
开始在visual studio2010上试,后来修改了一下换到VC++6.0上,结果都是一样的

另外还有一个问题,想必大家也看出来了,状态栏上文字是乱码
相关代码:

int xPos;
int yPos;
char str[50];
RECT rectClient,rectWindow;
int *pInt=new int[3];
……
case WM_LBUTTONDOWN:
xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
//sprintf(str,"你按下了左键\n坐标为:(%d,%d)",xPos,yPos);
//::MessageBoxA(hWnd,str,"提示",MB_OK);
sprintf(str,"(%d,%d)",xPos,yPos);
::SendMessageA(hWndStatus,SB_SETTEXT,0,(WPARAM)str);
break;
其中的 char类型的str不好转化为WPARAM型,用其它的数据类型又不好做printf格式化工作

希望有朋友能够解决这两个问题!!!!!
...全文
117 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wencan2010 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hztj2005 的回复:]

我遇到过,是状态栏的值设置有误:
印象中,设置时,参数是坐标,不是长度。要是当成长度了,累计计算,事右值小于左值,就看不见了。
也许我记反了。
[/Quote]

是我把座標錯當成長度了
把其中一句稍微改一下就OK了。
for(int itemWidth=(rectClient.right-rectClient.left)/3,i=0;i<3;i++,pInt[0],pInt[1],pInt[2])
pInt[i]=itemWidth*(i+1);
被我用做參考的例子都只分成兩欄,後一欄設置為-1
hztj2005 2010-09-27
  • 打赏
  • 举报
回复
我遇到过,是状态栏的值设置有误:
印象中,设置时,参数是坐标,不是长度。要是当成长度了,累计计算,事右值小于左值,就看不见了。
也许我记反了。
Eleven 2010-09-27
  • 打赏
  • 举报
回复

HWND DoCreateStatusBar(HWND hwndParent, int idStatus, HINSTANCE hinst, int cParts)
{
HWND hwndStatus;
RECT rcClient;
HLOCAL hloc;
PINT paParts;
int i, nWidth;

// Ensure that the common control DLL is loaded.
InitCommonControls();

// Create the status bar.
hwndStatus = CreateWindowEx(
0, // no extended styles
STATUSCLASSNAME, // name of status bar class
NULL, // no text when first created
SBARS_SIZEGRIP | // includes a sizing grip
WS_CHILD | WS_VISIBLE, // creates a visible child window
0, 0, 0, 0, // ignores size and position
hwndParent, // handle to parent window
(HMENU) idStatus, // child window identifier
hinst, // handle to application instance
NULL); // no window creation data

// Get the coordinates of the parent window's client area.
GetClientRect(hwndParent, &rcClient);

// Allocate an array for holding the right edge coordinates.
hloc = LocalAlloc(LHND, sizeof(int) * cParts);
paParts = (PINT) LocalLock(hloc);

// Calculate the right edge coordinate for each part, and
// copy the coordinates to the array.
nWidth = rcClient.right / cParts;
for (i = 0; i < cParts; i++) {
paParts[i] = nWidth;
nWidth += nWidth;
}

// Tell the status bar to create the window parts.
SendMessage(hwndStatus, SB_SETPARTS, (WPARAM) cParts, (LPARAM)
paParts);

// Free the array, and return.
LocalUnlock(hloc);
LocalFree(hloc);
return hwndStatus;
}

// 调用
case WM_CREATE:
hStatus = DoCreateStatusBar(hWnd, 0x1010, hInst, 4);
break;
Eleven 2010-09-27
  • 打赏
  • 举报
回复

HWND DoCreateStatusBar(HWND hwndParent, int idStatus, HINSTANCE
hinst, int cParts)
{
HWND hwndStatus;
RECT rcClient;
HLOCAL hloc;
PINT paParts;
int i, nWidth;

// Ensure that the common control DLL is loaded.
InitCommonControls();

// Create the status bar.
hwndStatus = CreateWindowEx(
0, // no extended styles
STATUSCLASSNAME, // name of status bar class
NULL, // no text when first created
SBARS_SIZEGRIP | // includes a sizing grip
WS_CHILD | WS_VISIBLE, // creates a visible child window
0, 0, 0, 0, // ignores size and position
hwndParent, // handle to parent window
(HMENU) idStatus, // child window identifier
hinst, // handle to application instance
NULL); // no window creation data

// Get the coordinates of the parent window's client area.
GetClientRect(hwndParent, &rcClient);

// Allocate an array for holding the right edge coordinates.
hloc = LocalAlloc(LHND, sizeof(int) * cParts);
paParts = (PINT) LocalLock(hloc);

// Calculate the right edge coordinate for each part, and
// copy the coordinates to the array.
nWidth = rcClient.right / cParts;
for (i = 0; i < cParts; i++) {
paParts[i] = nWidth;
nWidth += nWidth;
}

// Tell the status bar to create the window parts.
SendMessage(hwndStatus, SB_SETPARTS, (WPARAM) cParts, (LPARAM)
paParts);

// Free the array, and return.
LocalUnlock(hloc);
LocalFree(hloc);
return hwndStatus;
}

// 调用
WM_CREATE:
hStatus = DoCreateStatusBar(hWnd, 0x1010, hInst, 4);
break;
wencan2010 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 vocanicy 的回复:]

编码问题,如果想编写VC6和2010下通用的代码
可以把
char str[50];
sprintf(str,"(%d,%d)",xPos,yPos);
改为
TCHAR str[50];
_stprintf(str, _T("%d,%d"), xPos, yPos);
[/Quote]
乱码的问题是解决了
但三栏只看到第一栏的问题呢??
vocanicy 2010-09-27
  • 打赏
  • 举报
回复
编码问题,如果想编写VC6和2010下通用的代码
可以把
char str[50];
sprintf(str,"(%d,%d)",xPos,yPos);
改为
TCHAR str[50];
_stprintf(str, _T("%d,%d"), xPos, yPos);
wencan2010 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 visualeleven 的回复:]

VS2010下不行?编码问题???
[/Quote]
這個情況很常見
VC.net常常要進行VC6.0上不需要的格式轉換,很讓人頭痛
Eleven 2010-09-26
  • 打赏
  • 举报
回复
VS2010下不行?编码问题???
wencan2010 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visualeleven 的回复:]

SendMessage最后一个参数是LPARAM类型,你的str的数据对吗?要先保证它不是乱码
[/Quote]

不好意思,我把LPARAM当成WPARAM了
但问题依然无法解决

而且,尽管sendmessage函数最后一个参数错了,但在VC++6.0下能确保str正确输出
Eleven 2010-09-26
  • 打赏
  • 举报
回复
SendMessage最后一个参数是LPARAM类型,你的str的数据对吗?要先保证它不是乱码

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧