[C大程] 图形程序设计_4 编写窗口输入程序

#include "graphics.h"
#include "extgraph.h"
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "conio.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

#include <windows.h>
#include <olectl.h>
#include <stdio.h>
#include <mmsystem.h>
#include <wingdi.h>
#include <ole2.h>
#include <ocidl.h>
#include <winuser.h>

#define TIMER_BLINK 1

const int mseconds = 1000;

static char Text[100];
static int textlen = 0;
static int location=-1;

static bool isBlink = FALSE;  
static bool isDisplay = FALSE; 

void KeyboardEventProcess(int key,int event);
void CharEventProcess(char c);
void TimerEventProcess(int timerID);

void Main() 
{
	InitConsole();
    InitGraphics();        	
	MovePen(0.2,1);
	registerKeyboardEvent(KeyboardEventProcess);
    registerCharEvent(CharEventProcess);
	registerTimerEvent(TimerEventProcess);
    if (isBlink) startTimer(TIMER_BLINK, mseconds);
    isBlink = !isBlink;
    if (isBlink ) startTimer(TIMER_BLINK, mseconds);
    else cancelTimer(TIMER_BLINK);
}

void CharEventProcess(char key)
{
//	printf("textlen=%d,text=%s\n",textlen,Text);
	if(textlen>=99||GetCurrentX()>=GetWindowWidth()-0.2)return;
	if(key==VK_BACK||key==VK_DELETE)return;
	if(key=='\r'){
	 	SetEraseMode(TRUE);
		if(isBlink && isDisplay){
			DrawTextString("_");
			MovePen(GetCurrentX()-TextStringWidth("_"),GetCurrentY());
		}
    	InitConsole();
	 	printf("%s\n", Text);
		system("pause");
		FreeConsole();
		MovePen(GetCurrentX()-TextStringWidth(Text), GetCurrentY());
   		DrawTextString(Text);
   		SetEraseMode(FALSE);
   		MovePen(GetCurrentX()-TextStringWidth(Text), GetCurrentY());
   		textlen=0;
   		location=-1;
   		Text[0]='\0';
    }
	else{
		char str[2]={0,0};
		str[0]=key;
		if(location==textlen-1){
			Text[textlen++]=key;
			Text[textlen]='\0';
			DrawTextString(str);
			location++;
		}
		else{
			int i;
			string s1;
			s1=SubString(Text,location+1,textlen); 
			SetEraseMode(TRUE);
			DrawTextString(s1);
			SetEraseMode(FALSE);
			MovePen(GetCurrentX()-TextStringWidth(s1),GetCurrentY());
			DrawTextString(str);
			DrawTextString(s1);
			MovePen(GetCurrentX()-TextStringWidth(s1),GetCurrentY());
			textlen++;
			location++;
			for(i=textlen;i>=location+1;i--){
				Text[i]=Text[i-1];
			}
			Text[location]=key;
		}
   	    if (isBlink && isDisplay) {
			SetEraseMode(TRUE);
			MovePen(GetCurrentX()-TextStringWidth(str), GetCurrentY());
 			DrawTextString("_");
 			MovePen(GetCurrentX()-TextStringWidth("_"),GetCurrentY());
 			SetEraseMode(FALSE);
 			DrawTextString(str);
		}
		else{
			MovePen(GetCurrentX()-TextStringWidth(str),GetCurrentY());
			DrawTextString(str);
		}
	}	
}
void KeyboardEventProcess(int key,int event)
{
	static char str[2]={0,0};
	switch(event){
		case KEY_DOWN:
			if(key==VK_DELETE||key==VK_BACK){
				if(location<0)return;
				int i;
				SetEraseMode(TRUE);
                if (isBlink && isDisplay) {
	 	     	   MovePen(GetCurrentX(), GetCurrentY());
 	          	   DrawTextString("_");
 	          	   MovePen(GetCurrentX()-TextStringWidth("_"), GetCurrentY());
		        }
 	 			str[0]=Text[location];
				MovePen(GetCurrentX()-TextStringWidth(str),GetCurrentY());
				DrawTextString(str);
				if(location==textlen)MovePen(GetCurrentX()-TextStringWidth(str),GetCurrentY());
				else{
					string s1,s2;
					s1=SubString(Text,location+1,textlen);
					s2=SubString(Text,location,textlen); 
					SetEraseMode(TRUE);
					DrawTextString(s1);
					SetEraseMode(FALSE);
					MovePen(GetCurrentX()-TextStringWidth(s2),GetCurrentY());
					DrawTextString(s1);
					MovePen(GetCurrentX()-TextStringWidth(s1),GetCurrentY());
				}
		        for(i=location;i<textlen;i++){
		        	Text[i]=Text[i+1];
		        }
		        Text[--textlen]='\0';
				location--;
 				SetEraseMode(FALSE);					
			}
			else if(key==VK_LEFT){
				if(location<0)return;
				if (isBlink && isDisplay) {
	 	     	   	SetEraseMode(TRUE);
	 	     	    MovePen(GetCurrentX(), GetCurrentY());
 	              	DrawTextString("_");
 	         	   	SetEraseMode(FALSE);
		           	MovePen(GetCurrentX()-TextStringWidth("_"), GetCurrentY());
		       	}
		       	str[0]=Text[location];
		       	MovePen(GetCurrentX()-TextStringWidth(str),GetCurrentY());
		       	location--;
			}
			else if(key==VK_RIGHT){
				if(location>=textlen)return; 
				if (isBlink && isDisplay) {
	 	     	   	SetEraseMode(TRUE);
	 	     	    MovePen(GetCurrentX(), GetCurrentY());
 	              	DrawTextString("_");
 	         	   	SetEraseMode(FALSE);
		           	MovePen(GetCurrentX()-TextStringWidth("_"), GetCurrentY());
		       	}
		       	str[0]=Text[location+1];
		       	MovePen(GetCurrentX()+TextStringWidth(str),GetCurrentY());
		       	location++;
			}
	}
}

void TimerEventProcess(int timerID)
{
	double x, y;
	bool erasemode;
	switch (timerID) {
		case TIMER_BLINK:
		if (!isBlink) return;
	    erasemode = GetEraseMode();
		x = GetCurrentX();
		y = GetCurrentY();
		SetEraseMode(isDisplay);
		DrawTextString("_");
		MovePen(x, y);
		if(textlen-1!=location){
			char str[2]={0,0};
			str[0]=Text[location+1];
			SetEraseMode(FALSE);
			DrawTextString(str);
			MovePen(x,y);
		}
	      SetEraseMode(erasemode);
		  isDisplay = !isDisplay;
		  break;
	    default:
		  break;
	  }
}

 


  • 24
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值