单击按钮时WinAPI没有任何反应

时间:2013-03-10 21:50:03

标签: c++ winapi visual-c++ button

我的问题是:在下面的C ++代码中,为什么在MessageBoxWndProc1调用"C:\Windows\SysWOW64\ntdll.dll", Can't find or open PDB file. 时,点按该按钮什么都不做?

P.S:编译完成后,出现了以下错误:

#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;
}

代码:

WndProc

现在正常工作,只需将按钮功能作为其中一个案例添加到WndProc1 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; } 已删除)

LRESULT CALLBACK

最后一个新手问题:LONG WINAPI和{{1}}之间的区别是什么?

2 个答案:

答案 0 :(得分:4)

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;
// ...

我在WM_COMMAND的{​​{1}} / switch添加了case

答案 1 :(得分:3)

您期望如何调用WndProc1?它与任何窗口类都没有关联...你必须处理WndProc内的WM_COMMAND(按钮,以及其他常用控件,通过{{通知他们的他们的事件1}})。