如何在窗口中添加一些文本行?

时间:2019-03-25 14:53:40

标签: c++ winapi window

我能够用Tile创建一个窗口。现在如何在窗口中添加新的文本行?

我成功完成的所有操作只是更改了窗口标题,而这并不是我想要的。我想在窗口框中添加一些文本行。

SendMessage函数不适用于我。

如果有人对此有建议,请告诉我!

\\

enter image description here

3 个答案:

答案 0 :(得分:1)

要在客户区中绘制文本,您的wndProc通常会使用DrawTextTextOut之类的东西。通常,您这样做是为了响应WM_PAINT

为了能够回复外部消息,通常会发送一条包含文本的消息。窗口将接收到该消息,存储(接收到)其文本,并(通常)使该窗口的矩形无效。由于该窗口现在已失效,因此下一次机会是Windows,Windows会向您的窗口发送WM_PAINT消息(然后您将绘制出文本)。

答案 1 :(得分:0)

处理WM_PAINT消息并直接在窗口的HDC上绘制文本是一种选择。

另一种选择是在窗口中创建子项STATIC control,然后可以使用SetWindowText()WM_SETTEXT消息将所需的文本分配给该子项。无需手动绘制。

答案 2 :(得分:0)

最后,我弄清楚了如何完成此操作:

win32 app picture

    #ifndef UNICODE
#define UNICODE
#endif
using namespace std;

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


int X_Coordinate = 215;
int Y_Coordinate = 415;
int Width = 700;
int Height = 500;

char Text[] = {"abc123"};

char Window_Title[] = "My title";
char Window_Image[] = "D:\\bitmap1.bmp";


const char* csWindow_Title = Window_Title;
const char* csWindow_Image = Window_Image;


HBITMAP bitmap; // Creates bitmap object based on a handle to a Windows Windows Graphics Device Interface (GDI) bitmap and a handle to a GDI palette.


// Utilities
bool ConvertConstChartoLPWSTR (const char* as , wchar_t* wString  )
{
    memset(wString,0,sizeof(wString));
    MultiByteToWideChar(CP_ACP, 0, as, -1, wString, 4096);
    return wString;

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{


    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";

    WNDCLASS wc = { };

    //Registering the Window Class
    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255)); // set window background color ( RGB ) - white

    RegisterClass(&wc);  // register the window class with the operating system

    wchar_t* wWindow_Title=new wchar_t[4096];
    memset(wWindow_Title,0,sizeof(wWindow_Title)); // init variable
    ConvertConstChartoLPWSTR(csWindow_Title,wWindow_Title); // convert


    // Create the window.
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        wWindow_Title,                  // Window text
        WS_OVERLAPPEDWINDOW,           // Window style


        // Size and position
        X_Coordinate, Y_Coordinate, Width, Height,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );


    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

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

    return 0;
}


//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

// ----------------------------- START -> SWITCH case -----------------------------------------------------

    switch (uMsg)
    {

// ----------------------------- START -> case WM_DESTROY -------------------------------------------------
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
// ----------------------------- END -> case WM_DESTROY ---------------------------------------------------


// ----------------------------- START -> case WM_PAINT ---------------------------------------------------
    case WM_PAINT:
        {

            wchar_t* wWindow_Image=new wchar_t[4096];
            memset(wWindow_Image,0,sizeof(wWindow_Image));
            ConvertConstChartoLPWSTR(csWindow_Image,wWindow_Image); // convert

            bitmap=(HBITMAP)LoadImage(NULL,          // A handle to the module that contains the image to be loaded. To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp — set this parameter to NULL
                                      wWindow_Image, // The image to be loaded.
                                      IMAGE_BITMAP,  // The type of image to be loaded.
                                      690, // The width, in pixels, of the icon or cursor.
                                      540, // he height, in pixels, of the icon or cursor.
                                      LR_LOADFROMFILE); //Loads the stand-alone image from the file specified by lpszName (icon, cursor, or bitmap file).

            PAINTSTRUCT ps; // declare structure with information for an application
            HDC hdc = BeginPaint(hwnd, &ps);  // prepare the specified window for painting


            int index = sizeof(Text);


            HDC hMemDC=CreateCompatibleDC(hdc); // create a compatible DC ( hMemDC ) o be the same like another one ( hdc )
            ::SelectObject(hMemDC,bitmap); // Selects an object into the specified device context (DC). The new object replaces the previous object of the same type.

            long retval=SetTextAlign(hdc,TA_TOP); // allignment of written area


            const char* theval;
            int u = 5;

            for(int b = 0; b < sizeof(Text); b++)
             {
                string sym(1, Text[b]);   // convert char to const char*

                theval = sym.c_str();

                cout<<b<<theval;    

                wchar_t wtext[sizeof(Text)];
                memset(wtext,0,sizeof(wtext));
                ConvertConstChartoLPWSTR(theval,wtext); // convert


                // Here application is laid out.
                TextOut (hdc, 5, u, wtext, sizeof(Text)); 
                u = u + 15;
             }

                index = index + u; // claculate the size of written area

                BitBlt(  hdc,       // handler
                         0,         // The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
                         index,     // The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
                         700,       // The width, in logical units, of the source and destination rectangles.
                         980,       // The height, in logical units, of the source and the destination rectangles.
                         hMemDC,    // handler for source ( image ).
                         0,         // The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
                         0,         // The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
                         SRCCOPY ); // A raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.
                                    // SRCCOPY - Copies the source rectangle directly to the destination rectangle.


               EndPaint(hwnd, &ps); // function marks the end of painting in the specified window
        }
        return 0;

// ----------------------------- END -> case WM_PAINT ---------------------------------------------------


    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
// ----------------------------- END -> SWITCH case -----------------------------------------------------

} // END -> LRESULT CALLBACK WindowProc