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

问题描述 投票:-1回答:2

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

我成功完成的只是改变窗口的标题而不是我想要的。我想在窗口框中添加一些文本行。

SendMessage功能对我不起作用。

如果有人有这方面的提示告诉我!

#include <windows.h>

const char g_szClassName[] = "myWindowClass";
//The Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Title of window",
        WS_OVERLAPPEDWINDOW,
        1390, 540, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);



    // The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

enter image description here

c++ winapi window
2个回答
1
投票

要在客户区域中绘制文本,您的wndProc通常会使用类似DrawTextTextOut的内容。你通常这样做是为了回应WM_PAINT

为了能够响应外部消息,您通常会发送包含该文本的消息。窗口将接收该窗口,存储(收到)其收到的文本(并且通常)使窗口的矩形无效。由于窗口现在无效,下一次获得的机会,Windows将向您的窗口发送一个WM_PAINT消息(然后您将绘制文本)。


0
投票

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

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


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
© www.soinside.com 2019 - 2024. All rights reserved.