How to calculate ListView header's height when using vbasic?

已答覆 How to calculate ListView header's height when using vbasic?

  • 2012年3月15日 上午 01:42
     
     
    In ListView, in one of the columns I am placing command buttons (which height is equal to ListView row height) (255)
    (Each ListView's row has its own command button)
    In IDE runtime all buttons are nicely lined up and all looks nice.
    However, after compiling to executable, the command buttons are not lined up with ListView rows anymore.
    Here is the code:
    =============

    cmdLine(0).Height = 255
    cmdLine(0).top = lvSelLines(1).top + 385


    For i = 0 To lvSelLines(1).ListItems.Count - 1
    If i > 0 Then
    Load cmdLine(i)
    cmdLine(i).top = cmdLine(i - 1).top + cmdLine(0).height
    End If
    cmdLine(i).Visible = True
    cmdLine(i).ZOrder
    Next
    =================

    That code works fine in my Windows 7 and the buttons are aligned.
    However, when I tried it in Win XP I needed to adjust cmdLine(0).top
    to:
    cmdLine(0).top = lvSelLines(1).top + 305

    How to make that code universal?
    Thanks,
    jas

所有回覆

  • 2012年3月16日 上午 07:47
    版主
     
     已答覆 包含代碼

    Hi jastrzebiec,
    You can get the header control by sending LVM_GETHEADER message. After get the header control, we can use the GetWindowRect function to get the bounding rectangle, see the following code.

            private void button1_Click(object sender, EventArgs e)
            {
                RECT r = new RECT();
                int hwnd = GetHeaderControl(this.listView1);
                if (hwnd > 0)
                {
                    if (GetWindowRect(hwnd, ref r) > 0)
                    {
                        int height = r.bottom - r.top;
                    }
                }
    
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
            [DllImport("user32.dll")]
    public static extern int GetWindowRect(int hwnd, ref RECT rc);
            public static int GetHeaderControl(ListView list)
            {
                const int LVM_GETHEADER = 0x1000 + 31;
                return SendMessage(list.Handle, LVM_GETHEADER, 0, 0);
            }

    Actually, if you have added item to the ListView, there is an easy way to get the height.

    int height = this.listView1.TopItem.Bounds.Top 

    If you still have any doubt and concern about this issue, please let us know.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


  • 2012年3月16日 下午 05:45
     
     

    Thank you.

    It helps me a lot. Do you know how to account for the header border?
    I believe that bounding rectangle is the inner one.
    I am interested of outside boundary.

    Is that border fixed? 2 pixels?

    How to find out?

    /jas

  • 2012年3月19日 上午 07:42
    版主
     
     
    Hi jastrzebiec,
    According to MSDN, GetWindowRect function retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
    What do you mean by “outside boundary” and “inner boundary”? 
    Could you please kindly elaborate your question?
    In addition, do you just want to get the height of the header?
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


  • 2012年3月19日 上午 07:45
    版主
     
     
    In addition to my last reply, you can convert c# to vb.net by using some tool, such as http://www.developerfusion.com/tools/convert/csharp-to-vb/.
    Best Regards,
    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us