home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Education
/
collectionofeducationcarat1997.iso
/
COMPUSCI
/
CENVIW.ZIP
/
WHORYOU.CMM
< prev
next >
Wrap
Text File
|
1993-12-21
|
12KB
|
321 lines
// WhoRYou.cmm - CEnvi demonstration for designing a dialog window.
// This demo prompts the user for information about
// themselves. You can see from this sample code
// that dedigning your own windows and windows behavior
// is very flexible, but also complicated.
#include <Window.lib>
#include <WinUtil.lib>
#include <Message.lib>
main()
{
do {
if ( GetUserID(FirstName,LastName,Sex) )
TryAgain = ShowOKmessage(FirstName,LastName,Sex);
else
TryAgain = ShowCancelMessage();
} while( TryAgain );
}
GetUserID(FirstName,LastName,Sex) // dialog box for user info
{
// Initialize some size parameters
AveCharWidth, AveCharHeight;
GetCharacterSizes(AveCharWidth,AveCharHeight);
AveRowGap = AveCharHeight / 2;
AveColGap = AveCharWidth
// Initially make the main window (which will be resized later).
// For now draw it off the screen so no one sees
MainWindow = MakeWindow(NULL,NULL,"GetUserIDFunc","Scientific Survey",
WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE,
-10,-10,9,9,NULL,uid);
// Make a couple of lines of descriptive text
Description = "This scientifically-designed survery will determine if "
"CEnvi is right for you. Use a number 2 pencil. "
"Good luck! (no cheating)";
#define MAX_TEXTLEN 40
DescRowCount = (strlen(Description) + 15/*wrap room*/) / MAX_TEXTLEN ;
MakeWindow(MainWindow,"static","GetUserIDChildFunc",Description,WS_CHILD | WS_VISIBLE,
AveColGap,AveRowGap,MAX_TEXTLEN * AveCharWidth,DescRowCount * AveCharHeight,NULL,uid);
BottomRow = AveRowGap + DescRowCount * AveCharHeight;
// Request the user's first name, which is three fields
#define EDIT_MARGIN (AveCharHeight / 4) // extra space around edit field
BottomRow += AveRowGap;
prompt = "First Name";
EditLength = 22; // default size of window for data input
MakeWindow(MainWindow,"static",NULL,prompt,WS_CHILD | WS_VISIBLE,
AveColGap,BottomRow + EDIT_MARGIN,
width = AveCharWidth * (strlen(prompt)+1),AveCharHeight,NULL);
MakeWindow(MainWindow,"static",NULL,NULL,WS_CHILD | WS_VISIBLE | SS_BLACKFRAME,
col = AveColGap * 2 + width,BottomRow,
(1+EditLength)*AveCharWidth,AveCharHeight + 2*EDIT_MARGIN,NULL);
uid.FirstNameHwnd = MakeWindow(MainWindow,"edit","GetUserIDChildFunc",NULL,
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT,
col + AveCharWidth / 2,BottomRow + EDIT_MARGIN,
EditLength*AveCharWidth,AveCharHeight,NULL,uid);
BottomRow += AveCharHeight + 2*EDIT_MARGIN;
// Request the user's last name, which is much like the first
BottomRow += AveRowGap;
prompt = "Last Name";
EditLength = 30; // default size of window for data input
MakeWindow(MainWindow,"static",NULL,prompt,WS_CHILD | WS_VISIBLE,
AveColGap,BottomRow + EDIT_MARGIN,
width = AveCharWidth * (strlen(prompt)+1),AveCharHeight,NULL);
MakeWindow(MainWindow,"static",NULL,NULL,WS_CHILD | WS_VISIBLE | SS_BLACKFRAME,
col = AveColGap * 2 + width,BottomRow,
(1+EditLength)*AveCharWidth,AveCharHeight + 2*EDIT_MARGIN,NULL);
uid.LastNameHwnd = MakeWindow(MainWindow,"edit","GetUserIDChildFunc",NULL,
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT,
col + AveCharWidth / 2,BottomRow + EDIT_MARGIN,
EditLength*AveCharWidth,AveCharHeight,NULL,uid);
BottomRow += AveCharHeight + 2*EDIT_MARGIN;
// Add radio buttons to select sex
BottomRow += AveRowGap;
#define PUSHBUTT_HEIGHT AveCharHeight
width = AveCharWidth * (4 + strlen("Female"));
uid.FemaleHwnd = MakeWindow(MainWindow,"button","GetUserIDChildFunc","Female",WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
AveColGap * 3,BottomRow,width,PUSHBUTT_HEIGHT,NULL,uid);
uid.MaleHwnd = MakeWindow(MainWindow,"button","GetUserIDChildFunc","Male",WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
AveColGap * 4 + width,BottomRow,AveCharWidth * (4 + strlen("Male")),PUSHBUTT_HEIGHT,NULL,uid);
BottomRow += PUSHBUTT_HEIGHT;
// Finally, add the OK and CANCEL buttons
BottomRow += AveRowGap * 2;
#define BUTTON_WIDTH 10 * AveCharWidth
#define BUTTON_HEIGHT AveCharHeight * 3 / 2
uid.OKhwnd = MakeWindow(MainWindow,"button","GetUserIDChildFunc","OK",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_DISABLED,
AveColGap * 3,BottomRow,BUTTON_WIDTH,BUTTON_HEIGHT,NULL,uid);
uid.CancelHwnd = MakeWindow(MainWindow,"button","GetUserIDChildFunc","CANCEL",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
AveColGap * 6 + BUTTON_WIDTH,BottomRow,BUTTON_WIDTH,BUTTON_HEIGHT,NULL,uid);
SizeAndCenterDisplay(MainWindow,AveColGap,AveRowGap);
// Initialize with FirstName as the active field
SetFocus(uid.FirstNameHwnd);
uid.OKselected = False;
while ( DoWindows() && !uid.OKselected ) ;
if ( uid.OKselected ) {
GetEditText(uid.FirstNameHwnd,FirstName);
FirstName[0] = toupper(FirstName[0]);
GetEditText(uid.LastNameHwnd,LastName);
LastName[0] = toupper(LastName[0]);
if ( SendMessage(uid.FemaleHwnd,BM_GETCHECK,0,0) ) strcpy(Sex,"woman");
else if ( SendMessage(uid.MaleHwnd,BM_GETCHECK,0,0) ) strcpy(Sex,"man");
else strcpy(Sex,"person");
BreakWindow(MainWindow);
return(TRUE);
}
return(FALSE);
}
/**** Windows functions called from GetUserInfo ****/
GetUserIDFunc(hwnd,msg,parm1,parm2,uid)
{
if ( msg == WM_COMMAND ) {
childHwnd = parm2 & 0xFFFF;
switch ( (parm2 >> 16) & 0xFFFF ) {
case BN_CLICKED:
switch( childHwnd ) {
case uid.OKhwnd:
uid.OKselected = TRUE;
break;
case uid.CancelHwnd:
BreakWindow(hwnd);
break;
default:
ShouldOKbeEnabled(uid);
break;
}
break;
case EN_CHANGE:
ShouldOKbeEnabled(uid);
break;
}
}
}
GetUserIDChildFunc(hwnd,msg,parm1,parm2,uid)
{
#define VK_SHIFT 0x10
if ( WM_CHAR == msg ) {
switch ( parm1 ) {
case '\t':
// Tab to next field or back tab to previous field. Will allow moving
// to any field that is not "static" type and not disabled
Backward = (0x80 & DynamicLink("USER","GETKEYSTATE",SWORD16,PASCAL,VK_SHIFT));
Sibling = hwnd;
BLObSize(_className,40);
do {
// if this is the end of the list then go to the other end
if ( Sibling == GetWindow(Sibling,Backward ? GW_HWNDFIRST : GW_HWNDLAST ) )
Sibling = GetWindow(Sibling,Backward ? GW_HWNDLAST : GW_HWNDFIRST );
else
Sibling = GetWindow(Sibling,Backward ? GW_HWNDPREV : GW_HWNDNEXT);
_len = GetClassName(Sibling,_className,39);
} while( (_len == 6 && !memicmp(_className,"static",6))
|| !IsWindowEnabled(Sibling) );
SetFocus(Sibling);
return 0;
case ' ':
// don't let last name or first name accept spaces
if ( hwnd == uid.LastNameHwnd || hwnd == uid.FirstNameHwnd )
return 0;
break;
case '\r';
// Enter selects the OK button, and so post a ' ' to the
// OK button window
if ( IsWindowEnabled(uid.OKhwnd) ) {
PostMessage(uid.OKhwnd,WM_KEYDOWN,' ',parm2);
PostMessage(uid.OKhwnd,WM_KEYUP,' ',parm2);
}
return(0);
}
}
}
ShouldOKbeEnabled(uid) // adjust the OK button to be enabled or NOT
{
WantOKstate = ( 0 != GetWindowTextLength(uid.FirstNameHwnd)
&