Skip to content

Instantly share code, notes, and snippets.

@watamario15
Last active December 12, 2023 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watamario15/0ce68d7825062094c7282fdc7c762efe to your computer and use it in GitHub Desktop.
Save watamario15/0ce68d7825062094c7282fdc7c762efe to your computer and use it in GitHub Desktop.
Sample program for SHARP Brain (https://oucc.org/blog/articles/303)
/*
* This program is licensed under the CC0 1.0 (Public Domain).
* このプログラムは CC0 1.0 (パブリックドメイン) でライセンスされています。
* https://creativecommons.org/publicdomain/zero/1.0/
*/
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#ifndef WS_OVERLAPPEDWINDOW
#define WS_OVERLAPPEDWINDOW \
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
#endif
#include <tchar.h>
#ifdef UNDER_CE
#if SW_MAXIMIZE != 12
#undef SW_MAXIMIZE
#define SW_MAXIMIZE 12
#endif
#if SW_MINIMIZE != 6
#undef SW_MINIMIZE
#define SW_MINIMIZE 6
#endif
#if WS_MINIMIZEBOX != 0x00010000L
#undef WS_MINIMIZEBOX
#define WS_MINIMIZEBOX 0x00010000L
#endif
#if WS_MAXIMIZEBOX != 0x00020000L
#undef WS_MAXIMIZEBOX
#define WS_MAXIMIZEBOX 0x00020000L
#endif
#ifndef _tWinMain
#define _tWinMain WinMain
#endif
#endif
#define WND_CLASS_NAME TEXT("brain-main")
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HFONT hFont = NULL;
static RECT rect;
switch (uMsg) {
case WM_CLOSE: // 閉じるボタン
DestroyWindow(hWnd);
break;
case WM_SIZE: { // ウィンドウサイズ変化時
GetClientRect(hWnd, &rect);
LOGFONT rLogfont;
ZeroMemory(&rLogfont, sizeof(rLogfont));
if (rect.right / 20 < rect.bottom / 4) {
rLogfont.lfHeight = rect.right / 20;
} else {
rLogfont.lfHeight = rect.bottom / 4;
}
rLogfont.lfWeight = FW_BOLD;
rLogfont.lfCharSet = DEFAULT_CHARSET;
rLogfont.lfQuality = ANTIALIASED_QUALITY;
_tcscpy(rLogfont.lfFaceName, TEXT("Tahoma"));
if (hFont) DeleteObject(hFont);
hFont = CreateFontIndirect(&rLogfont);
InvalidateRect(hWnd, NULL, TRUE);
break;
}
case WM_PAINT: { // 再描画要求受領
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(0, 255, 0));
SelectObject(hdc, hFont);
DrawText(hdc, TEXT("Welcome to the SHARP Brain!"), -1, &rect,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY: // 終了時
PostQuitMessage(0);
break;
default: // 無視するメッセージ
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
WNDCLASS wcl;
wcl.hInstance = hInstance;
wcl.lpszClassName = WND_CLASS_NAME;
wcl.lpfnWndProc = WindowProc;
wcl.style = 0;
wcl.hIcon = NULL;
#ifdef UNDER_CE
wcl.hCursor = NULL;
#else
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
#endif
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
if (!RegisterClass(&wcl)) return FALSE;
HWND hWnd = CreateWindowEx(0, WND_CLASS_NAME, TEXT("Sample App"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, NULL, NULL, hInstance, NULL);
if (!hWnd) return FALSE;
ShowWindow(hWnd, nShowCmd);
#ifdef UNDER_CE
ShowWindow(hWnd, SW_MAXIMIZE); // Brain は画面が小さいので最大化
#endif
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
return (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment