简体   繁体   中英

Message after clicking on the edit control

I'm new to WinAPI development.

The main window contains three Edit controls.When clicking with the left mouse button on Edit1, the message "Edit1 choosed" is to be displayed. When you click on Edit2, it is supposed to display the message "Edit2 choosed". Similarly, when you click on Edit3, the message "Edit3 choosed" should be displayed. I tried using various combinations of WM_LBUTTONDOWN and WM_SETFOCUS, unfortunately the program does not work. Could someone please help me?

#include <windows.h>

#define Edit1 501
#define Edit2 502
#define Edit3 503

HWND TextBox1, TextBox2, TextBox3;
HFONT HF  = CreateFont (30, 0, 00, 00, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, 
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

switch(Message) 
{
    case WM_SETFOCUS:
            
        if(wParam == Edit1)
        {
            MessageBox(NULL, "Edit1 choosed","Edit1",MB_ICONEXCLAMATION|MB_OK);
        }
        break;          
        if(wParam == Edit2)
        {
            MessageBox(NULL, "Edit2 choosed","Edit2",MB_ICONEXCLAMATION|MB_OK);
        }
        break;
        if(wParam == Edit3)
        {
            MessageBox(NULL, "Edit3 choosed","Edit3",MB_ICONEXCLAMATION|MB_OK);
        }
        break;
    case WM_CREATE:
    {
        TextBox1 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT,
        30, 70, 190, 40, hwnd, (HMENU)Edit1, GetModuleHandle(NULL), NULL); 
        SendMessage(TextBox1, WM_SETFONT,( WPARAM ) HF, 0 );

        TextBox2 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT,
        30, 130, 190, 40, hwnd, (HMENU)Edit2, GetModuleHandle(NULL), NULL); 
        SendMessage(TextBox2, WM_SETFONT,( WPARAM ) HF, 0 );
        
        TextBox3 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT,
        30, 190, 190, 40, hwnd, (HMENU)Edit3, GetModuleHandle(NULL), NULL); 
        SendMessage(TextBox3, WM_SETFONT,( WPARAM ) HF, 0 );
                
        break;      
    }
    case WM_DESTROY: 
    {
        PostQuitMessage(0);
        break;
    }

    default:
        return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}

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

memset(&wc,0,sizeof(wc));
wc.cbSize        = sizeof(WNDCLASSEX);
wc.lpfnWndProc   = WndProc; 
wc.hInstance     = hInstance;
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); 
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,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL,NULL,hInstance,NULL);

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

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

the "break" in the message loop under WM_SETFOCUS is always executed. So the program never reaches the point of the second edit control. Move the break to before the closing "}" and retry.

{
MessageBox(NULL, "Edit1 choosed","Edit1",MB_ICONEXCLAMATION|MB_OK);
break; 
}

Furthermore, consider using 'else if'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM