LB_ADDSTRING无法运作 [英] LB_ADDSTRING cannot work

查看:82
本文介绍了LB_ADDSTRING无法运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我使用DialogBoxParam创建了一个模型对话框.但是当我想向列表框中添加字符串时,它没有用.希望您能为我提供帮助.Tnk..这是我的代码.

Hey guys.I create a model dialog using DialogBoxParam.But when I want to add string to list box,it didn''t work.I hope you can help me.Tnk..Here is my code.

<pre lang="cs">void FillListBox(HWND hwnd, UINT filter) {<br />
    SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_RESETCONTENT,0,0 );<br />
<br />
    int index;<br />
    for ( int i=0; i<numMessages; i++) {<br />
        if ( msginfo[i].fType & filter ) {<br />
//          index = (int)SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );<br />
//          index = ::SendMessage( ::GetDlgItem(hwnd,IDC_MESSAGES),LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );<br />
            index = ListBox_AddString( GetDlgItem(hwnd,IDC_MESSAGES),msginfo[i].pWmMsg );<br />
            SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType );<br />
        }<br />
    }<br />
}</pre><br />


无论我使用哪种方法,它都根本不起作用.为什么?


Whatever method I use,it didn''t work at all.Why??

推荐答案

我看不到代码中的真正问题.但是您应该区分组合框和列表框.
I cant see a really problem in your code. But you should distinguish between combo and list box.
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#ifdef _WIN64
  #define  ON64(C,E)  C
#else
  #define  ON64(C,E)  E
#endif

#ifdef _DEBUG
  #define  ASSERT(B)  if(!(B)){ __int2c(); }
#else
  #define  ASSERT(B)  
#endif

static LPCDLGTEMPLATE __dlgResource(const int idr,HINSTANCE h)
{
  HRSRC      hrsrc = FindResource(h,MAKEINTRESOURCE(idr),RT_DIALOG);
  HGLOBAL    hres = hrsrc?LoadResource(h,hrsrc):0;
  return (LPCDLGTEMPLATE)(hres?LockResource(hres):0);
}

void FillListBox(HWND hwnd,const int idc,UINT filter);
void FillComboBox(HWND hwnd,const int idc, UINT filter);

ON64(INT_PTR,int) FAR PASCAL __dlgProc(HWND h,unsigned int m,WPARAM w,LPARAM l)
{
  switch(m)
  {
    case WM_CLOSE: EndDialog(h,0); break;
    case WM_COMMAND:
      switch(LOWORD(w))
      {
        case IDOK:
        case IDCANCEL: EndDialog(h,LOWORD(w)); break;
      }
    break;
    case WM_INITDIALOG:
      FillListBox(h,101,0x0002);
      FillComboBox(h,102,0x0002);
    break;
  }
  return 0;
}

int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
  DialogBoxIndirectParam(h,__dlgResource(1,h),0,__dlgProc,0);
  return 0;
}

  struct
  {
    const TCHAR*  pWmMsg;
    unsigned int  fType;
  } msginfo[] =
  {
    { __T("first")  ,0x001 },
    { __T("second")  ,0x003 },
    { __T("third")  ,0x007 },
  };

  const int  numMessages = sizeof(msginfo)/sizeof(msginfo[0]);

void FillListBox(HWND hwnd,const int idc,UINT filter)
{
  HWND    hlb = ::GetDlgItem(hwnd,idc);
  int     index,i,done;

  ASSERT(IsWindow(hlb)); // invalid window?
  if(IsWindow(hlb))
  {
    SendMessage(hlb,LB_RESETCONTENT,0,0 );
    for(done=i=0;i<numMessages;i++)
    {
      if(msginfo[i].fType & filter)
      {
        index = SendMessage(hlb,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg);
        SendMessage(hlb,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType);
        ++done;
      }
    }
    if(0==done)
    {
      index = SendMessage(hlb,LB_ADDSTRING,0,(LPARAM)__T("(empty)"));
      SendMessage(hlb,LB_SETITEMDATA,(WPARAM)index,(LPARAM)0);
    }
  }
}

void FillComboBox(HWND hwnd,const int idc, UINT filter)
{
  HWND    hcb = ::GetDlgItem(hwnd,idc);
  int     index,i,done;

  ASSERT(IsWindow(hcb)); // invalid window?
  if(IsWindow(hcb))
  {
    SendMessage(hcb,CB_RESETCONTENT,0,0 );
    for(done=i=0;i<numMessages;i++)
    {
      if(msginfo[i].fType & filter)
      {
        index = SendMessage(hcb,CB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg);
        SendMessage(hcb,CB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType);
        ++done;
      }
    }
    if(0==done)
    {
      index = SendMessage(hcb,CB_ADDSTRING,0,(LPARAM)__T("(empty)"));
      SendMessage(hcb,CB_SETITEMDATA,(WPARAM)index,(LPARAM)0);
    }
  }
}


祝你好运.


Good luck.


可能的问题:
1. hwnd不是窗口(使用IsWindow()进行检查)
2. IDC_MESSAGES不是控件-参见(1)
3. numMessages为零
4. msginfo[i].fType & filter永远不是真的
5. msginfo[i].pWmMsg是NULL或空字符串

再进行一些错误检查和调试器,应该很容易找出问题所在.
Possible problems:
1. hwnd is not a window (use IsWindow() to check)
2. IDC_MESSAGES is not a control - see (1)
3. numMessages is zero
4. msginfo[i].fType & filter is never true
5. msginfo[i].pWmMsg is NULL or empty string

With a little more error checking and the debugger it should be easy to find out what the problem is.


我刚刚尝试了以下代码:

I just tried the following code:

void FillListBox(HWND hwnd, UINT filter)
{
    SendDlgItemMessage( hwnd,IDC_NOTE,LB_RESETCONTENT,0,0 );

    int index;
    for ( int i=0; i < numMessages; i++)
    {
        if ( msginfo[i].fType & filter )
        {
            index = (int)SendDlgItemMessage( hwnd,IDC_NOTE,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );
            SendDlgItemMessage( hwnd,IDC_NOTE,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType );
        }
    }
}


而且效果很好.检查所有变量项(例如filternumMessages)的值,以确保使用正确的值.


and it worked fine. Check the values of all the variable items like filter and numMessages to ensure you are using the right values.


这篇关于LB_ADDSTRING无法运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