Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle click event in win32 API?

Tags:

c

winapi

api

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

  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; 
}
like image 403
kiddo Avatar asked Mar 26 '10 08:03

kiddo


People also ask

How to Create button click event in c++?

If you are adding the button to a parent window that you do not own, you can subclass the parent window using SetWindowsLongPtr(GWL_WNDPROC) or SetWindowSubClass() , and then you can handle messages that are sent to it, such as BN_CLICKED .

What is Win32 API used for?

Alternatively referred to as the Windows API and WinAPI, Win32 is the main set of Microsoft Windows APIs used for developing 32-bit applications. These APIs are responsible for functions in the following categories: Administration and Management - Install, configure, and service applications or systems.

What is Win32 API call?

Remarks# WinAPI (also known as Win32; officially called the Microsoft Windows API) is an application programming interface written in C by Microsoft to allow access to Windows features. The main components of the WinAPI are: WinBase: The kernel functions, CreateFile, CreateProcess, etc.


2 Answers

Yikes.

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

#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);
}
like image 94
Chris Becke Avatar answered Oct 16 '22 03:10

Chris Becke


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

like image 39
Blindy Avatar answered Oct 16 '22 04:10

Blindy