Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2378
  • Last Modified:

Autocomplete combobox(ex) using Win32 API in C

Using plain Win32 API in C, how do I capture the keyboard input within a control(edit,combobox,etc.) on a dialog-based app?

As example, how should I do autocomplete comboboxes in a dialog-based Win32 API app?

Additional 150 points will be added for best suitable answer for autocomplete combobox (and comboboxex) using simple Win32 API in C (code, explanation, etc.). Or in non-MFC C++.  

Thanks in advanced!
Roland
0
RolandAZ
Asked:
RolandAZ
  • 5
  • 5
1 Solution
 
Roshan DavisCommented:
If u r looking for a VB code, just llok in to this
http://www.vbthunder.com/source/combobox/autocomplete.htm

GOOD LUCK
0
 
PavlikCommented:
One possible way is to subclass every control, created in your app. In this case you will receive all the messages addressed to that controls. But it's not trivial task.
0
 
PavlikCommented:
Sorry. My note is valid only if you can't modify and recompile that app. Otherwise it is just excersise for windows messages processing.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
RolandAZAuthor Commented:
Thanks for comments, Roshmon and Pavlik.

Roshmon,
The VB code will surely be used in a VB app. What I need though is code in C Win32 API. Or in non-MFC C++ code/sample.

Pavlik,
Can you give example code in C or non-MFC C++? 150 points will be added to the question if I can use that C, or/and non-MFC C++ code.

Thanks again, guys!

P.S. Question updated to include the programming language ,C/C++, required.



 
0
 
PavlikCommented:
I'm kind of busy now. I'll try to write some example on Thursday.
0
 
RolandAZAuthor Commented:
Thanks Pavlik...

0
 
PavlikCommented:
Ok. Finaly i did it.
Sorry for the late answer.

The idea is to use SetWindowsHookEx() function with WH_SYSMSGFILTER. It will give you ability to catch every message, sent to controls like edit or combobox.
My sample consists of 3 files:
EditHook.h - functions that will be exported from DLL.
EditHook.cpp - Implementation of DLL.
Slon1.cpp - simple console app which loads DLL and calls functions from it.


EditHook.h
===========================================
#ifdef EDITHOOK_EXPORTS
#define EDITHOOK_API __declspec(dllexport)
#else
#define EDITHOOK_API __declspec(dllimport)
#endif

EDITHOOK_API void SetEditHook();
EDITHOOK_API void CancelEditHook();
-------------------------------------------
EditHook.cpp
===========================================
#include <windows.h>
#include "EditHook.h"

HMODULE hm;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{
  switch (ul_reason_for_call)
  {
    case DLL_PROCESS_ATTACH:
      hm = (HMODULE)hModule;
      break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
      break;
  }
  return TRUE;
}

HHOOK hHook;

LRESULT CALLBACK MySysMsgProc(
  int nCode,      // message flag
  WPARAM wParam,  // not used
  LPARAM lParam   // message data
)
{
  MSG* pMsg = (MSG*)lParam;
  if (nCode == MSGF_DIALOGBOX)
  {
    char className[256];
    GetClassName(pMsg->hwnd, className, 255);
    if (strncmp(className, "Edit",4) == 0)
    {
      char aaa[4];
      SendMessage(pMsg->hwnd, WM_GETTEXT, 4, (LPARAM)aaa);
      if (strncmp(aaa, "aaa", 3) == 0)
        SendMessage(pMsg->hwnd, WM_SETTEXT, 0, (LPARAM)"bbb");
    }
  }
  return CallNextHookEx(hHook, nCode, wParam, lParam);
}

EDITHOOK_API void SetEditHook()
{
  hHook = SetWindowsHookEx(WH_SYSMSGFILTER, MySysMsgProc, hm, 0);
}

EDITHOOK_API void CancelEditHook()
{
  UnhookWindowsHookEx(hHook);
}
-------------------------------------------
Slon1.cpp
===========================================
#include <windows.h>
#include "EditHook.h"

int main(int argc, char* argv[])
{
  SetEditHook();
  for (int i = 0; i < 100; i++)
    Sleep(1000);
  CancelEditHook();
  return 0;
}
-------------------------------------------

So EditHook.cpp should be compiled to DLL.
Slon1.cpp should be compiled to console app. And EditHook.lib should be added to it.

After you have both of them simply run Slon1.exe and during its execution start any application with edit box. And try to enter "aaa" (without quotes) in that edit box. If they will change to "bbb" then your hook is working.

Good luck.

Pavlik.
0
 
RolandAZAuthor Commented:
Thanks for great sample, Pavlik!
It works great...
But I'm looking for autocomplete comboboxex
I should give you some points though, i don't know how to do that though. If you know how I'll give 100 points to you.

I'm adding another 50 points ...

I'm willing to give 300 points for the best suitable answer.

Thanks again

Roland




0
 
RolandAZAuthor Commented:
All Experts,
How do I give 100 points to Pavlik but still keep the question open?

Thanks again

Roland
0
 
PavlikCommented:
Well actually this sample just shows you the way how to catch messages to any edit controls. Once you know how to do this you can catch messages to other controls (like comboboxes or buttons for example).

In your particular case all you need is to get the parent of edit control and check if it is ComboBox. And if it is so, do whatever you want.

In the example from my previous message you should replace MySysMsgProc with the code like this:
===========================================
LRESULT CALLBACK MySysMsgProc(
 int nCode,      // message flag
 WPARAM wParam,  // not used
 LPARAM lParam   // message data
)
{
 MSG* pMsg = (MSG*)lParam;
 if (nCode == MSGF_DIALOGBOX)
 {
   static const char EditName[] = "Edit";
   char className[256];
   GetClassName(pMsg->hwnd, className, sizeof(EditName));
   if (strncmp(className, EditName, sizeof(EditName)) == 0)
   {
     HWND hParent = GetParent(pMsg->hwnd);
     if (hParent != 0)
     {
       static const char ComboName[] = "ComboBox";
       GetClassName(hParent, className, sizeof(ComboName));
       if (strncmp(className, ComboName, sizeof(ComboName)) == 0)
       {
         char aaa[4];
         SendMessage(pMsg->hwnd, WM_GETTEXT, 4, (LPARAM)aaa);
         if (strncmp(aaa, "aaa", 3) == 0)
         {
           SendMessage(pMsg->hwnd, WM_SETTEXT, 0, (LPARAM)"aaabbb");
           SendMessage(pMsg->hwnd, EM_SETSEL, 3, -1);
         }
       }
     }
   }
 }
 return CallNextHookEx(hHook, nCode, wParam, lParam);
}
===========================================

This code won't work in completely all the cases. To write the final code you should know more about apps you are going to hook. Play with Spy++, look at the windows structure for your app, at the messages your comboboxes receive. Check the docs about SetWindowsHookEx() function. This will help you to find the right way of achieving your goal.

If you won't manage to control comboboxes in some app, just let me know. In this case i'll need to look at that app.

Good Luck.
0
 
RolandAZAuthor Commented:
Thanks, again, Pavlik!

You deserve the points.

Roland
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now