The listbox class with right button function is one that we oftenuse in our programs. It would be nice if we could build it oncean use it without having to incorporate into every project webuild. One way to do this is by creating a DLL. Borland'sWindows Programmers Guide describes a DLL with the following
A dynamic-link library (DLL) is a library of functions, data,and resources whose references are resolved at run time ratherthan at compile time.Applications that use code from static-linked libraries attachcopies of that code at link time. Applications that use code fromDLLs share that code with all other applications using the DLL,therefore reducing application size. For example, you might wantto define complex windowing behavior, shared by a group of yourapplications, in an ObjectWindows DLL.
If you have a class like our RightButtonListBox that you feel will be useful in more than one problem. Firstbuild a simple project using AppExpert which you can use to test the new class. When you are sure the class performs the way you want, thenwe create a DLL
We will create a simple program to test our DLL. When we createanother project that uses RightButtonListBox, this project needonly have access to the dll, the library and the header for our class RightButtonListBox.As long as this header is not changed, you may alter the functionsof RightButtonListBox and never have to recompile the programthat uses the DLL. All linking takes place at runtime. You willeffectively create two independent programs with one having accessto the latest version of the other.
In our test we create the same AppExpert project we created tobuild our DLL. First create a new directory (preferably withinthe DLL directory so both tests are stored near each other). Copyboth the DLL and the LIB files into this directory. Then callon AppExpert to create an MDI project with it's child as a listbox. I called my project Testrbdl. I want to replace the TListBoxin the child window with a RightButtonListBox, so I add a variable(of type RightButtonListBox *) to the child class and replacethe client assignment in the Child constructor. We must fill inthe Module parameter here since we will use memory allocated bythe application not the DLL where the definition of RightButtonListBoxresides.
TestrddlMDIChild::TestrddlMDIChild (TMDIClient &parent, const char far *title, TWindow *clientWnd, bool shrinkToClient, TModule *module) : TMDIChild (parent, title,!clientWnd ? new RightButtonListBox(0, 0, 0, 0, 100, 100,::Module) : clientWnd, shrinkToClient, module){ // INSERT>> Your constructor code here.}Next add the SetupWindow function and a handler for right buttonevents in the Child.
void TestrddlMDIChild::SetupWindow (){ TMDIChild::SetupWindow(); // INSERT>> Your code here. RightButtonListBox *rbBox = TYPESAFE_DOWNCAST(GetFirstChild(),RightButtonListBox); rbBox->AddString("first"); rbBox->AddString("second"); rbBox->AddString("third");}void TestrddlMDIChild::EvRButtonDown (uint modKeys, TPoint& point){ TMDIChild::EvRButtonDown(modKeys, point); // INSERT>> Your code here. MessageBox("Got the right button message");}Once you have created all of this code, you must include the headerfor RightButtonListBox in the header for child and in projectadd the right button LIB to the project
.
You should now be able to run this project. Remember to selectnew from the file menu to create the child. Then when you pressthe right button in the listbox, the object pointed to shouldbe selected and the message box indicating the child receivedthe right button event should appear.
One of the most perplexing errors that can occur when you areusing dlls, is the "cannot create process" error. This occurs when oneof the dlls used by a project is not in the local directory or the Windowsdirectory (the two places a program looks at runtime for dlls). To find outwhich dll is not available, simply try to run the program directly from Windows.The missing dlls will be listed by the system.
The DLL that you have created is very large. Most of the sizecomes from debugging information. To learn how to reduce the sizeof your DLL or any executable file click here.
You may download the following zip file which contains both projectsdiscussed here. In addition you can download all you need to used a right-button listboxdll by clicking here. A complete application usingthis dll is presented in week 6.