Solved

Autocomplete combobox(ex)  using Win32 API in C

Posted on 2002-04-19
11
2,163 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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