Solved

Autocomplete combobox(ex)  using Win32 API in C

Posted on 2002-04-19
11
2,188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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