简体   繁体   中英

How do I load hex value from program settings or INI (custom class) in C# as an int?

I am using C# application to monitor another program, and simply click a button when inactivity is detected.

I am using Pinvoke and that works great. My issue is loading the button control ID from an INI which is in hex format. inif simply reads INI file for hex string ("000003F2").

//Load control id from INI file
public int m_button_id;
int m_button_id = Int32.Parse(inif.Read("MMonitor", "button_id"));

Now I need this to work with the following...

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr GetDlgItem(IntPtr hWnd, int nIDDlgItem);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);

//Click Button
IntPtr hWndButton = GetDlgItem(m_handle, m_button_id);
int wParam = (BN_CLICKED << 16) | (m_button_id & 0xffff);
SendMessage(m_handle, WM_COMMAND, wParam, hWndButton);

m_button_id has to be in the format of hex not decimal. It should load this "000003F2" from INI and set it equal to m_button_id. I am having so many issues due to it being a string.

This works fine if I set manually...

public int m_button_id = 0x3F2;

Use Convert.ToInt32(hexString, 16)

or int.Parse(hexString, System.Globalization.NumberStyles.HexNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM