简体   繁体   English

如何使用 WIN32 API for C++ 更改文本的颜色和大小?

[英]How to change color and sizing of text using WIN32 API for C++?

I have been searching for days for an answer to my question, but I can't seem to find any solution which works for me... Possibly because I may be thinking of this all wrong.我一直在寻找我的问题的答案几天,但我似乎找不到任何适合我的解决方案......可能是因为我可能认为这一切都是错误的。

I am trying to create a basic window which pops up a string of text in a large red font using WIN32 API in C++.我正在尝试创建一个基本的 window,它使用 C++ 中的 WIN32 API 以大红色字体弹出一串文本。

I have tried using the WM_CTLCOLORSTATIC control within my WindowProcedure, but I can't seem to get the text to change color.我曾尝试在 WindowProcedure 中使用 WM_CTLCOLORSTATIC 控件,但我似乎无法让文本更改颜色。 However, I am able to create text with the WM_CREATE, so I am confused on what I am doing wrong...但是,我可以使用 WM_CREATE 创建文本,所以我对自己做错了什么感到困惑......

Here is what I have written so far.这是我到目前为止所写的。

#include <iostream>
#include <windows.h>

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

HWND textfield;
HDC hdc;

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nccmdshow) {

    WNDCLASSW wc = { 0 };

    wc.hbrBackground =  (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"myWindowClass";
    wc.lpfnWndProc = WindowProcedure;

    if (!RegisterClassW(&wc))
        return -1;

    CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 500, 200, 1000, 500,
        NULL, NULL, NULL, NULL);

    MSG msg = {0};

    while (GetMessage(&msg, NULL, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {


    switch (msg) {
    case WM_CREATE:
        textfield = CreateWindow(L"STATIC", L"DEBOODAH!", WS_VISIBLE | WS_CHILD , 500, 100, 300, 25, hWnd, NULL, NULL, NULL);
        break;

    case WM_CTLCOLORSTATIC:

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }

}

Any help would be greatly appreciated!任何帮助将不胜感激!

The WM_CTLCOLORSTATIC message gets a device context handle ( HDC ) in wParam. WM_CTLCOLORSTATIC 消息在 wParam 中获取设备上下文句柄 ( HDC )。 Here's how you use it:以下是你如何使用它:

case WM_CTLCOLORSTATIC:
    SetTextColor((HDC)wp, RGB(255, 0, 0));
    return DefWindowProcW(hWnd, msg, wp, lp);

The text size is a whole different story.文本大小是一个完全不同的故事。 Once you create the static window, you have to construct a font and feed it to the static:创建 static window 后,您必须构建一个字体并将其提供给 static:

case WM_CREATE:
    textfield = CreateWindow(/*...*/);
    HFONT hf = CreateFont(/*size=*/-12, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"MS Sans Serif");
    SendMessage(textfield, WM_SETFONT, (WPARAM)hf, 0);
    break;

Painting your own text, as Sebastian suggests, is also possible.正如塞巴斯蒂安建议的那样,绘制自己的文本也是可能的。

You could read about drawing inside a window.您可以阅读有关在 window 内部绘图的信息。

Typically one would put the following code inside case WM_PAINT:通常将以下代码放入 case WM_PAINT 中:

case WM_PAINT:
    PAINTSRUCT ps;
    HDC hdc = BeginPaint(hwnd, ps);
    // draw here
    EndPaint(hwnd, &ps);

With the hdc handle you can call functions like TextOutA/TextOutW, SetTextColors and others使用 hdc 句柄,您可以调用 TextOutA/TextOutW、SetTextColors 等函数

You create a 2nd smaller window inside, when the parent window is created (WM_CREATE).当创建父 window (WM_CREATE) 时,您在内部创建第二个较小的 window。 This smaller window can be preset only rudimentarily.这个较小的 window 只能进行初步预设。 Try to leave it away, when painting with WM_PAINT.在使用 WM_PAINT 进行绘画时,请尽量不要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM