简体   繁体   English

如何在C#中从程序设置或INI(自定义类)中将十六进制值作为int加载?

[英]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. 我正在使用C#应用程序监视另一个程序,当检测到不活动时,只需单击一个按钮即可。

I am using Pinvoke and that works great. 我正在使用Pinvoke,效果很好。 My issue is loading the button control ID from an INI which is in hex format. 我的问题是从十六进制格式的INI加载按钮控件ID。 inif simply reads INI file for hex string ("000003F2"). inif只是读取INI文件中的十六进制字符串(“ 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. m_button_id必须采用十六进制格式,而不是十进制格式。 It should load this "000003F2" from INI and set it equal to m_button_id. 它应从INI加载此“ 000003F2”,并将其设置为等于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) 使用Convert.ToInt32(hexString, 16)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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