Solved

Autocomplete combobox(ex)  using Win32 API in C

Posted on 2002-04-19
11
2,044 Views
Last Modified: 2013-12-03
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
Comment
Question by:RolandAZ
  • 5
  • 5
11 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6953376
If u r looking for a VB code, just llok in to this
http://www.vbthunder.com/source/combobox/autocomplete.htm

GOOD LUCK
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955106
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
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955109
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
 
LVL 1

Author Comment

by:RolandAZ
ID: 6955745
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
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960631
I'm kind of busy now. I'll try to write some example on Thursday.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:RolandAZ
ID: 6974871
Thanks Pavlik...

0
 
LVL 1

Expert Comment

by:Pavlik
ID: 7000147
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
 
LVL 1

Author Comment

by:RolandAZ
ID: 7006227
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
 
LVL 1

Author Comment

by:RolandAZ
ID: 7006242
All Experts,
How do I give 100 points to Pavlik but still keep the question open?

Thanks again

Roland
0
 
LVL 1

Accepted Solution

by:
Pavlik earned 200 total points
ID: 7006711
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
 
LVL 1

Author Comment

by:RolandAZ
ID: 7031511
Thanks, again, Pavlik!

You deserve the points.

Roland
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now