DrawText WinAPI不显示结果

时间:2016-07-15 20:59:19

标签: c++ c

我尝试使用Windows API DrawText()在当前程序的控制台窗口上绘制一个字符串,该程序假设(我认为)绘制一个" Hello World"绿色(0x33CC33),但我没有得到任何结果,我不知道我做错了什么。

这是一段代码:

#include <stdio.h>
#include <Windows.h>

int main(void) {
    RECT Rect;
    SetConsoleTitleA("Win32CPPTest");
    HWND WinHandle = FindWindowA("ConsoleWindowClass", "Win32CPPTest");
    if (WinHandle == NULL) {
        printf("Failed to Find Target Window Hande.\n");
        return GetLastError();
    };
    HDC WinDC = GetWindowDC(WinHandle);
    if (WinDC == NULL) {
        printf("Failed To Get Target Window DC.\n");
        return GetLastError();
    };
    GetClientRect(WinHandle, &Rect);    //get the RECT structure from target handle 
    SetTextColor(WinDC, 0x33CC33);      //set text color to green
    SetBkMode(WinDC, TRANSPARENT);      //set background mix mode
    Rect.left = 40;                     //set font format
    Rect.top = 10;
    DrawTextA(WinDC, "Hello World!", -1, &Rect, DT_SINGLELINE | DT_NOCLIP);    //this function call is not showing anything on console window
    DeleteDC(WinDC);    //I set a breakpoint here so the HDC structure won't be destory
    return 0;
};

0 个答案:

没有答案