上一篇: MiniGUI源码分析-- 开始篇
接下来,通过剖析MiniGUI的最简单的例程,来详细说明MiniGUI程序是如何创建和运行的。
这个例程,可以从很多地方得到,凡是接触过MiniGUI的朋友,首先接触的便是这个例子,为了方便大家阅读,贴在下面
/*
** $Id: helloworld.c,v 1.38 2007-10-25 07:56:45 weiym Exp $
**
** Listing 2.1
**
** helloworld.c: Sample program for MiniGUI Programming Guide
** The first MiniGUI application.
**
** Copyright (C) 2004 ~ 2007 Feynman Software.
**
** License: GPL
*/
#include <stdio.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
static char welcome_text [512];
static char msg_text [256];
static RECT welcome_rc = {10, 100, 600, 400};
static RECT msg_rc = {10, 100, 600, 400};
static const char* syskey = "";
static int last_key = -1;
static int last_key_count = 0;
static void make_welcome_text (void)
{
const char* sys_charset = GetSysCharset (TRUE);
const char* format;
if (sys_charset == NULL)
sys_charset = GetSysCharset (FALSE);
SetRect (&welcome_rc, 10, 10, g_rcScr.right - 10, g_rcScr.bottom / 2 - 10);
SetRect (&msg_rc, 10, welcome_rc.bottom + 10, g_rcScr.right - 10, g_rcScr.bottom - 20);
if (strcmp (sys_charset, FONT_CHARSET_GB2312_0) == 0
|| strcmp (sys_charset, FONT_CHARSET_GBK) == 0) {
format = "欢迎来到 MiniGUI 的世界! 如果您能看到该文本, 则说明 MiniGUI Version %d.%d.%d 可在该硬件上运行!";
}
else if (strcmp (sys_charset, FONT_CHARSET_BIG5) == 0) {
format = "欢迎来到 MiniGUI 的世界! 如果您能看到该文本, 则说明 MiniGUI Version %d.%d.%d 可在该硬件上运行!";
}
else {
format = "Welcome to the world of MiniGUI. \nIf you can see this text, MiniGUI Version %d.%d.%d can run on this hardware board.";
}
sprintf (welcome_text, format, MINIGUI_MAJOR_VERSION, MINIGUI_MINOR_VERSION, MINIGUI_MICRO_VERSION);
strcpy (msg_text, "No message so far.");
}
static int HelloWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
syskey = "";
switch (message) {
case MSG_CREATE:
make_welcome_text ();
SetTimer (hWnd, 100, 200);
break;
case MSG_TIMER:
sprintf (msg_text, "Timer expired, current tick count: %ul.",
GetTickCount ());
InvalidateRect (hWnd, &msg_rc, TRUE);
break;
case MSG_LBUTTONDOWN:
strcpy (msg_text, "The left button pressed.");
InvalidateRect (hWnd, &msg_rc, TRUE);
break;
case MSG_LBUTTONUP:
strcpy (msg_text, "The left button released.");
InvalidateRect (hWnd, &msg_rc, TRUE);
break;
case MSG_RBUTTONDOWN:
strcpy (msg_text, "The right button pressed.");
InvalidateRect (hWnd, &msg_rc, TRUE);
break;