2014-12-03 101 views
1

我试图在屏幕上显示文本,在任何情况下都不可点击,而没有任何窗口。这个想法是能够显示通知。我与我想要的有点接近,但是一个真正奇怪的问题刚刚显示。这是代码:如何在没有窗口的情况下绘制文本

#include <Windows.h> 

int main(void){ 

    HDC hdc = ::GetDC(0); 
    RECT rect; 
    SetTextColor(hdc, RGB(0, 0, 255)); 
    SetBkMode(hdc, TRANSPARENT); 
    SetBkColor(hdc, RGB(0, 0, 0)); 
    auto hFont = CreateFont(40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, L"Verdana"); 
    auto hTmp = (HFONT)SelectObject(hdc, hFont); 
    rect.left = 40; 
    rect.top = 10; 

    while (true){ 
     DrawText(hdc, L"THIS IS A TEXT", -1, &rect, DT_SINGLELINE | DT_NOCLIP); 
     Sleep(1); 
    } 
    DeleteObject(SelectObject(hdc, hTmp)); 
    ::ReleaseDC(0, hdc); 
    return 0; 

} 

,这是当我改变了文本设置从redblue和大小8040会发生什么:

buggz

出于某种原因,我仍然可以看到旧的文本,重新运行程序后,这告诉我,我误解了一些东西。有没有更好,更干净的方法来做到这一点?

编辑:我检查了Windows通知,这不是一个解决方案。想象一下,你正在玩一个全屏游戏,并想知道一封电子邮件是否到达。另一个重要的事情是它不能点击,所以误点击不会最小化你的游戏。当你打电话时,如何让你的应用程序最小化的Skype弹出窗口令人讨厌?

+0

您正在主设备上下文中绘图。 – mudasobwa 2014-12-03 11:58:56

+0

@mudasobwa你能否澄清你的评论?那是什么意思? – SlySherZ 2014-12-03 12:00:09

+0

“这个想法是能够显示通知。”自定义通知系统非常糟糕,最好不要收到任何通知。当你的操作系统支持一个内置的系统通知机制(如Windows 10)就可以使用它。 – bames53 2014-12-03 12:01:57

回答

2

您已绕过所有的窗口/客户端控件,因此系统不知道该区域需要清除。你需要手动告诉它,特别是因为你没有使用Windows消息通知机制。

之前绘制它,你想无效屏幕的一部分,告诉窗口重绘:

​​
+0

谢谢,这工作!还有一件事:有没有办法让这项工作通过全屏应用程序? – SlySherZ 2014-12-03 12:56:10

+2

如果有人正在使用全屏应用,那么他们不想看到通知。不要进入最高级别的比赛。 – 2014-12-03 13:06:06

1

我结束了运行到很多的问题,同时努力使这项工作。如果有人最终访问此页面寻找对我遇到的同样问题的答案,我希望你比我有更容易的时间。这是我的代码:

#include <Windows.h> 


INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    PSTR lpCmdLine, INT nCmdShow) 
{ 
    // Define and initialize variables 
    HDC   hdc; 
    HDC   hdcMem; 
    HBITMAP  hbmMem; 
    HANDLE  hOld; 
    RECT rect; 
    SIZE sz; 
    int win_width = 0; 
    int win_height = 0; 
    int font_size = 20; 
    int location_x = 40; 
    int location_y = 40; 
    int border = font_size/4; 
    int duration = 10000;   // In miliseconds. The notification will always stay up more time 
    wchar_t* font_face = L"Consolas"; 
    wchar_t message[100]; 

    // Save command-line arguments to message; They are showed by the notification 
    MultiByteToWideChar(0, 0, 
     lpCmdLine, 
     strlen(lpCmdLine), 
     message, 
     100 
     ); 
    message[strlen(lpCmdLine)] = L'\0'; 


    // Acquire screen 
    hdc = ::GetDC(0); 

    //Create necessary font 
    HFONT hFont = CreateFont(font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, font_face); 
    HFONT hTmp = (HFONT)SelectObject(hdc, hFont); 

    // Calculate size of the text 
    GetTextExtentPoint32(hdc, message, wcslen(message), &sz); 
    win_width = sz.cx; 
    win_height = sz.cy; 
    rect = { 0, 0, sz.cx, sz.cx }; 

    // Create an off-screen DC for double-buffering 
    hdcMem = CreateCompatibleDC(hdc); 
    hbmMem = CreateCompatibleBitmap(hdc, win_width + 2 * border, win_height + 2 * border); 

    // Configure off-screen DC 
    SetBkMode(hdcMem, OPAQUE); 
    SetTextColor(hdcMem, RGB(125, 125, 255)); 
    SetBkColor(hdcMem, RGB(0, 0, 0)); 
    SelectObject(hdcMem, hFont); 
    hOld = SelectObject(hdcMem, hbmMem); 

    // Draw loop 
    for (int i = 0; i < duration; i++) 
    { 
     // Draw into hdcMem 
     DrawText(hdcMem, message, -1, &rect, DT_SINGLELINE); 

     // Transfer the off-screen DC to the screen 
     BitBlt(hdc, location_x, location_y, win_width + 2 * border, win_height + 2 * border, hdcMem, -5, -5, SRCCOPY); 


     // Don't eat all the cpu! 
     Sleep(1); 
    } 

    // Delete notification right after time expires 
    ::InvalidateRect(0, &rect, false); 
    ::UpdateWindow(0); 

    // Free-up the off-screen DC 
    SelectObject(hdcMem, hOld); 
    DeleteObject(hbmMem); 
    DeleteDC(hdcMem); 

    // Release created objects 
    DeleteObject(SelectObject(hdc, hTmp)); 
    ::ReleaseDC(0, hdc); 
    return 0; 
} 

它仍然可以改善,很多。唯一出现的是带有通知的矩形。 您传递给程序的参数将显示为消息。与hdcMem相关的所有内容都已实施,以避免闪烁。我还不能改变更大矩形的背景。

相关问题