简单的MiniGUI软键盘

由于已经购买mGi模块,故而自己写的这个用不着了,留在这里做个记念吧。以下是关键代码,其实思想蛮简单,就是发送消息给输入法窗口就万事大吉了。但是有个最重要的bug就是中文输入无法选择所有的汉字,估计要改一下minigui输入法的源码,mGi的软键盘是利用了两行。示例图如下,很简陋,如果经过美化,可以做到很漂亮。
 
#include "common.h"

#define ITEM_NUM1	14  /* coolbar */
#define ITEM_NUM2	14  /* coolbar */
#define ITEM_NUM3    12  /* coolbar */
#define ITEM_NUM4	12  /* coolbar */
#define ITEM_NUM5	8  /* coolbar */
#define IDC_NovaSKB1  1001  /* ime */
#define IDC_NovaSKB2  1002  /* ime */
#define IDC_NovaSKB3  1003  /* ime */
#define IDC_NovaSKB4  1004  /* ime */
#define IDC_NovaSKB5  1005  /* ime */

#define HEIGHT_SKB 115 /* 软键盘 */
#define WIDTH_SKB 240 /* 软键盘 */

static const char* caption1[] =
{
   " `  ", " 1 ", " 2 ", " 3 ", " 4 ", " 5  "," 6 ", " 7 ", " 8 ", " 9 ", " 0  ",  " -  ", " = ", " <- "
};

static const int SCANCODE1[] = 
{
	SCANCODE_GRAVE, SCANCODE_1, SCANCODE_2, SCANCODE_3, SCANCODE_4, 
	SCANCODE_5, SCANCODE_6, SCANCODE_7, SCANCODE_8, SCANCODE_9, 
	SCANCODE_0, SCANCODE_MINUS, SCANCODE_EQUAL, SCANCODE_BACKSPACE
};

static const char* caption2[] =
{
   "Tab", " q ", " w ", " e ", " r ", " t  ","  y ", " u ", " i ", " o ", " p ",  "  [  ", "  ]  ", " \\ "
};

static const int SCANCODE2[] = 
{
	SCANCODE_TAB, SCANCODE_Q, SCANCODE_W, SCANCODE_E, SCANCODE_R, 
	SCANCODE_T, SCANCODE_Y, SCANCODE_U, SCANCODE_I, SCANCODE_O, 
	SCANCODE_P, SCANCODE_BRACKET_LEFT, SCANCODE_BRACKET_RIGHT, SCANCODE_BACKSLASH
};

static const char* caption3[] =
{
   "Shift  ", " a ", " s ", " d ", " f ", " g  ","  h ", " j ", " k ", " l ", "  ;  ",  " ' "
};
static const int SCANCODE3[] = 
{
	SCANCODE_LEFTSHIFT, SCANCODE_A, SCANCODE_S, SCANCODE_D, SCANCODE_F, 
	SCANCODE_G, SCANCODE_H, SCANCODE_J, SCANCODE_K, SCANCODE_L, 
	SCANCODE_SEMICOLON, SCANCODE_APOSTROPHE
};


static const char* caption4[] =
{
   "Ctrl       ", " z ", " x ", " c ", " v ", " b  ","  n ", " m ",  "  , ",  " . ", " / ", " Enter "
};
static const int SCANCODE4[] = 
{
	SCANCODE_LEFTCONTROL, SCANCODE_Z, SCANCODE_X, SCANCODE_C, SCANCODE_V, 
	SCANCODE_B, SCANCODE_N, SCANCODE_M, SCANCODE_COMMA, SCANCODE_PERIOD, 
	SCANCODE_SLASH, SCANCODE_ENTER
};

static const char* caption5[] =
{
   "Del    ", "  英  ", "  中  ", "   Space   ",
   	"  |  ",  "  |  ", " -> ", " <- "
};

static const int SCANCODE5[] = 
{
	SCANCODE_REMOVE, -1, -1, SCANCODE_SPACE, SCANCODE_CURSORBLOCKDOWN,
	SCANCODE_CURSORBLOCKUP, SCANCODE_CURSORBLOCKLEFT, SCANCODE_CURSORBLOCKRIGHT
};

