2

我想在我的主窗口中创建一个嵌入的小框,它可以接受文本输入并包含按钮。当我说嵌入式时,我的意思是我希望它无缝集成到我的主窗口中,而没有它自己的关闭按钮。我无休止地搜索谷歌,并没有找到不涉及mfc或.net的答案。如何在原始 win32 应用程序中执行此操作。请将您的代码添加到我的骨架中,如下所示。谢谢!

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("App") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("App"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;

     switch (message)
     {
     case WM_CREATE:

          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;



          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
4

3 回答 3

6

您的问题不是很清楚,但是您在 Win32 中动态创建文本框和按钮的方式是使用该CreateWindow功能。Win32 为控件注册了特殊类,您可以使用它们作为第一个参数传递给CreateWindow.

要创建一个文本框,调用是 -

CreateWindow("EDIT", 0, WS_BORDER|WS_CHILD|WS_VISIBLE, 56, 10, 50, 18, g_hWnd, 0, hInst, 0);

创建一个按钮 -

CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, 0, hInst, 0);

您必须指定WS_CHILDWS_VISIBLE样式,还必须提供主窗口的句柄作为其父窗口。

于 2012-07-08T03:30:42.193 回答
4

你想收到什么消息?

例如,按下按钮时会发送 WM_COMMAND 消息。按如下方式处理这些消息:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc ;
    PAINTSTRUCT ps;
    RECT        rect ;

    switch (message)
        {
        case WM_CREATE:
            return 0 ;

        case WM_COMMAND:
            {
            switch(LOWORD(wParam))
                {
                case IDC_BUTTON:
                    /*
                    IDC_BUTTON is id given to button
                    This id is specified while creating window. If you are using
                    CreateWindow then it will be as follows:

                    CreateWindow("BUTTON", 0, WS_CHILD|WS_VISIBLE, 70, 70, 80, 25, g_hWnd, (HMENU)IDC_BUTTON, hInst, 0);

                    and in resource.rc file or in the beginning of code. Do "#define IDC_BUTTON                  3456"
                    3456 is just a reference you can give any other no as well. Prefer using bigger number so that they can not conflict with existing ones.
                    */

                    //do whatever you want to do here when button is pressed
                    break
                }
            }
            break;

        case WM_PAINT:
            hdc = BeginPaint (hwnd, &ps) ;

            EndPaint (hwnd, &ps) ;
            return 0 ;

        case WM_DESTROY:
            PostQuitMessage (0) ;
            return 0 ;
        }

    return DefWindowProc (hwnd, message, wParam, lParam) ;
}
于 2012-07-09T12:08:32.260 回答
0

我认为Edit Control可能会满足您的需求。该链接指向 Microsoft 有关编辑控件的文档。

An edit control is a rectangular control window typically used in a dialog box to permit the user to enter and edit text by typing on the keyboard.

于 2021-09-25T03:20:09.643 回答