Go Premium for a chance to win a PS4. Enter to Win

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

Enable other programs button/menu from your own program.

Hi, I'm trying to make a Button Enabler in Delphi, I need some help on how I could enable/disable button's and menu's in other programs.

could anyone show me how I could do this.

thxs
0
SaLz
Asked:
SaLz
  • 8
  • 5
1 Solution
 
DaFoxCommented:
Hi,

that depends on what buttons you want to disable, e.g. this sample disables the close button close in notepad's caption bar and system menu.

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow('notepad', nil);
  menu := GetSystemMenu(wnd, false);
  if (menu <> 0) then EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
end;

But this doesn't work for normal buttons or toolbuttons, of course.

Markus
0
 
SaLzAuthor Commented:
how would I be able to enable a menu in another program like

File|View|Grayed|Help

how could I get the menu ungrayed and enabled so I can view the dropdown menu
0
 
DaFoxCommented:
Hi SaLz,

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow('notepad', nil);
  menu := GetMenu(wnd);
  // uIDEnableItem = 2, since "grayed" is the third entry in the menu (zero-based)
  if (menu <> 0) then EnableMenuItem(menu, 2, MF_BYPOSITION or MF_GRAYED);
  // this would gray the first item in "grayed" menu
  if (menu <> 0) then EnableMenuItem(GetSubMenu(menu, 2), 0, MF_BYPOSITION or MF_GRAYED);
end;

To enable the items simply pass MF_ENABLED as param instead of MF_GRAYED. You may also want to take a look at ModifyMenu() API.

Markus
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SaLzAuthor Commented:
wnd := FindWindow('notepad', nil);
how would I do it for another program?

0
 
DaFoxCommented:
From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp:

HWND FindWindow(
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName
);

You either have to know the classname or the windowname of your target application. If you don't know it, try to get it with Spy++ or any other aquivalent tool (e.g. www.x-spy.net [it's freeware]).

So,

wnd := FindWindow('anotherClassName', nil);
wnd := FindWindow(nil, 'anotherWindowTitle');
wnd := FindWindow('anotherClassName', 'anotherWindowTitle');

would work as long as the classname or/and windowname is correct.

Markus
0
 
SaLzAuthor Commented:
ok am working on my form I've added a "MainMenu1" with these

File | View | Grayed | Help

The Window Class is "TForm1"
The Window Title is "Form1"

I've put this in.

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow(PChar(Edit2.text), PChar(Edit1.text));
  menu := GetMenu(wnd);
  if (menu <> 0) then EnableMenuItem(menu, 2, MF_BYPOSITION or MF_ENABLED);
  if (menu <> 0) then EnableMenuItem(GetSubMenu(menu, 2), 0, MF_BYPOSITION or MF_ENABLED);
end;

I've made a program just now that gets the Window Title & Class name when I go over it with my mouse, I've put the Interval to 2 seconds so it gives me time to press Enable or Disable, I've tryed it on nodepad, wordpad, windows media player it works and on my form and it works.

it doesn't work on Internet Explorer, folders, Outlook Express or DC++ what could be the problem?

Class's
Internet Exploer = IEFrame
Windows Media Player = WMP Skin Host
Outlook Express = Outlook Express Browser Class
DC++ = DC++

what am I doing wrong here? I've put in the class, I've put in the title, but in some programs it doesn't work.
0
 
DaFoxCommented:
>> Internet Exploer,Windows Media Player, Outlook Express, DC++

They're all working with Toolbars instead of normal main menus. Take a look at the child windows of e.g. Internet Explorer (WorkerW -> RebarWindow32 -> ...). As I said before, you have to adjust your code on the design of your target application. If it uses a normal main menu like notepad it's easy. If it uses toolbars it gets more complicated. And last not least you always have to know the classname (and classnames of the childwindows). For MS products they vary with different versions.

Markus
0
 
SaLzAuthor Commented:
ok, thxs
0
 
SaLzAuthor Commented:
I have another question on this, once I have enabled the menu now do I enable the submenu if they are disabled as well in form1
0
 
SaLzAuthor Commented:
like
File | View | Grayed | Help
                 Menu 1
                 Menu 1
                 Grayed
                 Menu 1
                 Grayed

How could I get the drop down menu enabled as above? so its not just 1 there all enabled



0
 
SaLzAuthor Commented:
nm, I've sorted it :) I luv ya :)
0
 
SaLzAuthor Commented:
Would you know how to enable Buttons? in the same manner?

asume that I have the class and title how would I do this?

I have made a program that will get the class and title just how to enable a button or other things like that?
0
 
DaFoxCommented:
Assuming you are talking about "normal" buttons, just use EnableWindow() API.

Markus
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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