Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать

2.2 The control elements of dialog window

The list box usage in dialog window programs

List or ListBox is a one of control elements. It’s organized by CListBox class. Listbox can send and receive messages. Therefore, the messages are send to list on it’s initialization. When list is initialized, it’s receive messages on actions, produced by user.

The Listbox generates messages of different types. For example messages on double mouse click on list element, on losing of active state (on inactivity of listbox), on elment selection in the list. Every event is described by identifier code.

Following is a list of potential message-map entries and a description of the cases in which they would be sent to the parent:

  • ON_LBN_DBLCLK   The user double-clicks a string in a list box. Only a list box that has the LBS_NOTIFY style will send this notification message.

  • ON_LBN_ERRSPACE   The list box cannot allocate enough memory to meet the request.

  • ON_LBN_KILLFOCUS   The list box is losing the input focus.

  • ON_LBN_SELCANCEL   The current list-box selection is canceled. This message is only sent when a list box has the LBS_NOTIFY style.

  • ON_LBN_SELCHANGE   The selection in the list box is about to change. This notification is not sent if the selection is changed by the CListBox::SetCurSel member function. This notification applies only to a list box that has the LBS_NOTIFY style. The LBN_SELCHANGE notification message is sent for a multiple-selection list box whenever the user presses an arrow key, even if the selection does not change.

  • ON_LBN_SETFOCUS   The list box is receiving the input focus.

  • ON_WM_CHARTOITEM   An owner-draw list box that has no strings receives a WM_CHAR message.

  • ON_WM_VKEYTOITEM   A list box with the LBS_WANTKEYBOARDINPUT style receives a WM_KEYDOWN message.

To speak the truth, every macro has standard handle, so usually you have no need rewrite them. Generally macro has the following view (on example):

ON_LBN_DBLCLK(ID, HandlerName)

CListBox class has a number special function to work with information in the list:

CListBox::AddString (lpctstr lpszItem ); // Call this member function to add a string (lpszItem) to a list box;

CListBox::GetCurSel( ); // Retrieves the zero-based index of the currently selected item, if any, in a single-selection list box.

CListBox::GetText( int nIndex, LPTSTR lpszBuffer ); // Gets a string from a list box, there is nIndex -  specifies the zero-based index of the string to be retrieved; lpszBuffer   Points to the buffer that receives the string. The buffer must have sufficient space for the string and a terminating null character. The size of the string can be determined ahead of time by calling the GetTextLen member function.

Other functions: DeleteString() – deletes string from the list, FindString() – finds the string, SetCurSel() – set the selection cursor on the element, InsertString() – inserts the string at set index, GetCount() – returns the number of elements in the list. All of that functions belong to the CListBox class.

But there is the question, how to implement the function to the list. It’s need to receive the pointer to the list by GetDlgItem(), which returns the pointer on object, whose identifier is pointed out. If object isn’t exist function returns NULL.

The initialization of lists

Initially list is created as some empty element, it’s initialisated every time, when dialog window is displayed. To make that there is created its own version of OnInitDialog() function. Here is an example of dialog window initialization:

Listing 2.5 The Initialization of dialog window.

BOOL CSampleDialog::OnInitDialog()

{CDialog::OnInitDialog();

ll= (CListBox *)CWnd::GetDlgItem(IDC_LIST1);

ll->AddString("Apple!"); ll->AddString("Orange!");

return TRUE;}

There can be such question – what is the ll-variable. That variable is usually set in the dialog window class description: CListBox ll; (usually in private section of the class). After that, by GetDlgItem() function object ll is linked to identifier of resource (IDC_LIST1). The last strings are addition of text strings to our list (strings "Apple!", "Orange!").

The creation of Edit control

The CEdit class provides the functionality of a Windows edit control. An edit control is a rectangular child window in which the user can enter text.

You can create an edit control either from a dialog template or directly in your code. In both cases, first call the constructor CEdit to construct the CEdit object, then call the Create member function to create the Windows edit control and attach it to the CEdit object.

Construction can be a one-step process in a class derived from CEdit. Write a constructor for the derived class and call Create from within the constructor.

CEdit inherits significant functionality from CWnd. To set and retrieve text from a CEdit object, use the CWnd member functions SetWindowText and GetWindowText, which set or get the entire contents of an edit control, even if it is a multiline control. Also, if an edit control is multiline, get and set part of the control's text by calling the CEdit member functions GetLine, SetSel, GetSel, and ReplaceSel.

The initialization of edit control is provided in similar manner:

ed = (CEdit *)CWnd::GetDlgItem(IDC_EDIT1);

The next function makes interesting thing – the information collection from edit control ed (by GetWindowText() function to string str) to Listbox ll. Action is produced after OK button pressing in the dialog window.

Listing 2.6 The OnOK redefined handler.

void CSampleDialog::OnOK()

{char str[10];

ed->GetWindowText(str,sizeof(str));

ll->AddString(str);}

Listing 2.7 The program with list box and edit box using.

// d01.h: interface for the classes.

class CApp : public CWinApp

{public:

BOOL InitInstance();

};

class CSampleDialog : public CDialog

{public:

void OnClear();

void OnDelete();

void OnAdd();

BOOL OnInitDialog();

CSampleDialog(UINT id, CWnd *Owner) :

CDialog (id,Owner) {}

DECLARE_MESSAGE_MAP()

private:

CButton *bt_clear;

CButton *bt_delete;

CButton *bt_add;

CEdit *ed;

CListBox *lb;

};

// d01.cpp: implementation of the CApp class.

#include <afxwin.h>

#include <string.h>

#include "d01.h"

#include "resource.h"

BOOL CApp::InitInstance()

{CSampleDialog a(IDD_DIALOG1,m_pMainWnd);

a.DoModal();

return TRUE;

}

CApp App;

BEGIN_MESSAGE_MAP(CSampleDialog,CDialog)

ON_COMMAND(IDC_BUTTON1,OnAdd)

ON_COMMAND(IDC_BUTTON2,OnDelete)

ON_COMMAND(IDC_BUTTON3,OnClear)

END_MESSAGE_MAP()

BOOL CSampleDialog::OnInitDialog()

{CDialog::OnInitDialog();

lb=(CListBox*)GetDlgItem(IDC_LIST1);

ed=(CEdit*)GetDlgItem(IDC_EDIT1);

bt_add=(CButton*)GetDlgItem(IDC_BUTTON1);

bt_delete=(CButton*)GetDlgItem(IDC_BUTTON2);

bt_clear=(CButton*)GetDlgItem(IDC_BUTTON3);

return TRUE;

}

void CSampleDialog::OnAdd()

{char a[50];

ed->GetWindowText(a,sizeof(a));

int i=lb->GetCurSel();

if(strlen(a)==0)MessageBox("You've set empty string","Error");

else {if(i==LB_ERR)lb->AddString(a);

else lb->InsertString(i,a);}

}

void CSampleDialog::OnDelete()

{int i=lb->GetCurSel();

if(i==LB_ERR)MessageBox("You need string selection","Error");

if(i!=LB_ERR)lb->DeleteString(i);

}

void CSampleDialog::OnClear()

{int i=lb->GetCount();

for(int j=0;j<i+1;j++)lb->DeleteString(0);

}

Pic 2.1 The view of dialog window resource for program in Listing 2.7.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]