static void create_novaskb1 (HWND hWnd)
{
	HWND cb;
	COOLBARITEMINFO item;
	int i;
	static BITMAP del_bmp;

	if (LoadBitmap (HDC_SCREEN, &del_bmp, "./little/del.bmp") != ERR_BMP_OK)
		exit(1);

	cb = CreateWindow (CTRL_COOLBAR,
                    "",
                    WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    IDC_NovaSKB1,
                    0, 0, 240, 20,
                    hWnd,
                    0);

	item.ItemType = TYPE_TEXTITEM;
	item.Bmp = NULL;
	item.dwAddData = 0;
    for (i = 0; i < ITEM_NUM1-1; i++) {
	item.insPos = i;
	item.id = i;
	item.Caption = caption1[i];
	item.ItemHint = NULL;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
    }
	/* del 按键 */
        item.insPos = i;
        item.id = i;
        item.ItemType = TYPE_BMPITEM;
        item.Bmp = &del_bmp;
        item.ItemHint = NULL;
        item.Caption = NULL;
        item.dwAddData= 0;
        SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);

}

static void create_novaskb2 (HWND hWnd)
{
    HWND cb;
    COOLBARITEMINFO item;
    int i;

    cb = CreateWindow (CTRL_COOLBAR,
                    "",
                    WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    IDC_NovaSKB2,
                    0, 22, 240, 20,
                    hWnd,
                    0);

    item.ItemType = TYPE_TEXTITEM;
    item.Bmp = NULL;
	item.dwAddData = 0;
    for (i = 0; i < ITEM_NUM2; i++) {
	item.insPos = i;
	item.id = i;
	item.Caption = caption2[i];
	item.ItemHint = NULL;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
    }
}

static void create_novaskb3 (HWND hWnd)
{
    HWND cb;
    COOLBARITEMINFO item;
    int i;

    cb = CreateWindow (CTRL_COOLBAR,
                    "",
                    WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    IDC_NovaSKB3,
                    0, 44, 240, 20,
                    hWnd,
                    0);

    item.ItemType = TYPE_TEXTITEM;
    item.Bmp = NULL;
	item.dwAddData = 0;
    for (i = 0; i < ITEM_NUM3; i++) {
	item.insPos = i;
	item.id = i;
	item.Caption = caption3[i];
	item.ItemHint = NULL;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
    }
}
static void create_novaskb4 (HWND hWnd)
{
    HWND cb;
    COOLBARITEMINFO item;
    int i;

    cb = CreateWindow (CTRL_COOLBAR,
                    "",
                    WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    IDC_NovaSKB4,
                    0, 66, 240, 20,
                    hWnd,
                    0);

    item.ItemType = TYPE_TEXTITEM;
    item.Bmp = NULL;
	item.dwAddData = 0;
    for (i = 0; i < ITEM_NUM4; i++) {
	item.insPos = i;
	item.id = i;
	item.Caption = caption4[i];
	item.ItemHint = NULL;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
    }
}
static void create_novaskb5 (HWND hWnd)
{
	HWND cb;
	COOLBARITEMINFO item;
	int i;
	static BITMAP down_bmp;
	static BITMAP up_bmp;
	static BITMAP left_bmp;
	static BITMAP right_bmp;

	if (LoadBitmap (HDC_SCREEN, &down_bmp, "./little/down.bmp") != ERR_BMP_OK)
		exit(1);
	if (LoadBitmap (HDC_SCREEN, &up_bmp, "./little/up.bmp") != ERR_BMP_OK)
		exit(1);
	if (LoadBitmap (HDC_SCREEN, &left_bmp, "./little/left.bmp") != ERR_BMP_OK)
		exit(1);
	if (LoadBitmap (HDC_SCREEN, &right_bmp, "./little/right.bmp") != ERR_BMP_OK)
		exit(1);


    cb = CreateWindow (CTRL_COOLBAR,
                    "",
                    WS_CHILD | WS_VISIBLE | WS_BORDER, 
                    IDC_NovaSKB5,
                    0, 88, 240, 20,
                    hWnd,
                    0);

    item.ItemType = TYPE_TEXTITEM;
    item.Bmp = NULL;
	item.dwAddData = 0;
    for (i = 0; i < ITEM_NUM5 - 4; i++) {
	item.insPos = i;
	item.id = i;
	item.Caption = caption5[i];
	item.ItemHint = NULL;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
    }
#if 1
    	/* down 按键 */
        item.insPos = i;
        item.id = i;
        item.ItemType = TYPE_BMPITEM;
        item.Bmp = &down_bmp;
        item.ItemHint = NULL;
        item.Caption = NULL;
        item.dwAddData= 0;
        SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);

    	/* up 按键 */
	i++;
	item.insPos = i;
	item.id = i;
	item.ItemType = TYPE_BMPITEM;
	item.Bmp = &up_bmp;
	item.ItemHint = NULL;
	item.Caption = NULL;
	item.dwAddData= 0;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);

    	/* left 按键 */
    	i++;
	item.insPos = i;
	item.id = i;
	item.ItemType = TYPE_BMPITEM;
	item.Bmp = &left_bmp;
	item.ItemHint = NULL;
	item.Caption = NULL;
	item.dwAddData= 0;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);

    	/* right 按键 */
    	i++;
	item.insPos = i;
	item.id = i;
	item.ItemType = TYPE_BMPITEM;
	item.Bmp = &right_bmp;
	item.ItemHint = NULL;
	item.Caption = NULL;
	item.dwAddData= 0;
	SendMessage (cb, CBM_ADDITEM, 0, (LPARAM)&item);
