Failed to set checkbox using C # WinAPI

I am trying to check a checkbox inside an AviReComp program and I cannot do this. I tried all kinds of codes:

//Check the checkbox IntPtr SubtitlesSection = FindWindowEx(MoreOptions, IntPtr.Zero, null, "Subtitles"); IntPtr AddSubtitlesCheckbox = FindWindowEx(SubtitlesSection, IntPtr.Zero, null, "Enable/Disable"); SendMessage(AddSubtitlesCheckbox, BM_SETSTATE, 1, IntPtr.Zero); SendMessage(AddSubtitlesCheckbox, BM_SETCHECK, 1, IntPtr.Zero); SendMessage(AddSubtitlesCheckbox, WM_PAINT, 0, IntPtr.Zero); SendMessage(AddSubtitlesCheckbox, WM_LBUTTONDOWN, 1, MakeLParam(10, 10)); SendMessage(SubtitlesSection, WM_PARENTNOTIFY, (int)MakeLParam((int)AddSubtitlesCheckbox, WM_LBUTTONDOWN), MakeLParam(26, 31)); SendMessage(SubtitlesSection, WM_PARENTNOTIFY, (int)MakeLParam((int)AddSubtitlesCheckbox, WM_LBUTTONUP), MakeLParam(26, 31)); 

The checkbox is located on the "Add-ons" tab under the "Subtitles" section and is called "Enable / Disable".

Am I doing something wrong?

Thanks for any help!

Edit: Now I see that this code really works and it checks the checkbox, but I still have a problem, since it does not change all the controls that should change when I check the box manually, and not inside my program. Is there a way to force the parent to repaint itself or to trigger a change event when I check the box as checked?

-one
source share
2 answers

try using spy ++ to make sure the checkbox is checked, if all else fails, and you need to run this on Vista and higher. I would use Windows Automation

0
source

When the state of the child control changes (in response to a user action), it sends some notifications to the parent window, and the parent window, by detecting these messages, performs actions. These notifications are WM_COMMAND and WM_NOTIFY .

While tracking messages sent to the parent window of the checkbox control (and checking the control with the mouse), I noticed one WM_COMMAND message and two WM_NOTIFY messages. Those messages that are not available when I programmatically sent a BM_SETCHECK message to this flag. So the magic revealed.

Sending WM_NOTIFY bit complicated because you need to allocate memory in the address space of another process (using VirtualAllocEx , for the NMHDR structure), fill the memory (using WriteProcessMemory ), send a message, and then release the allocated memory.

Sending a WM_COMMAND message WM_COMMAND much easier. I tested it and it will work!

  Win32.SendMessage(SubtitlesSection, Win32.Message.WM_COMMAND, 0, AddSubtitlesCheckbox); 

The message is sent to the parent control using the control handle as the fourth parameter. The third parameter of the function must be a control identifier, and the identifier changes each time. But, I hope, it seems that the program checks the control descriptor, not the identifier.

0
source

Source: https://habr.com/ru/post/1397844/


All Articles