Max Yari
Max Yari

Reputation: 3687

WinAPI nothing happens on button click

My Question is: In the below C++ code, why does clicking on the button do nothing while it is supposed to call MessageBox from WndProc1?

P.S: After compiling, I got some errors like the following:

"C:\Windows\SysWOW64\ntdll.dll", Can't find or open PDB file.

Code:

#include <Windows.h>

LRESULT CALLBACK WndProc(
  _In_  HWND hwnd,
  _In_  UINT uMsg,
  _In_  WPARAM wParam,
  _In_  LPARAM lParam
);

LONG WINAPI WndProc1(
  _In_  HWND hwnd_button,
  _In_  UINT uMsg,
  _In_  WPARAM wParam,
  _In_  LPARAM lParam
);

//Точка входа в программу
int WINAPI WinMain
    (
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//Создаем класс окна
WNDCLASS WindowClass;

//Заполняем структуру 
WindowClass.style       = 0;
WindowClass.lpfnWndProc     = (WNDPROC)WndProc;
WindowClass.cbClsExtra      = 0;
WindowClass.cbWndExtra      = 0;
WindowClass.hInstance       = hInstance;
WindowClass.hIcon       = LoadIcon(hInstance,
                  (LPCTSTR)IDI_APPLICATION);
WindowClass.hCursor     = LoadCursor(NULL, IDC_ARROW);
WindowClass.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
WindowClass.lpszMenuName    = 0;
WindowClass.lpszClassName   = TEXT("Class");

//Зарегистируем класс окна
RegisterClass(&WindowClass);

//Создаем переменную, в которой поместим идентификатор окна
HWND hWnd;

hWnd = CreateWindow(TEXT("Class"), TEXT("ClickTest"),
       WS_OVERLAPPEDWINDOW, 0, 0, 500, 300, NULL, NULL, hInstance, NULL); 

//Создаем кнопку
HWND hWnd_button;

hWnd_button = CreateWindow(TEXT("button"), TEXT("Click me"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 10, 80, 30, hWnd, (HMENU)10000, hInstance, NULL);

//показать окно
ShowWindow(hWnd, nCmdShow);

//обновить содержимое окна
UpdateWindow(hWnd);

//Создадим переменную для храненния сообщений
MSG msg;

//Создадим цикл обработки сообщений
while(GetMessage(&msg, NULL,0 ,0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return 0;
}

LONG WINAPI WndProc1(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch (Message){
case WM_COMMAND:
    if(LOWORD(wparam)==10000)
    {
        MessageBox(hwnd, TEXT("Button Pressed"), TEXT(""), 0);
    }
    return 0;}}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_CREATE:
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
case WM_PAINT:
    BeginPaint(hWnd, &PS);
    EndPaint(hWnd, &PS);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Now working, just added button function as one of the cases to WndProc (WndProc1 deleted)

  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wparam, LPARAM lParam)
 {
 PAINTSTRUCT PS;
 switch(message)
 {
 case WM_CREATE:
    break;
 case WM_DESTROY:
    PostQuitMessage(0);
    break;
case WM_PAINT:
    BeginPaint(hWnd, &PS);
    EndPaint(hWnd, &PS);
    break;
case WM_COMMAND:
    if(LOWORD(wparam)==10000)
    {
        MessageBox(hWnd, TEXT("Button Pressed"), TEXT(""), 0);
    }
default:
    return DefWindowProc(hWnd, message, wparam, lParam);
}
return 0; 
  }

One final newbie question: what's the difference between LRESULT CALLBACK and LONG WINAPI then?

Upvotes: 2

Views: 9216

Answers (2)

masoud
masoud

Reputation: 56479

Do this modification on WndProc:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_COMMAND:
    if(LOWORD(wParam)==10000)
    {
        MessageBox(hWnd, TEXT("Button Pressed"), TEXT(""), 0);
    }
    break;
case WM_CREATE:
    break;
// ...

I added WM_COMMAND in the switch/case of WndProc.

Upvotes: 5

Matteo Italia
Matteo Italia

Reputation: 126777

How do you expect WndProc1 to be called? It isn't associated to any window class... You have to handle the WM_COMMAND inside WndProc (buttons, as well as other common controls, notify their parent of their events via WM_COMMAND).

Upvotes: 3

Related Questions