#endif

}

static int NovaSKBProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case MSG_CREATE:
			create_novaskb1(hWnd);
			create_novaskb2(hWnd);
			create_novaskb3(hWnd);
			create_novaskb4(hWnd);
			create_novaskb5(hWnd);
		 break;

	case MSG_COMMAND:
	{
		int id = LOWORD (wParam);
		int code = HIWORD (wParam);
			
		switch (id)
		{
			case IDC_NovaSKB1:
				SendMessage (hIMEWnd, MSG_KEYDOWN, SCANCODE1[code], 0);
				break;
			case IDC_NovaSKB2:
				SendMessage (hIMEWnd, MSG_KEYDOWN, SCANCODE2[code], 0);
				break;
			case IDC_NovaSKB3:
				SendMessage (hIMEWnd, MSG_KEYDOWN, SCANCODE3[code], 0);
				break;
			case IDC_NovaSKB4:
				SendMessage (hIMEWnd, MSG_KEYDOWN, SCANCODE4[code], 0);
				break;
			case IDC_NovaSKB5:
				if (code == 1)
				{
					/* 英文 */
					SetIMEStatus(IS_ENABLE, 0);
					/* 刷新输入窗口 */
					ShowWindow(hIMEWnd, SW_HIDE);
					ShowWindow(hIMEWnd, SW_SHOWNORMAL);
				}
				else if (code == 2)
				{
					/* 中文 */
					SetIMEStatus(IS_ENABLE, 1);
					/* 刷新输入窗口 */
					ShowWindow(hIMEWnd, SW_HIDE);
					ShowWindow(hIMEWnd, SW_SHOWNORMAL);
				}
				else
				{
					SendMessage (hIMEWnd, MSG_KEYDOWN, SCANCODE5[code], 0);
//				        Send2ActiveClient (MSG_KEYDOWN, SCANCODE_CURSORDOWN, 0);
//				        Send2ActiveClient (MSG_KEYUP, SCANCODE_CURSORDOWN, 0);
				}
				break;
		}
		
	}
	break;

    case MSG_KEYDOWN:
        Send2ActiveClient (MSG_KEYDOWN, wParam, lParam);
        break;

    case MSG_DESTROY:
        DestroyAllControls (hWnd);
	return 0;

    case MSG_CLOSE:
        DestroyMainWindow (hWnd);
        PostQuitMessage (hWnd);
        return 0;
    }

    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}


static void InitNovaSKBCreateInfo (PMAINWINCREATE pCreateInfo, int x, int y) 
{ 
    pCreateInfo->dwStyle = WS_ABSSCRPOS | WS_BORDER | WS_VISIBLE; 
    pCreateInfo->dwExStyle = WS_EX_TOOLWINDOW; 
    pCreateInfo->spCaption = "NovaSKB" ; 
    pCreateInfo->hMenu = 0; 
    pCreateInfo->hCursor = GetSystemCursor(IDC_ARROW); 
    pCreateInfo->hIcon = 0; 
    pCreateInfo->MainWindowProc = NovaSKBProc; 
    pCreateInfo->lx = x; 
    pCreateInfo->ty = y; 
    pCreateInfo->rx = x + WIDTH_SKB; 
    pCreateInfo->by = y + HEIGHT_SKB;

    pCreateInfo->iBkColor = PIXEL_lightwhite	; 
    pCreateInfo->dwAddData = 0; 
    pCreateInfo->hHosting = HWND_DESKTOP; 
} 

HWND NovaSKBWindow(HWND hosting, int x, int y)
{
    HWND hwnd_novaskb;
    MAINWINCREATE CreateInfo;

    InitNovaSKBCreateInfo(&CreateInfo, x, y);
    CreateInfo.hHosting = hosting;

    hwnd_novaskb = CreateMainWindow (&CreateInfo);
    if (hwnd_novaskb == HWND_INVALID)
		return -1;
    ShowWindow(hwnd_novaskb, SW_HIDE);

    return hwnd_novaskb;
}

你可能感兴趣的:(简单的MiniGUI软键盘)