【问题标题】:How can I add some text line in my window?如何在我的窗口中添加一些文本行?
【发布时间】:2023-03-20 08:28:01
【问题描述】:

我可以创建一个带有 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;
}

【问题讨论】:

标签: c++ winapi window


【解决方案1】:

要在客户区绘制文本,您的wndProc 通常会使用DrawTextTextOut 之类的内容。您通常会这样做以响应 WM_PAINT

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      最后我想出了如何完成这个:

      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); // alignment 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 2020-06-19
        • 1970-01-01
        相关资源
        最近更新 更多