在VC(非MFC)中控制slider的位置,但是不知道为什么却没有任何效果但是 操作 如下: 
先设置slider的最大值和最小值 
LenSlidertemp1=SendDlgItemMessage(hwndCtl, IDC_SLIDER1,TBM_SETRANGEMAX , TRUE, SliderMax); 
LenSlidertemp2=SendDlgItemMessage(hwndCtl, IDC_SLIDER1,TBM_SETRANGEMIN , TRUE, SliderMin); 不知道为什么 这里temp1 temp2返回值都是0 然后为了检验一下 设置slider到中间: dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETPOS, 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); 
SendDlgItemMessage( 
hwndCtl, 
IDC_SLIDER1, 
TBM_SETPOS, 
TRUE, 
m_dwCurrPercent  //直接设置为50 
); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETPOS, 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETTHUMBLENGTH, 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETRANGEMAX ,0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); 
dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETRANGEMIN , 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); 
输出全部是0 
不知道为什么没有效果

解决方案 »

  1.   

    SendDlgItemMessage第一个参数应该是对话框(控件父窗口)的句柄,看你的命名似乎是控件自身的句柄,那么直接用SendMessage好了。
      

  2.   


    这个控件是在 About对话框中的  而这对话框的回调函数About:
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    CBaseDlg *vRecDlg;    switch (message)
        {
            case WM_INITDIALOG:
                RECT rectChild, rectParent;
                int DlgWidth, DlgHeight; // dialog width and height in pixel units
                int NewPosX, NewPosY;
        vRecDlg= new CBaseDlg();
        if(vRecDlg ==NULL)
        {
                 RETAILMSG(1,(L"vRecDlg requirment error\n"));
         return 0;
             }
        vRecDlg->OnInitDialog(hDlg);    //这里先设置slider的最大值和最小值 
                // trying to center the About dialog
                if (GetWindowRect(hDlg, &rectChild)) 
                {
                    GetClientRect(GetParent(hDlg), &rectParent);
                    DlgWidth = rectChild.right - rectChild.left;
                    DlgHeight = rectChild.bottom - rectChild.top ;
                    NewPosX = (rectParent.right - rectParent.left - DlgWidth) / 2;
                    NewPosY = (rectParent.bottom - rectParent.top - DlgHeight) / 2;

                    // if the About box is larger than the physical screen 
                    if (NewPosX < 0) NewPosX = 0;
                    if (NewPosY < 0) NewPosY = 0;
              SetWindowPos(hDlg, 0, rectChild.left, rectChild.top,
    wid, hei, SWP_NOZORDER | SWP_NOZORDER);
                }
                return (INT_PTR)TRUE;        case WM_COMMAND:
                if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
                {
                    EndDialog(hDlg, LOWORD(wParam));
    CommandBar_Destroy(g_hWndCommandBar);
    PostQuitMessage(0);
                    return TRUE;
                }else
    vRecDlg->OnCommand(HIWORD(wParam), LOWORD(wParam), (HWND)lParam);//这里操作slider的位置
                break;        case WM_CLOSE:
                EndDialog(hDlg, message);
    CommandBar_Destroy(g_hWndCommandBar);
    PostQuitMessage(0);
                return TRUE;    }
        return (INT_PTR)FALSE;
    }关于Oncommand()函数和ini函数:BOOL CBaseDlg::OnCommand(WORD wNotifyCode, WORD wID, HWND hwndCtl)
    {
    BOOL brc = TRUE;
    HWND    phWnd = NULL;
    DWORD m_dwCurrPercent=50;
    DWORD dwPercent;
    switch(wID)
    {
    case IDC_Start:
    RETAILMSG(1,(L"\nIDC_Start\n"));
    SetTimer(hwndCtl,1,1000,NULL); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETPOS, 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent));
    SendDlgItemMessage(
    hwndCtl, 
    IDC_SLIDER1,
    TBM_SETPOS,
    TRUE,
    m_dwCurrPercent
    );  dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETPOS, 0, 0);

    RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETTHUMBLENGTH, 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent)); dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETRANGEMAX ,0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent));
    dwPercent = SendDlgItemMessage(hwndCtl, IDC_SLIDER1, TBM_GETRANGEMIN , 0, 0); RETAILMSG(1,(L"\nIDC_Start = %d\n",dwPercent));
    // OnTimer(1);
    startAudioRec();
    break;
    case IDC_Stop:
    RETAILMSG(1,(L"\nIDC_Stop\n"));
    KillTimer(hwndCtl,1);
    MSG msg;
    // buttonStop.SetState(1);
    msg.message = WM_USER+1;
    RETAILMSG(1,(L"\nIDC_Stop1\n"));
    PostMessage(hwndCtl,msg.message,0,0);
    RETAILMSG(1,(L"\nIDC_Stop2\n"));
    slider_count = 0;
    break;
    case IDC_Play:
    RETAILMSG(1,(L"\nIDC_Play\n"));
    break;
    default:
    break;
    }
    return TRUE;}BOOL CBaseDlg::OnInitDialog(HWND hwndCtl)
    {
    DWORD LenSlidertemp;
    DWORD SliderMax=100;
    DWORD SliderMin=0;
    HWND shwnd;
    shwnd=GetDlgItem(hwndCtl,IDC_SLIDER1);
    if(shwnd==NULL)
    RETAILMSG(1,(L"shwnd is null"));
    LenSlidertemp=SendDlgItemMessage(shwnd, IDC_SLIDER1,TBM_SETRANGEMAX , FALSE, SliderMax); LenSlidertemp=SendDlgItemMessage(shwnd, IDC_SLIDER1,TBM_SETRANGEMIN , FALSE, SliderMin); return TRUE;
    }
      

  3.   

    其中ini函数中 有这么一行 我是测试用的BOOL CBaseDlg::OnInitDialog(HWND hwndCtl) 

    DWORD LenSlidertemp; 
    DWORD SliderMax=100; 
    DWORD SliderMin=0; 
    HWND shwnd; 
    shwnd=GetDlgItem(hwndCtl,IDC_SLIDER1); //这个shwnd原来是没有的 直接就是hwndCtl 但是也不行的
    if(shwnd==NULL) 
    RETAILMSG(1,(L"shwnd is null")); 
    LenSlidertemp=SendDlgItemMessage(shwnd, IDC_SLIDER1,TBM_SETRANGEMAX , FALSE, SliderMax); LenSlidertemp=SendDlgItemMessage(shwnd, IDC_SLIDER1,TBM_SETRANGEMIN , FALSE, SliderMin); return TRUE; 
    }
      

  4.   

    直接用函数好了阿
    CWnd *pWnd = GetDlgItem(IDC_SLIDER1);
    if(pWnd && pWnd->m_hWnd)
      ((CSliderCtrl*)pWnd)->SetRange(SliderMin,SliderMax);如果用消息
    ::SendMessage(pWnd->m_hWnd, TBM_SETRANGEMIN, FALSE, (LPARAM) SliderMin);
    ::SendMessage(pWnd->m_hWnd, TBM_SETRANGEMIN, TRUE, (LPARAM) SliderMax);参看 DDV_MinMaxSlider 原形void AFXAPI DDV_MinMaxSlider(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal)
    {
      ASSERT(minVal <= maxVal);
      if (!pDX->m_bSaveAndValidate)
      {
        if (minVal > value || maxVal < value)
        {
         #ifdef _DEBUG
          int nIDC = GetWindowLong(pDX->m_hWndLastControl, GWL_ID);
          TRACE1("Warning: initial dialog data is out of range in control ID %d.\n", nIDC);
         #endif
          return;     // don't stop now
        }
      }
      ::SendMessage(pDX->m_hWndLastControl, TBM_SETRANGEMIN, FALSE, (LPARAM) minVal);
      ::SendMessage(pDX->m_hWndLastControl, TBM_SETRANGEMIN, TRUE, (LPARAM) maxVal);
    }
      

  5.   

    CSliderCtrl 是MFC的 不能用to zgl7903 你的用sendmessage不行 CWnd *pWnd = GetDlgItem(IDC_SLIDER1); 
    CWND好像在这里不能识别
    我是用:
    HWND shwnd; 
    shwnd=GetDlgItem(hwndCtl,IDC_SLIDER1); //这里成功  ::SendMessage(hwndCtl, TBM_SETRANGEMIN, FALSE, (LPARAM) minVal); 
      ::SendMessage(hwndCtl, TBM_SETRANGEMIN, TRUE, (LPARAM) maxVal);不能我怀疑  hwndCtl 出问题 但是不肯定 因为shwnd=GetDlgItem(hwndCtl,IDC_SLIDER1); 返回了啊所以不明白为什么了
    谢谢