Domanda

(non gestito C ++) Ho già riusciti disegno file PNG a una finestra a più livelli trasparente che posso trascinare attorno al desktop, ma ora il mio problema è il disegno di testo in una finestra a più livelli trasparente

Ecco il mio codice e il mio tentativo di disegnare il testo in mezzo, è importante notare che sto usando la screenDC invece di utilizzare quello in messaggi WM_PAINT

[modifica] codice aggiornato dopo i commenti, ora sto solo cercando di testo di scrittura sul bitmap prima di ottenere la versione HBITMAP che ho bisogno di usare questa volta sto usando DrawString perché TextOut () non è GDI +, spero davvero DrawString è GDI + lol ancora non funziona, però, chiedo che cosa sto facendo male

void Draw() // draws a frame on the layered window AND moves it based on x and y
{
    HDC screenDC( NULL ); // grab screen
    HDC sourceDC( CreateCompatibleDC(screenDC) );

    POINT pos = {x,y}; // drawing location
    POINT sourcePos = {0,0}; // top left of image
    SIZE size = {100,100}; // 100x100 image

    BLENDFUNCTION blendFunction = {0};
    HBITMAP bufferBitmap = {0};
    Bitmap* TheBitmap = crnimage; // crnimage was already loaded earlier

    // ------------important part goes here, my attempt at drawing text ------------//

 Gdiplus::Graphics     Gx(TheBitmap);

// Font* myFont =    new Font(sourceDC);
 Font myFont(L"Arial", 16);


 RectF therect;
 therect.Height = 20;
 therect.Width = 180;
 therect.X = 0;
 therect.Y = 0;

 StringFormat format;
 format.SetAlignment(StringAlignmentCenter);
 format.GenericDefault();
 Gdiplus::SolidBrush   GxTextBrush(Gdiplus::Color(255, 255, 0,255));


 WCHAR thetext[] = L"Sample Text";

 int stats = Gx.DrawString(thetext, -1, &myFont, therect, &format, &GxTextBrush);
 if(stats) // DrawString returns nonzero if there is an error
     msgbox(stats); 
 stats = Gx.DrawRectangle(&Pen(Color::Red, 3), therect);
 // the rectangle and text both draw fine now

 // ------------important part goes here, my attempt at drawing text ------------//

    TheBitmap->GetHBITMAP(0, &bufferBitmap);
    HBITMAP oldBmpSelInDC;
    oldBmpSelInDC = (HBITMAP)SelectObject(sourceDC, bufferBitmap);

    // some alpha blending
    blendFunction.BlendOp = AC_SRC_OVER;
    blendFunction.SourceConstantAlpha = wndalpha;
    blendFunction.AlphaFormat = AC_SRC_ALPHA;
    COLORREF colorKey( RGB(255,0,255) );
    DWORD flags( ULW_ALPHA);

    UpdateLayeredWindow(hWnd, screenDC, &pos, & size, sourceDC, &sourcePos,
    colorKey, &blendFunction, flags);

    // release buffered image from memory
    SelectObject(sourceDC, oldBmpSelInDC);
    DeleteDC(sourceDC);
    DeleteObject(bufferBitmap); 

    // finally release the screen
    ReleaseDC(0, screenDC);
}

Ho cercato di scrivere il testo sulla mia finestra a più livelli da due giorni, ma da quei tentativi So che ci sono molti modi in cui posso andare a fare questo (Purtroppo non ho idea di come esattamente)

La solita opzione che vedo è il disegno di testo in una bitmap, quindi rendendo il bitmap in sé

  1. Utilizzare GDI + per caricare una bitmap
  2. Crea un oggetto Graphics dal bitmap
  3. Usa DrawString per il testo di scrittura alla bitmap
  4. Eliminare l'oggetto Graphics
  5. Utilizzare il bitmap Salva metodo per salvare il risultato in un file

A quanto pare si può anche fare un oggetto grafico da una corrente continua, quindi disegnare testo sulla DC, ma ancora una volta non ho idea di come fare questo

È stato utile?

Soluzione

L'approccio generale sembra di destra, ma penso che hai avuto qualche problema con la chiamata DrawString. Controlla la documentazione (in particolare il campione) su MSDN .

Gx.DrawString(thetext, 4, NULL, therect, NULL,  NULL)

Il terzo, quinto, sesto e parametri (tipo di carattere, il formato e spazzola) probabilmente bisogno di essere specificato. La documentazione non dice che essi sono opzionali. Passando NULL per questi è probabilmente causato da GDI + per trattare la chiamata come un no-op.

Il secondo parametro non dovrebbe includere la terminazione L '\ 0' nella stringa. E 'probabilmente più sicuro per uso -1 se la stringa è sempre terminato.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top