程式語言 - Wine - C/C++ - Create Font



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles

在Windows視窗程式設計中,當需要文字輸出顯示在視窗上時,一般都會使用自定義的字型,因為系統預設的字型太小,而自定義的字型,除了可以是粗體或者斜體,還可以設定自定義的長寬尺寸,司徒使用一個簡單例子來說明如何建立自定義的字型

main.c

#include <stdbool.h>
#include <windows.h>
   
static HWND hWin = NULL;
static WNDPROC defWndProc = NULL;
   
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc = NULL;
    HFONT font = NULL;
    PAINTSTRUCT ps = { 0 };
    const char *pMsg = "Test";
  
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
  
        font = CreateFont(48,
            0,
            0,
            0,
            FW_BOLD,
            false,
            false,
            false,
            0,
            OUT_OUTLINE_PRECIS,
            CLIP_DEFAULT_PRECIS,
            CLEARTYPE_QUALITY,
            DEFAULT_PITCH | FF_DONTCARE,
            "Arial"
        );
  
        SetTextColor(hdc, RGB(0xff, 0x00, 0x00));
        SetBkMode(hdc, TRANSPARENT);
        SelectObject(hdc, font);
        TextOut(hdc, 10, 10, pMsg, strlen(pMsg));
        EndPaint(hWnd, &ps);
        DeleteObject(font);
        break;
    }
 
    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}
   
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    hWin = CreateWindow(WC_DIALOG, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 320, 240, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWin, GWLP_WNDPROC, (long int)WndProc);
   
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
    }
 
    return 0;
}

編譯、執行

$ winegcc main.c -o main -lgdi32
$ wine main.exe.so