How to find out if a check box is checked...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tbarto
    New Member
    • Feb 2007
    • 19

    How to find out if a check box is checked...

    Hi everybody

    For some time I have been working on an UI testing application. The application under test was developed in C#, and my application has been developed in C# as well. I do not have a source code of the application under test, so I use Win API messages and methods. I faced the following problem:

    The application under test contains a check box:

    Class: WindowsForms10. BUTTON
    Text: "Some text"
    Style: "Some styles"

    After I get a handle to the check box ( I am sure I have the correct handle ) I use SendMessage to retrieve the check box state:

    Code:
            
    
    private const int BM_GETSTATE       = 0x00F2;
    private const int BM_GETCHECK       = 0x00F0;
    private const int BST_UNCHECKED     = 0x0000;
    private const int BST_CHECKED       = 0x0001;
    
    int result;
    
    result = SendMessage(correctHandle, BM_GETSTATE, 0, 0);
    
    or
    
    result = SendMessage(correctHandle, BM_GETCHECK, 0, 0);
    My problem is that both SendMessage methods from above always return 0, even if a check box is checked.

    Can anybody tell me what is wrong with the code above?

    Thanks in advance.
  • amitjul
    New Member
    • Apr 2007
    • 5

    #2
    Hi tbarto,
    Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.


    Originally posted by tbarto
    Hi everybody

    For some time I have been working on an UI testing application. The application under test was developed in C#, and my application has been developed in C# as well. I do not have a source code of the application under test, so I use Win API messages and methods. I faced the following problem:

    The application under test contains a check box:

    Class: WindowsForms10. BUTTON
    Text: "Some text"
    Style: "Some styles"

    After I get a handle to the check box ( I am sure I have the correct handle ) I use SendMessage to retrieve the check box state:

    Code:
            
    
    private const int BM_GETSTATE       = 0x00F2;
    private const int BM_GETCHECK       = 0x00F0;
    private const int BST_UNCHECKED     = 0x0000;
    private const int BST_CHECKED       = 0x0001;
    
    int result;
    
    result = SendMessage(correctHandle, BM_GETSTATE, 0, 0);
    
    or
    
    result = SendMessage(correctHandle, BM_GETCHECK, 0, 0);
    My problem is that both SendMessage methods from above always return 0, even if a check box is checked.

    Can anybody tell me what is wrong with the code above?

    Thanks in advance.

    Comment

    • tbarto
      New Member
      • Feb 2007
      • 19

      #3
      Originally posted by amitjul
      Hi tbarto,
      Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.
      Hi amitjul

      As far as now I haven't found the solution. I've been working on some other issues. If I am able to get the solution, I will post it here.

      Regards

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Originally posted by amitjul
        Hi tbarto,
        Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.
        Googling for SendMessage, I found out the the Net checkbox will always return 0 because it has an extended style of '0000000B'. He later posted that he solved the problem with AccessibleObjec tFromWindow but that it was difficult because of poor documentation. He did not post a solution :mad:

        AccessibleObjec tFromWindow documentation is http://msdn2.microsoft.com/en-us/library/ms696137.aspx

        Finally, googling AccessibleObjec tFromWindow "C#" shows some working code, but very ugly. If this helps you get a solution, please post it here. Thanks! --Sam

        Comment

        • tbarto
          New Member
          • Feb 2007
          • 19

          #5
          Gentleman

          I have found how to get access to an UI component with AccessibleOblec tFromWindow, however it still does not work for me:

          Code:
           
          private const int OBJID_WINDOW = 0;
          
          [DllImport("oleacc.dll", PreserveSig = false)]
          [return: MarshalAs(UnmanagedType.Interface)]
          public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);
          
          Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
          
          IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
          ref uid);
          
          object o = accObj.get_accValue(0); //always 0
          
          object o1 = accObj.get_accName(0); // works fine
          About the code above: the get_accName works fine for me, it returns a text that the checxbox contains. Unfortunately, get_accValue always returns 0, even if the checkbox is checked. Perhaps any of You can see that there is something wrong?

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            Originally posted by tbarto
            Gentleman

            I have found how to get access to an UI component with AccessibleOblec tFromWindow, however it still does not work for me:

            Code:
             
            private const int OBJID_WINDOW = 0;
             
            [DllImport("oleacc.dll", PreserveSig = false)]
            [return: MarshalAs(UnmanagedType.Interface)]
            public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);
             
            Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
             
            IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
            ref uid);
             
            object o = accObj.get_accValue(0); //always 0
             
            object o1 = accObj.get_accName(0); // works fine
            About the code above: the get_accName works fine for me, it returns a text that the checxbox contains. Unfortunately, get_accValue always returns 0, even if the checkbox is checked. Perhaps any of You can see that there is something wrong?
            So each control has a GUID? How do you figure out what it is?

            Comment

            • tbarto
              New Member
              • Feb 2007
              • 19

              #7
              Originally posted by SammyB
              So each control has a GUID? How do you figure out what it is?
              Hi

              This is what I've been trying to figure out. I have found that:

              Code:
              Guid g = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
              when googling, but I do not exactly understand the "618736e0-3c3d-11cf-810c-00aa00389b71" identifier. I have tried to change that identifier and run the code, but it caused an exception. Since I am not experienced in working with "Guid" I did not find the answer for Your question so far.

              Comment

              • SammyB
                Recognized Expert Contributor
                • Mar 2007
                • 807

                #8
                > I have found that:Guid when googling
                Hopefully, not at http://www.cafepress.com/cp/moredeta...olorNo=23&pr=F

                Comment

                • tbarto
                  New Member
                  • Feb 2007
                  • 19

                  #9
                  Originally posted by SammyB
                  > I have found that:Guid when googling
                  Hopefully, not at http://www.cafepress.com/cp/moredeta...olorNo=23&pr=F
                  No, definitely not that one :-)

                  Comment

                  • SammyB
                    Recognized Expert Contributor
                    • Mar 2007
                    • 807

                    #10
                    Originally posted by tbarto
                    No, definitely not that one :-)
                    Up until now, I have associated GUIDs with tools that are in the registry and used code such as http://msdn2.microsoft.com/en-us/lib...id(vs.80).aspx. But, it looks like from http://msdn2.microsoft.com/en-us/lib...29(VS.80).aspx that there maybe a GUID for each control. It looks like http://msdn2.microsoft.com/en-us/library/ms797957.aspx may be some help in finding the GUIDs. What a rat's nest!

                    Comment

                    • tbarto
                      New Member
                      • Feb 2007
                      • 19

                      #11
                      Originally posted by SammyB
                      Up until now, I have associated GUIDs with tools that are in the registry and used code such as http://msdn2.microsoft.com/en-us/lib...id(vs.80).aspx. But, it looks like from http://msdn2.microsoft.com/en-us/lib...29(VS.80).aspx that there maybe a GUID for each control. It looks like http://msdn2.microsoft.com/en-us/library/ms797957.aspx may be some help in finding the GUIDs. What a rat's nest!
                      Thanks for the info You've provided. I will try work on that, unfortunately next week, because tomorrow my vacation days start :-)
                      Thanks one more time for Your help

                      Comment

                      • amitjul
                        New Member
                        • Apr 2007
                        • 5

                        #12
                        Hi Guys,
                        That Guid is the constant which you can't change (i dont know the reason :) ). and AccessibleObjec tFromWindow is working with preety weird way. i tried to decode it but i failed. ne way, i just found a good solution.

                        IAccessible accObj = (IAccessible)Ac cessibleObjectF romWindow(hwnd, OBJID_WINDOW,
                        ref uid);

                        In above code, instead of passing OBJID_WINDOW , try with OBJID_CLIENT

                        accObj.get_accS tate(0) will return you some integer value like as below:

                        public const long UNCHECKED = 1048576;
                        public const long CHECKED = 1048592;
                        public const long UNCHECKED_FOCUS ED = 1048580; // if control is focused
                        public const long CHECKED_FOCUSED = 1048596; // if control is focused


                        so based on that you can easily distiguish that which one is checked (checkbox/radionbutton). Its weird but deadlines are there so.... :)





                        Originally posted by tbarto
                        Thanks for the info You've provided. I will try work on that, unfortunately next week, because tomorrow my vacation days start :-)
                        Thanks one more time for Your help

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          Originally posted by amitjul
                          Hi Guys,
                          That Guid is the constant which you can't change (i dont know the reason :) ). and AccessibleObjec tFromWindow is working with preety weird way. i tried to decode it but i failed. ne way, i just found a good solution.

                          IAccessible accObj = (IAccessible)Ac cessibleObjectF romWindow(hwnd, OBJID_WINDOW,
                          ref uid);

                          In above code, instead of passing OBJID_WINDOW , try with OBJID_CLIENT

                          accObj.get_accS tate(0) will return you some integer value like as below:

                          public const long UNCHECKED = 1048576;
                          public const long CHECKED = 1048592;
                          public const long UNCHECKED_FOCUS ED = 1048580; // if control is focused
                          public const long CHECKED_FOCUSED = 1048596; // if control is focused


                          so based on that you can easily distiguish that which one is checked (checkbox/radionbutton). Its weird but deadlines are there so.... :)
                          So, it all came out guid and you get the t-shirt.! :D

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Are you trying to capture the checked state of a checkbox in another application that is not your own? Sneaky sneaky.

                            Comment

                            • amitjul
                              New Member
                              • Apr 2007
                              • 5

                              #15
                              I am trying to get the state of checkbox/radiobutton/button from a C# application to C# application. so both are different application and both are mine. :)


                              Where is the T-shirt ?? :)

                              Originally posted by Plater
                              Are you trying to capture the checked state of a checkbox in another application that is not your own? Sneaky sneaky.

                              Comment

                              Working...