Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » FAQ VC++ / MFC » CComboBox: "Deaktivieren" jedoch Eingabe zulassen?

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
12.10.2005, 13:55 Uhr
FloSoft
Medialer Over-Flow
(Administrator)


Hi,
wie kann ich eine Combobox deaktivieren, damit man eben kein Dropdown mehr ausführen kann und man auch keine davon mehr auswählen kann? Nur das Editfeld soll man noch benutzen können (also was eingeben) Wie kann ich da am einfachsten lösen?

über GetComboBoxInfo krieg ich zwar ein handle auf das Editfeld, nur wird das mit deaktiviert sobald ich die Combobox deaktivier.
--
class God : public ChuckNorris { };
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
12.10.2005, 14:42 Uhr
FloSoft
Medialer Over-Flow
(Administrator)


ok habs nun nur über das neuerstellen der Combo hinbekommen:

umstellen auf "CBS_SIMPLE", Combo neu erstellen.

Ich poste mal die Klasse die das macht:


C++:
#include "stdafx.h"
#include "MyComboBox.h"

IMPLEMENT_DYNAMIC(CMyComboBox, CComboBox)

BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
END_MESSAGE_MAP()

CMyComboBox::CMyComboBox()
{
  m_nDropHeight = 0;
  m_nMaxChars = 0;
}

CMyComboBox::~CMyComboBox()
{
}

void CMyComboBox::SetMaxChars(int nChars)
{
  COMBOBOXINFO info;
  ZeroMemory(&info, sizeof(COMBOBOXINFO));
  info.cbSize = sizeof(COMBOBOXINFO);
  GetComboBoxInfo(&info);

  m_nMaxChars = nChars;
  ::SendMessage(info.hwndItem, EM_LIMITTEXT, nChars, 0);
}

void CMyComboBox::SwitchToSimple()
{
  if( (GetStyle() & CBS_SIMPLE) == CBS_SIMPLE)
    return;

  CRect rc;
  GetDroppedControlRect(&rc);
  m_nDropHeight = rc.Height();

  ModifyStyle(CBS_DROPDOWN, CBS_SIMPLE);
  RecreateComboBox();

  SetWindowPos( NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED );

  COMBOBOXINFO info;
  ZeroMemory(&info, sizeof(COMBOBOXINFO));
  info.cbSize = sizeof(COMBOBOXINFO);
  GetComboBoxInfo(&info);

  if(m_nMaxChars > 0)
    ::SendMessage(info.hwndItem, EM_LIMITTEXT, m_nMaxChars, 0);
}

void CMyComboBox::SwitchToDropDown()
{
  if( (GetStyle() & CBS_DROPDOWN) == CBS_DROPDOWN)
    return;

  ModifyStyle(CBS_SIMPLE, CBS_DROPDOWN);
  RecreateComboBox();

  SetWindowPos( NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED );

  COMBOBOXINFO info;
  ZeroMemory(&info, sizeof(COMBOBOXINFO));
  info.cbSize = sizeof(COMBOBOXINFO);
  GetComboBoxInfo(&info);

  CRect rectEdit, rectCombo;

  GetWindowRect(&rectCombo);
  ::GetWindowRect(info.hwndItem, &rectEdit);

    int nNonDropHeight = rectEdit.Height() + (::GetSystemMetrics(SM_CYEDGE) * 2);

    rectCombo.bottom = rectCombo.top + nNonDropHeight + m_nDropHeight;

    if (::GetParent(GetSafeHwnd()))
    {
    ::ScreenToClient(::GetParent(GetSafeHwnd()), &rectCombo.TopLeft());
    ::ScreenToClient(::GetParent(GetSafeHwnd()), &rectCombo.BottomRight());
    }
  ::MoveWindow(GetSafeHwnd(), rectCombo.left, rectCombo.top, rectCombo.Width(), rectCombo.Height(), TRUE);

  if(m_nMaxChars > 0)
    ::SendMessage(info.hwndItem, EM_LIMITTEXT, m_nMaxChars, 0);
}

BOOL CMyComboBox::RecreateComboBox(LPVOID lpParam)
{
    if (this->GetSafeHwnd() == NULL)
        return FALSE;

    CWnd* pParent = GetParent();

    if (pParent == NULL)
        return FALSE;

    // get current attributes
    DWORD dwStyle = GetStyle();
    DWORD dwStyleEx = GetExStyle();
    CRect rc;

//    GetDroppedControlRect(&rc);

    // DP: Altered as per suggestion by CheGueVerra
    GetWindowRect(&rc);
    pParent->ScreenToClient(&rc);    // map to client co-ords
    UINT nID = GetDlgCtrlID();
    CFont* pFont = GetFont();
    CWnd* pWndAfter = GetNextWindow(GW_HWNDPREV);

    // get the currently selected text (and whether it is a valid list selection)
    CString sCurText;
    int nCurSel = GetCurSel();
    BOOL bItemSelValid = nCurSel != -1;
    if (bItemSelValid)
        GetLBText(nCurSel, sCurText);
    else
        GetWindowText(sCurText);

    // copy the combo box items into a temp combobox (not sorted)
    // along with each item's data
    CComboBox comboNew;
    if (! comboNew.CreateEx(dwStyleEx, _T("COMBOBOX"), _T(""), dwStyle, rc, pParent, nID, lpParam))
      return FALSE;
    comboNew.SetFont(pFont);
    int nNumItems = GetCount();
    for (int n = 0; n < nNumItems; n++)
    {
        CString sText;
        GetLBText(n, sText);
        int nNewIndex = comboNew.AddString(sText);
        comboNew.SetItemData(nNewIndex, GetItemData(n));
    }
    // re-set selected text
    if (bItemSelValid)
        comboNew.SetCurSel(comboNew.FindStringExact(-1, sCurText));
    else
        comboNew.SetWindowText(sCurText);

    // destroy the existing window, then attach the new one
    DestroyWindow();
    HWND hwnd = comboNew.Detach();
    Attach(hwnd);

    // position correctly in z-order
    SetWindowPos(pWndAfter == NULL ? &CWnd::wndBottom : pWndAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

  return TRUE;
}




C++:
#pragma once

class CMyComboBox : public CComboBox
{
    DECLARE_DYNAMIC(CMyComboBox)

public:
    CMyComboBox();
    virtual ~CMyComboBox();

  void SwitchToSimple();
  void SwitchToDropDown();

  void SetMaxChars(int nChars);

protected:
  BOOL RecreateComboBox(LPVOID lpParam = 0);

    DECLARE_MESSAGE_MAP()

private:
  int m_nDropHeight;
  int m_nMaxChars;
};



Benutzung dann z.b "m_cbMyCombo.SwitchToSimple()" oder "m_cbMyCombo.SwitchToDropDown()"
--
class God : public ChuckNorris { };
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ FAQ VC++ / MFC ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: