C语言 如何处理win32 API中的点击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2522029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:57:14  来源:igfitidea点击:

how to handle click event in win32 API?

capiwinapi

提问by kiddo

I have created a simple win 32 application..in which it has a textbox and a button in a dialog window..first when I created this..it didnt display the dialog window and then what I did is added the code below to handle the close(WM_CLOSE) of the dialog window...but I want to know, how to handle the button click event..

我创建了一个简单的 win 32 应用程序..其中它有一个文本框和一个对话框窗口中的按钮..首先当我创建这个..它没有显示对话框窗口然后我所做的是添加下面的代码来处理对话框窗口的关闭(WM_CLOSE)......但我想知道,如何处理按钮点击事件..

  void ValidatePassword(CString encryptedPassword)
{
    //create password dialog window
    CreateEvent(NULL,true,false,L"TestEvent");
    MSG msg;
    HWND hwnd = CreateWindowEx(0,WC_DIALOG,L"Security Alert",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                    600,300,300,200,NULL,NULL,NULL,NULL);

    //create label
    CreateWindowEx(NULL,L"Static",L"Requires Password to Run the File:", WS_CHILD|WS_VISIBLE,
                    10,25,300,20,hwnd,(HMENU)label_id,NULL,NULL);

    //create textboxcontrol within the dialog
    CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",L"",WS_CHILD|WS_VISIBLE | ES_PASSWORD,
                    10,50,125,25,hwnd,(HMENU)textbox_id,NULL,NULL);
    //create button
    HWND button = CreateWindowEx(WS_EX_CLIENTEDGE,L"Button",L"OK",WS_CHILD|WS_VISIBLE,
                    10,100,100,25,hwnd,(HMENU)button_id,NULL,NULL);

    ShowWindow (hwnd, SW_SHOW);
    UpdateWindow(hwnd);
    //SetWindowLong(button,DWL_DLGPROC, (long)myProc);

    while(GetMessage(&msg,NULL,0,0))
    {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }




}

LRESULT WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{


    HWND hwndButton;
    switch (message)
    { 
        /* Handles all Windows Messages */
        case WM_COMMAND:

            {
              if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED))
              {
                int iMID;
                iMID = LOWORD(wParam);
                switch(iMID)
                {
                  case button_id:
                      {
                       MessageBox(hwnd, (LPCTSTR)"You just pushed me!",  (LPCTSTR) "My Program!", MB_OK|MB_ICONEXCLAMATION);
                       break;
                       }
                  default:
                       break;
                }
              }
              break;
            }
        case WM_DESTROY:
            {
              PostQuitMessage (0);       /* send a WM_QUIT to Message Queue, to shut off program */
              break;
             }
    }

    return 0; 
}

回答by Chris Becke

Yikes.

哎呀。

It should not be necessary to call SetWindowLong to set the dialog proc for a dialog. Your "simple" program should look something like

不需要调用 SetWindowLong 来设置对话框的对话框过程。你的“简单”程序应该看起来像

#include <windows.h>
#include "resource.h"

BOOL CALLBACK myProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch(uMsg)
  {
  case WM_INITDIALOG:
    return TRUE;
  case WM_COMMAND:
    if( LOWORD(wParam) == IDCLOSE) // close button click
      EndDialog(hwnd,0);
    return TRUE;
  }
  return FALSE;
}

int CALLBACK WinMain(HINSTANCE hExe,HINSTANCE,LPCSTR,INT)
{
  return DialogBox(hExe,MAKEINTRESOURCE(IDD_DIALOG),NULL,myProc);
}

回答by Blindy

Check for WM_COMMAND. LOWORD(wParam)will be your control ID and lParamwill be your hWnd for the button.

检查WM_COMMANDLOWORD(wParam)将是您的控件 ID,lParam并将是您的按钮 hWnd。