home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 December
/
PCWorld_1998-12_cd.iso
/
software
/
sybase
/
ASA
/
asa60.exe
/
data1.cab
/
cxmp_files
/
mainwin.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-07-27
|
9KB
|
366 lines
/* MAINWIN.C Windows specific routines for all example programs
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef __WATCOMC__
#include <malloc.h>
#include <conio.h>
#else /* LATTICE */
#include <dos.h>
#endif
#include <windows.h>
#include "example.h"
#include "ntsvc.h"
long FAR _exportkwd PASCAL MainWndProc( HWND, unsigned, unsigned, long );
static HANDLE hInst;
static HWND hWnd;
static int yText;
static int CharHeight;
static int CharWidth;
static BOOL WindowCreated = FALSE;
extern int StartedDB;
static LPSTR Prompt;
static LPSTR PromptString;
static int StringLen;
int PageSize = 2;
int PASCAL do_main( HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow );
extern int Displaytext( int col, char *fmt, ... )
/***********************************************/
{
RECT hWndRect;
int x;
int len;
va_list arg_ptr;
char buffer[ 200 ];
HDC hDC;
va_start( arg_ptr, fmt );
vsprintf( buffer, fmt, arg_ptr );
va_end( arg_ptr );
if( SvcIsService() ) {
SvcGenerateLog( buffer, EVENTLOG_AUDIT_SUCCESS );
}
if( hWnd != (HWND) NULL ) {
len = strlen( buffer );
if( buffer[len - 1] == '\n' ) {
if( col == 0 && len == 1 ) return FALSE;
buffer[len - 1] = '\0';
}
hDC = GetDC( hWnd );
GetClientRect( hWnd, &hWndRect );
x = col * CharWidth;
if( x == 0 ) {
if( yText + CharHeight > hWndRect.bottom - CharHeight ) {
ScrollWindow( hWnd, 0, -CharHeight, NULL, NULL );
UpdateWindow( hWnd );
} else {
yText += CharHeight;
}
}
OemToAnsi( buffer, buffer );
TextOut( hDC, x, yText, buffer, strlen( buffer ) );
ReleaseDC( hWnd, hDC );
}
return( TRUE );
}
BOOL FAR _exportkwd PASCAL get_string_rtn( HWND hDlg, unsigned message,
unsigned wParam, long lParam )
/*********************************************************************/
{
lParam = lParam;
switch( message ) {
case WM_INITDIALOG:
SetWindowText( hDlg, (LPSTR) Prompt );
return( TRUE );
case WM_COMMAND:
switch( LOWORD( wParam ) ) {
case IDOK:
GetDlgItemText( hDlg, IDE_STRING_EDIT,
(LPSTR) PromptString, StringLen );
/* Fall through */
case IDCANCEL:
EndDialog( hDlg, TRUE );
}
}
return( FALSE );
}
static void prompt_for_string( LPSTR prompt, LPSTR buff, int len )
/****************************************************************/
{
FARPROC proc;
buff[0] = '\0';
Prompt = prompt;
PromptString = buff;
StringLen = len;
proc = MakeProcInstance( (FARPROC)get_string_rtn, hInst );
DialogBox( hInst, "DLG_PROMPT_STRING", hWnd, proc );
FreeProcInstance( proc );
}
extern void GetValue( char *prompt, char *buff, int len)
/******************************************************/
{
if( SvcGUIActive() ) {
prompt_for_string( (LPSTR) prompt, (LPSTR) buff, len );
} else {
strcpy( buff, "" );
}
}
extern void GetTableName( char *buff, int len)
/********************************************/
{
GetValue( (char *) "Enter table name", (char *) buff, len );
if( strlen( buff ) == 0 ) {
strcpy( buff, "employee" );
}
}
BOOL InitApplication( HANDLE hInstance )
/**************************************/
{
WNDCLASS wc;
wc.style = 0;
#ifdef __TURBOC__
wc.lpfnWndProc = MainWndProc;
#else
wc.lpfnWndProc = (WNDPROC) MainWndProc;
#endif
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
wc.hCursor = LoadCursor( 0, IDC_ARROW );
wc.hbrBackground = GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = "ExampleMenu";
wc.lpszClassName = "ExampleWClass";
return( RegisterClass( &wc ) );
}
BOOL InitInstance( HANDLE hInstance, int nCmdShow )
/*************************************************/
{
hInst = hInstance;
hWnd = CreateWindow( "ExampleWClass", "Example", WS_OVERLAPPEDWINDOW,
170, 50, 350, 300, 0, 0, hInstance, 0 );
if( !hWnd ) {
return( FALSE );
}
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
return( TRUE );
}
extern void Display_systemerror( char *message )
/**********************************************/
{
MessageBox( hWnd, message, "System Error", MB_OK );
}
extern void Display_refresh( void )
/*********************************/
{
UpdateWindow( hWnd );
}
int sv_start_service( HANDLE hInstance, HANDLE hPrevInstance,
char * lpCmdLine, int nCmdShow )
/***********************************************************/
{
int retcode;
if( SvcIsService() ) {
SvcSetRunning();
}
retcode = do_main( hInstance, hPrevInstance, lpCmdLine, SW_NORMAL );
if( SvcIsService() ) {
SvcSetStopped();
}
return( retcode );
}
void sv_stop_service( void )
/**************************/
{
}
void sv_resume_service( void )
/****************************/
{
}
void sv_pause_service( void )
/***************************/
{
}
void sv_debug_info( char *str )
/*****************************/
{
// Displaytext( 0, "%s", str );
SvcGenerateLog( str, EVENTLOG_INFORMATION_TYPE );
}
int PASCAL do_main( HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
/*********************************************************/
{
MSG msg;
lpCmdLine = lpCmdLine;
hInst = hInstance;
#ifdef NTSERVICE
if( !SvcGUIActive() ) {
// If service is not able to start window, print one
// screen full to application event log to demonstrate
// that it is running.
if( !WSQLEX_Init() ) {
SvcGenerateLog( "unable to Init example", 1 );
return( FALSE );
}
sv_debug_info( "printing data..." );
WSQLEX_Process_Command( 'p' );
sv_debug_info( "shutting down..." );
WSQLEX_Finish();
return( TRUE );
}
#endif
if( !hPrevInstance ) {
if( !InitApplication( hInstance ) ) {
return( FALSE );
}
}
sv_debug_info( "initialized application..." );
if( !InitInstance( hInstance, nCmdShow ) ) {
SvcGenerateLog( "unable to initialize application", 1 );
return( FALSE );
}
sv_debug_info( "initialized window..." );
if( !WSQLEX_Init() ) {
SvcGenerateLog( "unable to Init example", 1 );
return( FALSE );
}
sv_debug_info( "WSQLEX_Init successful..." );
Displaytext( 0, "==>" );
while( GetMessage( &msg, 0, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
sv_debug_info( "WSQLEX_Init message loop terminated..." );
return( msg.wParam );
}
long FAR _exportkwd PASCAL MainWndProc( HWND hWnd, unsigned message,
unsigned wParam, long lParam )
{
HDC hDC;
TEXTMETRIC tm;
PAINTSTRUCT ps;
switch( message ) {
case WM_KEYDOWN:
Displaytext( 5, "%c", wParam );
if( wParam == 'Q' ) {
WSQLEX_Finish();
DestroyWindow( hWnd );
break;
}
WSQLEX_Process_Command( wParam );
Displaytext( 0, "==>" );
break;
case WM_CREATE:
hDC = GetDC( hWnd );
GetTextMetrics( hDC, &tm );
CharHeight = tm.tmHeight + tm.tmExternalLeading;
CharWidth = tm.tmAveCharWidth;
ReleaseDC( hWnd, hDC );
break;
case WM_SIZE:
yText = 0;
PageSize = HIWORD( lParam ) / CharHeight - 2;
if( PageSize <= 0 ) {
PageSize = 1;
}
if( WindowCreated ) {
InvalidateRect( hWnd, NULL, TRUE );
UpdateWindow( hWnd );
Displaytext( 0, "==>" );
} else {
WindowCreated = TRUE;
}
break;
case WM_PAINT:
BeginPaint( hWnd, &ps );
EndPaint( hWnd, &ps );
break;
case WM_COMMAND:
switch( LOWORD( wParam ) ) {
case IDM_HELP:
SendMessage( hWnd, WM_KEYDOWN, 'H', 0 );
break;
case IDM_PRINT:
SendMessage( hWnd, WM_KEYDOWN, 'P', 0 );
break;
case IDM_UP:
SendMessage( hWnd, WM_KEYDOWN, 'U', 0 );
break;
case IDM_DOWN:
SendMessage( hWnd, WM_KEYDOWN, 'D', 0 );
break;
case IDM_TOP:
SendMessage( hWnd, WM_KEYDOWN, 'T', 0 );
break;
case IDM_BOTTOM:
SendMessage( hWnd, WM_KEYDOWN, 'B', 0 );
break;
case IDM_INSERT:
SendMessage( hWnd, WM_KEYDOWN, 'I', 0 );
break;
case IDM_NAME:
SendMessage( hWnd, WM_KEYDOWN, 'N', 0 );
break;
case IDM_QUIT:
SendMessage( hWnd, WM_KEYDOWN, 'Q', 0 );
break;
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return( DefWindowProc( hWnd, message, wParam, lParam ) );
}
return( 0 );
}
int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
/*****************************************************************************************/
{
#ifdef NTSERVICE
return( SvcWinMain( hInstance, hPrevInstance, lpCmdLine, nCmdShow ) );
#else
return( do_main( hInstance, hPrevInstance, lpCmdLine, nCmdShow ) );
#endif
}