First Window

This commit is contained in:
2025-11-06 00:19:19 +08:00
commit 6878d0d0de
5 changed files with 204 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
cmake-build-*/
.idea/
+21
View File
@@ -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 ()
+5
View File
@@ -0,0 +1,5 @@
#include <c_WinGuiApp.h>
c_WinGuiApp_t c_WinGuiApp = {0};
+105
View File
@@ -0,0 +1,105 @@
#ifndef INCLUDED_C_WINGUIAPP_H
#define INCLUDED_C_WINGUIAPP_H
#ifndef INCLUDED_WINDOWS_H
#define INCLUDED_WINDOWS_H
#include <windows.h>
#endif /*INCLUDED_WINDOWS_H*/
#ifndef INCLUDED_WINDOWSX_H
#define INCLUDED_WINDOWSX_H
#include <windowsx.h>
#endif /*INCLUDED_WINDOWSX_H*/
#ifndef INCLUDED_STRING_H
#define INCLUDED_STRING_H
#include <string.h>
#endif /*INCLUDED_STRING_H*/
#ifndef INCLUDED_STDLIB_H
#define INCLUDED_STDLIB_H
#include <stdlib.h>
#endif /*INCLUDED_STDLIB_H*/
#ifndef INCLUDED_TIME_H
#define INCLUDED_TIME_H
#include <time.h>
#endif /*INCLUDED_TIME_H*/
#ifndef INCLUDED_STDINT_H
#define INCLUDED_STDINT_H
#include <stdint.h>
#endif /*INCLUDED_STDINT_H*/
#ifndef INCLUDED_STDBOOL_H
#define INCLUDED_STDBOOL_H
#include <stdbool.h>
#endif /*INCLUDED_STDBOOL_H*/
#ifndef INCLUDED_STDATOMIC_H
#define INCLUDED_STDATOMIC_H
#include <stdatomic.h>
#endif /*INCLUDED_STDATOMIC_H*/
#ifndef INCLUDED_STDDEF_H
#define INCLUDED_STDDEF_H
#include <stddef.h>
#endif /*INCLUDED_STDDEF_H*/
#ifndef INCLUDED_INTTYPES_H
#define INCLUDED_INTTYPES_H
#include <inttypes.h>
#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*/
+70
View File
@@ -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)