473,698 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Manipulating win32 app menus from c# using pinvoke...

Hi,

I am trying to dynamically add menu items to external win32 applications
from c# code. I have found some VB6 examples on the net that do just that,
but when I rewrite them into c# they will either do nothing, add a separator
or add an empty menu item which does not trigger commands when clicked.

The difference between a separator or an empty item getting inserted lies in
supplying the fMask of the MENUITEMINFO with the MF_OWNERDRAW constant from
winuser.h

Does anyone have experience with this kind of thing?

Thanks in advance!

Here is the code to reproduce the situation, it starts up notepad and adds
an extra menu item at the bottom of the file menu:
(I only included win api constants which I thought are relevant to the issue
at hand so this message would not be unnecessarily long.)
----8<---------------------------------------

using System;
using System.Diagnost ics;
using System.Runtime. InteropServices ;

namespace Example
{
[StructLayout(La youtKind.Sequen tial)]
internal struct MENUITEMINFO
{
public uint cbSize;
public uint fMask;
public uint fType;
public uint fState;
public uint wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public string dwTypeData;
public IntPtr dwItemData;
public uint cch;
public IntPtr hbmpItem;
}

/// <summary>
/// Summary description for Example.
/// </summary>
public class Example
{

const string PATH_TO_NOTEPAD = @"C:\WINNT\syst em32\notepad.ex e";

[DllImport("user 32.dll")]
private static extern IntPtr GetMenu(IntPtr hWnd);

[DllImport("user 32.dll")]
private static extern IntPtr GetSubMenu(IntP tr hMenu, int nPos);

[DllImport("user 32.dll")]
private static extern int GetMenuItemCoun t(IntPtr hMenu);

[DllImport("user 32.dll")]
private static extern bool InsertMenuItem( IntPtr hMenu, uint uItem, bool
fByPosition, [In] ref MENUITEMINFO lpmii);

[DllImport("user 32.dll")]
private static extern bool DrawMenuBar(Int Ptr hWnd);

internal const UInt32 MIIM_FTYPE = 0x00000100;

internal const UInt32 MF_STRING = 0x00000000;
internal const UInt32 MF_OWNERDRAW = 0x00000100;

public static void Main(string[] args)
{
Process process = Process.Start(P ATH_TO_NOTEPAD) ;
IntPtr handle = process.MainWin dowHandle;

IntPtr mainMenu = GetMenu(handle) ;
IntPtr fileMenu = GetSubMenu(main Menu, 0);

MENUITEMINFO myMenuItem = new MENUITEMINFO();
myMenuItem.cbSi ze = (uint) Marshal.SizeOf( myMenuItem);
myMenuItem.fMas k = MIIM_FTYPE;
myMenuItem.fTyp e = MF_STRING;
myMenuItem.dwTy peData = "myName";
myMenuItem.cch = (uint) myMenuItem.dwTy peData.Length;

uint itemIndex = (uint) GetMenuItemCoun t(fileMenu) + 1;
InsertMenuItem( fileMenu, itemIndex, true, ref myMenuItem);
DrawMenuBar(han dle);
}
}
}


Nov 17 '05 #1
0 2053

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2513
by: nbotw | last post by:
Hi, I need a function call in my xml web service. I have an old win32 dll and i have created a class to have my declaration in. Imports System.Runtime.InteropServices Public Class Win32
0
2126
by: Veli-Pekka Tätilä | last post by:
Hi, My first post here. I've found some serious accessibility flaws in the Python 2.4 docs and wish they could be rectified over time. I'm very new to Python and initially contacted docs at python org, However, I haven't gotten a reply for a week or so, and figured out I could post here for a larger audience, then. Original message follows: Hello,
2
1904
by: bayX | last post by:
I want to call Win32 Api from C# ? How do i make it? I have seen public static extern int SendMessage( IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam );
5
5892
by: Markus Wildgruber | last post by:
Hi! Is there a substitute in the .NET Framework for the GetKeyState(...) function? I want to check whether the shift key is pressed. What's the best way to do this? Do I have to use GetKeyState(...) using PInvoke? TIA, Markus
6
1559
by: _R | last post by:
I've had to write a lot of code to interface C# to older Win32 DLLs. Basically, an unmanaged C++ class talks directly to the Win32 DLL. A managed C++ class encloses the unmanaged C++ class. C# talks to the managed C++ class. Lots of work. Is this simplified in C++/CLI? Any sample code anywhere?
2
1647
by: Bo Gusman | last post by:
Hi. I'm porting a 250KLOC VB6 app to .Net this coming year. In order to overcome some of the deficiencies in various version of VB (the app originally began its life as a Quick Basic app and has gone through all iterations of VB from 3 onward!), especially related to drawing, the original authors (one of whom is my boss) made extensive use of the Win32 API. Has anyone else done such a porting job and can provide me with lessons
0
1380
by: David | last post by:
I've just recently read several articles that state that the only way to write "sparse zeros" to an NTFS5 sparse file is to use Win32 DeviceIoControl with FSCTL_SET_ZERO_DATA in order to specify what ranges in the file are sparse zeros. However, it appears that the following steps also work when creating a new sparse file. First, using Win32 (via .NET 1.1 pinvoke), I ... CreateFile (returns a handle)
29
3259
by: Halid Umar A M | last post by:
Hi All, I m Halid Umar, network security research student. I will explain my situation and give me hint if you can. I expect reply from you all. * I have to handle numbers that have more than 64 bit like 128bit in cryptography applications. * For example, if i want to add two 128 bit numbers what should i do in C.
14
2129
by: John | last post by:
My friend told me that his company will migrate the VC++ win32 applications to C++ .NET windows applications. I don't understand why since currently Microsoft only supports .NET on windows platforms. If Win32 applications and .NET application both will run on windows only, what's the advantages for the migration? Or maybe this type of migration is not common in practice?
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.