define.h
#ifndef DEFINE_H_INCLUDED
#define DEFINE_H_INCLUDED
typedef short int16;
typedef int int32;
typedef long long int64;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef uint16 z_color;
static const int32 MOUSE_LEFT = 0;
static const int32 MOUSE_MIDDLE = 1;
static const int32 MOUSE_RIGHT = 2;
static const int32 MOUSE_DOWN = 3;
static const int32 MOUSE_UP = 4;
#endif // DEFINE_H_INCLUDED
gui.h
#ifndef GUI_H_INCLUDED
#define GUI_H_INCLUDED
#include "define.h"
#if defined(SYSTEM_WIN)
#include "gui_windows.h"
#endif
#endif // GUI_H_INCLUDED
gui_windows.h
#ifndef GUI_WINDOWS_H_INCLUDED
#define GUI_WINDOWS_H_INCLUDED
#include <assert.h>
#include <windows.h>
#include "define.h"
#define main fake_main
int fake_main();
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int);
LRESULT CALLBACK gui_mainProcess(HWND, UINT, WPARAM, LPARAM);
void gui_createMainWindow(const char *);
void gui_setMainWindowPosition(int32 x, int32 y);
void gui_setMainWindowSize(int32 w, int32 h);
void gui_setMainWindowToCenter(void);
void gui_setMainWindowMinimize(void);
void gui_setMainWindowMaximize(void);
void gui_setMainWindowRestore(void);
void gui_mainLoop(void);
void gui_repaint(void);
void gui_setPixel(int x, int y, int r, int g, int b);
z_color gui_getPixel(int x, int y);
void gui_setDisplayFunction(void (*func)(void));
void gui_setReshapeFunction(void (*func)(int32 w, int32 h));
void gui_setKeyboardFunction(void (*func)(int32 key, int32 state, int32 x, int32 y));
void gui_setMouseFunction(void (*func)(int32 button, int32 state, int32 x, int32 y));
void gui_setMotionFunction(void (*func)(int32 x, int32 y));
#endif // GUI_WINDOWS_H_INCLUDED
gui_windows.c
#include "gui_windows.h"
static HINSTANCE mainInstance;
static HWND handleMainWindow;
static HDC handleMainWindowDeviceContext;
static int32 systemWidth, systemHeight;
static void (*mainWindowDisplayFunction)();
static void (*mainWindowReshapeFunction)(int32, int32);
static void (*mainWindowKeyboardFunction)(int32, int32, int32, int32);
static void (*mainWindowMouseFunction)(int32, int32, int32, int32);
static void (*mainWindowMotionFunction)(int32, int32);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
mainInstance = hInstance;
systemWidth = GetSystemMetrics(SM_CXSCREEN);
systemHeight = GetSystemMetrics(SM_CYSCREEN);
return fake_main();
}
void gui_createMainWindow(const char *title)
{
WNDCLASS mainWindow;
mainWindow.style = CS_HREDRAW | CS_VREDRAW;
mainWindow.lpfnWndProc = gui_mainProcess;
mainWindow.cbClsExtra = 0;
mainWindow.cbWndExtra = 0;
mainWindow.hInstance = mainInstance;
mainWindow.hIcon = LoadIcon(NULL, IDI_APPLICATION);
mainWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
mainWindow.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
mainWindow.lpszMenuName = NULL;
mainWindow.lpszClassName = "mainWindow";
if(!RegisterClass(&mainWindow))
{
MessageBox(NULL, TEXT("Cannot register class for main window."), TEXT(title), MB_ICONERROR);
exit(0);
}
handleMainWindow = CreateWindow("mainWindow",
title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
mainInstance,
NULL);
ShowWindow(handleMainWindow, 1);
UpdateWindow(handleMainWindow);
}
void gui_setMainWindowPosition(int32 x, int32 y)
{
assert(handleMainWindow != NULL);
SetWindowPos(handleMainWindow, NULL, x, y, 0, 0, SWP_NOSIZE);
}
void gui_setMainWindowSize(int32 w, int32 h)
{
assert(handleMainWindow != NULL);
SetWindowPos(handleMainWindow, NULL, 0, 0, w, h, SWP_NOMOVE);
}
void gui_setMainWindowToCenter(void)
{
assert(handleMainWindow != NULL);
RECT rect;
GetWindowRect(handleMainWindow, &rect);
int32 w = rect.right - rect.left;
int32 h = rect.bottom - rect.top;
gui_setMainWindowPosition((systemWidth - w) >> 1, (systemHeight - h) >> 1);
}
void gui_setMainWindowMinimize(void)
{
assert(handleMainWindow != NULL);
SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_MINIMIZE, (LPARAM)NULL);
}
void gui_setMainWindowMaximize(void)
{
assert(handleMainWindow != NULL);
SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_MAXIMIZE, (LPARAM)NULL);
}
void gui_setMainWindowRestore(void)
{
assert(handleMainWindow != NULL);
SendMessage(handleMainWindow, WM_SYSCOMMAND, SC_RESTORE, (LPARAM)NULL);
}
void gui_mainLoop(void)
{
assert(handleMainWindow != NULL);
MSG message;
while(GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
void gui_repaint(void)
{
InvalidateRect(handleMainWindow, NULL, TRUE);
UpdateWindow(handleMainWindow);
}
void gui_setPixel(int x, int y, int r, int g, int b)
{
COLORREF color = RGB(r, g, b);
SetPixel(handleMainWindowDeviceContext, x, y, color);
}
z_color gui_getPixel(int x, int y)
{
return GetPixel(handleMainWindowDeviceContext, x, y);
}
LRESULT CALLBACK gui_mainProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
handleMainWindowDeviceContext = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
if(mainWindowDisplayFunction)
{
mainWindowDisplayFunction();
}
EndPaint(hwnd, &ps) ;
break;
case WM_MOUSEMOVE:
if(mainWindowMotionFunction)
{
mainWindowMotionFunction(LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_LBUTTONDOWN:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_LEFT, MOUSE_DOWN, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_LBUTTONUP:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_LEFT, MOUSE_UP, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_RBUTTONDOWN:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_RIGHT, MOUSE_DOWN, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_RBUTTONUP:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_RIGHT, MOUSE_UP, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_MBUTTONDOWN:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_MIDDLE, MOUSE_DOWN, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_MBUTTONUP:
if(mainWindowMouseFunction)
{
mainWindowMouseFunction(MOUSE_MIDDLE, MOUSE_UP, LOWORD(lParam), HIWORD(lParam));
}
break;
case WM_KEYDOWN:
break;
case WM_KEYUP:
break;
case WM_MOUSEWHEEL:
break;
case WM_DESTROY:
PostQuitMessage(0) ;
break;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void gui_setDisplayFunction(void (*func)(void))
{
mainWindowDisplayFunction = func;
}
void gui_setReshapeFunction(void (*func)(int32 w, int32 h))
{
mainWindowReshapeFunction = func;
}
void gui_setKeyboardFunction(void (*func)(int32 key, int32 state, int32 x, int32 y))
{
mainWindowKeyboardFunction = func;
}
void gui_setMouseFunction(void (*func)(int32 button, int32 state, int32 x, int32 y))
{
mainWindowMouseFunction = func;
}
void gui_setMotionFunction(void (*func)(int32 x, int32 y))
{
mainWindowMotionFunction = func;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "define.h"
#include "gui.h"
void displayFunction(void)
{
//TODO
}
void reshapeFunction(int32 w, int32 h)
{
//TODO
}
void keyboardFunction(int32 key, int32 state, int32 x, int32 y)
{
//TODO
}
void mouseFunction(int32 button, int32 state, int32 x, int32 y)
{
//TODO
}
void motionFunction(int32 x, int32 y)
{
//TODO
}
int main()
{
memset(t, 0, sizeof(t));
#ifndef DEBUG
freopen("log.txt", "w", stdout);
#endif
freopen("error.txt", "w", stderr);
gui_createMainWindow("ZPIC");
gui_setMainWindowSize(800, 600);
gui_setMainWindowToCenter();
gui_setDisplayFunction(displayFunction);
gui_setReshapeFunction(reshapeFunction);
gui_setKeyboardFunction(keyboardFunction);
gui_setMouseFunction(mouseFunction);
gui_setMotionFunction(motionFunction);
gui_mainLoop();
return 0;
}
本文介绍了一个基于 Windows 的图形用户界面应用程序框架,包括基本的数据类型定义、窗口创建及管理、事件处理等功能。该框架提供了设置显示回调、键盘鼠标事件处理等方法。

被折叠的 条评论
为什么被折叠?



