From 6878d0d0dea95f70a0cdae0aa51e5eb4bb905745 Mon Sep 17 00:00:00 2001 From: Chen Peng Date: Thu, 6 Nov 2025 00:19:19 +0800 Subject: [PATCH] First Window --- .gitignore | 3 ++ CMakeLists.txt | 21 ++++++++++ c_WinGuiApp.c | 5 +++ c_WinGuiApp.h | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ c_WinGuiApp.t.c | 70 ++++++++++++++++++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 c_WinGuiApp.c create mode 100644 c_WinGuiApp.h create mode 100644 c_WinGuiApp.t.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e560d4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cmake-build-*/ +.idea/ + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9bc10f3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.15) +project(WinGUI C) + +set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +file(GLOB SOURCES ${SOURCE_DIR}/*.c) +list(FILTER SOURCES EXCLUDE REGEX \\.t\\.c) +foreach (item IN LISTS SOURCES) + message(STATUS "[${PROJECT_NAME}] SOURCE: ${item}") +endforeach () + +add_library(${PROJECT_NAME} "") +target_sources(${PROJECT_NAME} PUBLIC ${SOURCES}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +file(GLOB TEST_SOURCES ${SOURCE_DIR}/*.t.c) +foreach (item IN LISTS TEST_SOURCES) + get_filename_component(TestUnit ${item} NAME_WLE) + message(STATUS "[${PROJECT_NAME}] TEST UNIT: ${TestUnit} - ${item}") + add_executable(${TestUnit} ${item}) + target_link_libraries(${TestUnit} PRIVATE ${PROJECT_NAME}) +endforeach () \ No newline at end of file diff --git a/c_WinGuiApp.c b/c_WinGuiApp.c new file mode 100644 index 0000000..a8a6245 --- /dev/null +++ b/c_WinGuiApp.c @@ -0,0 +1,5 @@ +#include + +c_WinGuiApp_t c_WinGuiApp = {0}; + + diff --git a/c_WinGuiApp.h b/c_WinGuiApp.h new file mode 100644 index 0000000..2dc23c6 --- /dev/null +++ b/c_WinGuiApp.h @@ -0,0 +1,105 @@ +#ifndef INCLUDED_C_WINGUIAPP_H +#define INCLUDED_C_WINGUIAPP_H + +#ifndef INCLUDED_WINDOWS_H +#define INCLUDED_WINDOWS_H +#include +#endif /*INCLUDED_WINDOWS_H*/ + +#ifndef INCLUDED_WINDOWSX_H +#define INCLUDED_WINDOWSX_H +#include +#endif /*INCLUDED_WINDOWSX_H*/ + +#ifndef INCLUDED_STRING_H +#define INCLUDED_STRING_H +#include +#endif /*INCLUDED_STRING_H*/ + +#ifndef INCLUDED_STDLIB_H +#define INCLUDED_STDLIB_H +#include +#endif /*INCLUDED_STDLIB_H*/ + +#ifndef INCLUDED_TIME_H +#define INCLUDED_TIME_H +#include +#endif /*INCLUDED_TIME_H*/ + +#ifndef INCLUDED_STDINT_H +#define INCLUDED_STDINT_H +#include +#endif /*INCLUDED_STDINT_H*/ + +#ifndef INCLUDED_STDBOOL_H +#define INCLUDED_STDBOOL_H +#include +#endif /*INCLUDED_STDBOOL_H*/ + +#ifndef INCLUDED_STDATOMIC_H +#define INCLUDED_STDATOMIC_H +#include +#endif /*INCLUDED_STDATOMIC_H*/ + +#ifndef INCLUDED_STDDEF_H +#define INCLUDED_STDDEF_H +#include +#endif /*INCLUDED_STDDEF_H*/ + +#ifndef INCLUDED_INTTYPES_H +#define INCLUDED_INTTYPES_H +#include +#endif /*INCLUDED_INTTYPES_H*/ + + + + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + +typedef struct { + HINSTANCE hInstance; + HWND hMainWindow; +}c_WinGuiApp_t; + +typedef bool (*c_WinGuiApp_fRegister_t)(HINSTANCE hInstance); +typedef HWND (*c_WinGuiApp_fCreate_t)(HINSTANCE hInstance, int nCmdShow); + +extern c_WinGuiApp_t c_WinGuiApp; +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + +#define c_WinGuiApp_Main(fRegister, fCreate) \ +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow){ \ + MSG Msg; \ + if(!hPrevInstance){ \ + if(!fRegister(hInstance)){ \ + return FALSE; \ + } \ + } \ + c_WinGuiApp.hMainWindow = fCreate(hInstance, nCmdShow); \ + if(!c_WinGuiApp.hMainWindow){ \ + return FALSE; \ + } \ + while(GetMessage(&Msg, NULL, 0, 0)){ \ + TranslateMessage(&Msg); \ + DispatchMessage(&Msg); \ + } \ + return Msg.wParam; \ +} + +#define c_WinGuiApp_OnEvent(msg, handler) case msg: return HANDLE_##msg(hWnd, wParam, lParam, handler); + +#define c_WinGuiApp_EventBegin(name) \ +static LRESULT CALLBACK name(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam){ \ + switch (Message) { + + +#define c_WinGuiApp_EventEnd\ + default: \ + return DefWindowProc(hWnd, Message, wParam, lParam); \ + } \ +} + + +#endif /*INCLUDED_C_WINGUIAPP_H*/ diff --git a/c_WinGuiApp.t.c b/c_WinGuiApp.t.c new file mode 100644 index 0000000..3ca5f41 --- /dev/null +++ b/c_WinGuiApp.t.c @@ -0,0 +1,70 @@ +#include "c_WinGuiApp.h" + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + +#define MAIN_WINDOW_CLASSNAME "c_WinGuiApp_Main" + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + +static void MainWindow_OnDestroy(HWND hwnd){ + PostQuitMessage(0); +} + +static void MainWindow_OnPaint(HWND hwnd){ + PAINTSTRUCT PaintStruct; + HDC hdc = BeginPaint(hwnd, &PaintStruct); + TextOutA(hdc, 10, 10, "Hello", 5); + EndPaint(hwnd, &PaintStruct); +} + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + +c_WinGuiApp_EventBegin(WndProc) + c_WinGuiApp_OnEvent(WM_DESTROY, MainWindow_OnDestroy) + c_WinGuiApp_OnEvent(WM_PAINT, MainWindow_OnPaint) +c_WinGuiApp_EventEnd + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + + + +static bool Register(HINSTANCE hInstance){ + WNDCLASS WndClass; + WndClass.lpszClassName = MAIN_WINDOW_CLASSNAME; + WndClass.style = CS_HREDRAW | CS_VREDRAW; + WndClass.cbWndExtra = 0; + WndClass.cbClsExtra = 0; + WndClass.hInstance = hInstance; + WndClass.lpfnWndProc = WndProc; + WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); + WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + WndClass.lpszMenuName = NULL; + return (RegisterClass(&WndClass)!=0); +} + + +static HWND Create(HINSTANCE hInstance, int nCmdShow){ + HWND hWindow = CreateWindowEx(0, MAIN_WINDOW_CLASSNAME, MAIN_WINDOW_CLASSNAME, + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL); + if(!hWindow) return NULL; + c_WinGuiApp.hMainWindow = hWindow; + ShowWindow(hWindow, nCmdShow); + UpdateWindow(hWindow); + return hWindow; +} + +/* -------------------------------------------------------------------------------------------------------------- */ +/* */ + + +c_WinGuiApp_Main(Register, Create) + +