Home>

I am currently studying windows api.
What I want to do is

1, Press the registration button
2, text edit comes out
3, enter your weight
4, Press the draw button
5, Recorded weight is displayed in parent window (current weight is 00)

I'm trying to make something as simple as

. Up to 4 on my own. However, when trying to implement 5, it is difficult to register the value in the variable because of the type difference.

We were able to display the recorded weight in the dialog box. However, it cannot be displayed in the parent window.

I don't need to answer the question, so I'd like to ask for advice like doing something like this.

The environment is the latest version of VisualStudio

// source.cpp
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<tchar.h>
#include "resource.h"

#define BUTTON_ID1 0
// global variables:
TCHAR szBuf_edit1 [1024];
// Forward the declaration of the function contained in this code module:
LRESULT CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
char edit_str [256];

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    WNDCLASS winc;
    winc.style = CS_HREDRAW | CS_VREDRAW;
    winc.lpfnWndProc = WndProc;
    winc.cbClsExtra = winc.cbWndExtra = 0;
    winc.hInstance = hInstance;
    winc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    winc.hCursor = LoadCursor (NULL, IDC_ARROW);
    winc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    winc.lpszMenuName = NULL;
    winc.lpszClassName = TEXT ("KITTY");
    if (! RegisterClass (&winc)) return -1;
    hwnd = CreateWindow (
        TEXT ("KITTY"), TEXT ("weight management app"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL
    );
    CreateWindow (
        TEXT ("BUTTON"), TEXT ("Record"),
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        0, 0, 100, 50,
        hwnd, (HMENU) BUTTON_ID1, hInstance, NULL
    );
    if (hwnd == NULL) return -1;
    while (GetMessage (&msg, NULL, 0, 0)) DispatchMessage (&msg);
    return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
        switch (msg) {
    case WM_DESTROY:
        PostQuitMessage (0);
        return 0;
    case WM_COMMAND:
        switch (LOWORD (wp)) {
        case BUTTON_ID1:
            DialogBox ((HINSTANCE) GetWindowLong (hwnd, GWLP_HINSTANCE), "DIALOG_BOX", hwnd, (DLGPROC) DialogProc);
        }
        return 0;

    }
    return DefWindowProc (hwnd, msg, wp, lp);
}
LRESULT CALLBACK DialogProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
    switch (msg) {
    case WM_CLOSE:
        EndDialog (hwnd, IDOK);
        break;
    case WM_COMMAND:
        switch (LOWORD (wp)) {
        case IDC_BUTTON1:
            GetDlgItemText (hwnd, IDC_EDIT1,
                (LPTSTR) szBuf_edit1, (int) sizeof (szBuf_edit1));
            SetDlgItemText (hwnd, IDC_STATIC1, (LPCTSTR) szBuf_edit1);
        }
        break;
    }
    return FALSE;
}


//resource.rc

DIALOG_BOX DIALOG 0, 0, 170, 62
FONT 9, "MS UI Gothic"
CAPTION "Dialog"
BEGIN
EDITTEXT IDC_EDIT1, 2, 2, 166, 13, ES_AUTOHSCROLL
PUSHBUTTON "description", IDC_BUTTON1, 132, 17, 26, 13
LTEXT "", IDC_STATIC1, 4, 40, 114, 8
END


resource.h

define IDC_EDIT1 1001
define IDC_BUTTON1 1002
define IDC_STATIC1 1003
ifndef IDC_STATIC
define IDC_STATIC -1

endif

`` `

  • Answer # 1

    It is probably said that "it doesn't work because of the type difference", probably because of the mixedTCHAR szBuf_edit1 []andchartypes, and Unicode characters in the project settings I thought it was pointing to things that were confusing about usingGetDlgItemTextandSetDlgItemTextfor building sets and multi-byte character sets. The Windows API has APIs that are separated for Unicode characters (wide characters) and Ansi characters (@multibyte characters). So isGetDlgItemText, which is divided intoGetDlgItemTextWandGetDlgItemTextA.GetDlgItemTextis actually a macro, which is automatically converted to the respective API when building a multi-byte character set in a Unicode build.

    If the purpose of this question is simply to say "I can't convert well", modify it like the following code and useWndProcandDlgProcTCHAR szBuf_edit1 []can be shared. (Please refer to the comment of "NOTE:" in the code)

    # include
    #include
    #include
    #include
    #include
    #include "resource.h"
    #define BUTTON_ID1 0
    // global variables:
    // NOTE1: Initialize with '\ 0' just in case
    TCHAR szBuf_edit1 [1024] = {_T ('\ 0')};
    LRESULT CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    // NOTE2: I will not use it, so delete it
    // char edit_str [256];
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        PSTR lpCmdLine, int nCmdShow)
    {
        HWND hwnd;
        MSG msg;
        WNDCLASS winc;
        winc.style = CS_HREDRAW | CS_VREDRAW;
        winc.lpfnWndProc = WndProc;
        winc.cbClsExtra = winc.cbWndExtra = 0;
        winc.hInstance = hInstance;
        winc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        winc.hCursor = LoadCursor (NULL, IDC_ARROW);
        winc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
        winc.lpszMenuName = NULL;
        winc.lpszClassName = TEXT ("KITTY");翻译不全翻译不全