Finding out control IDs - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 34

Thread: Finding out control IDs

  1. #16
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Sorry my mistake, use FindWindowEx. Its a childwindow and FindWindow only searches for Top-Level Windows.
    Hope this helps,
    Regards,
    Usman.

  2. #17
    Join Date
    Jul 2004
    Posts
    105
    I'm still having problems. here is what i am doing.

    BOOL CMyFileDialog::OnInitDialog() // Override
    {
    ...
    CFileDialog::OnInitDialog();

    // Get a pointer to the original dialog box.
    CWnd *wndDlg = GetParent();

    CListView *pListView = (CListView *)FindWindowEx(wndDlg->m_hWnd,m_hWnd,"SysListView32", NULL);

    ...

    return true;
    }

  3. #18
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Try this...
    Code:
    ::FindWindowEx(m_hWnd, NULL, "SysListView32", NULL);
    Hope this helps.
    Regards,
    Usman.

  4. #19
    Join Date
    Jul 2004
    Posts
    105
    Ok, that didnt seem to work either. Here is what I did.

    HWND hWnd = ::FindWindowEx(m_hWnd, NULL, "SysListView32", NULL);
    CListCtrl * pListCtrl = (CListCtrl *)FromHandle(hWnd);

    the first line set hWnd to 0x00000000. the second line I use to get a pointer to the object and that isnt working because the hWnd handle is bad. anything else you can think of? thanks so much for your time and help

  5. #20
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    Quote Originally Posted by senkyoshi
    Problem: i am trying to use FindWindow with class "SysListView32" but it isnt returning anything. any other way to get a reference to CListCtrl in the CFileDialog?
    thanks
    Have you already read this ( http://msdn.microsoft.com/msdnmag/issues/02/01/c/ ) article?
    Paul DiLascia pointed out how you could do this:
    ............
    Many of the common dialogs have controls with documented IDs like stc1 for a static text control and lst1 for a listbox; these symbols are #defined in <dlgs.h>. You might think the list control would be lst1, but spelunking with Spy (see Figure 1) reveals that the list control is actually contained in another window of class, SHELLDLL_DefView. The SHELLDLL_DefView window has ID lst2; the list control inside it has child ID 1. So to get the list control, you can write:

    // from your CFileDialog derivate
    CListCtrl* plc = (CListCtrl*)GetParent()->
    GetDlgItem(lst2)->GetDlgItem(1);

    Remember, when you customize the open file dialog, your CFileDialog is actually a child of the real dialog, which explains why you must use GetParent. (For details see my article "Give Your Applications the Hot New Interface Look with Cool Menu Buttons" in the January 1998 issue of MSJ.) The cast to CListCtrl* works as per the usual MFC trick, because CListControl has neither data members nor virtual functions; it's a pure wrapper class. (Since GetDlgItem returns a pointer to a temporary CWnd, not a CListCtrl, downcasting would normally be a major booboo.)
    Once you have a pointer to the list control, you can do whatever you likeŚwithin reason.........

  6. #21
    Join Date
    Jul 2004
    Posts
    105

    Smile

    Thanks VictorN! When I compile

    CListCtrl* plc = (CListCtrl*)GetParent()->GetDlgItem(lst2)->GetDlgItem(1);

    it says: error C2065: 'lst2' : undeclared identifier

    do you know why this is happening? do I need to add it or the library it is in?

    thanks


    ...nevermind. I had to include <dlgs.h>. I thought that I had already added it
    Last edited by senkyoshi; July 27th, 2004 at 09:49 AM.

  7. #22
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    Once again:
    Many of the common dialogs have controls with documented IDs like stc1 for a static text control and lst1 for a listbox; these symbols are #defined in <dlgs.h>....
    Have you #included <dlgs.h>?

  8. #23
    Join Date
    Jul 2004
    Posts
    105

    Question

    Yes, I actually saw that and it works fine now.
    But now, compiling works fine but when I run it and when it gets to that line I get an Unhandled Exception (MFC42D) 0x00000005 Access Violation. Here is my code:

    BOOL CMyFileDialog::OnInitDialog() // Override
    {
    CFileDialog::OnInitDialog();

    // Get a pointer to the original dialog box.
    CWnd *wndDlg = GetParent();

    if ( wndDlg != NULL ) {
    wndDlg->PostMessage(WM_COMMAND, 40964, NULL);
    }

    CListCtrl* plc = (CListCtrl*)wndDlg->GetDlgItem(lst2)->GetDlgItem(1);

    return true;
    }

    any ideas?
    thanks

  9. #24
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    The problem seems to be the List Control is yet not created while OnInitDialog is being called...
    Post user defined Message to your dialog and try to retrieve list control pointer in the handler of this message:
    Code:
    // .cpp
    #define MY_MESSAGE    (WM_APP + 1)
    .......
    BEGIN_MESSAGE_MAP(CMyFileDialog, CDialog)
    	//{{AFX_MSG_MAP(CMyFileDialog)
    ..............
    	//}}AFX_MSG_MAP
    	ON_MESSAGE(MY_MESSAGE, OnMyMessage)
    
    END_MESSAGE_MAP()
    .........
    BOOL CMyFileDialog::OnInitDialog() // Override
    {
           CFileDialog::OnInitDialog();
    
           // Get a pointer to the original dialog box.
           CWnd *wndDlg = GetParent();
    
           if ( wndDlg != NULL ) {
                  wndDlg->PostMessage(WM_COMMAND, 40964, NULL);
           }
           PostMessage(MY_MESSAGE);
           return true;
    }
    
    LRESULT CMyFileDialog::OnMyMessage(WPARAM, LPARAM)
    {
           ListCtrl* plc = (CListCtrl*)GetParent()->GetDlgItem(lst2)->GetDlgItem(1);
           return 0;
    }
    ..........
    .........
    // .h
    	//{{AFX_MSG(CMyFileDialog)
    ..............
    	//}}AFX_MSG
    	afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);
    	DECLARE_MESSAGE_MAP()
    .........

  10. #25
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    senkyoshi!
    Take a look at this article: List View Mode, SetForegroundWindow, and Class Protection

    PS:
    however (in contrary to the author of this article), you should newer use (WM_USER + < n >) as ID for the user defined messages in MFC aoolications. Use (WM_APP + < n >) instead!

  11. #26
    Join Date
    Jul 2004
    Posts
    105

    Question

    Wonderful!
    things are working great now. Now on to another problem. On page one of this post usman999_1 says to use

    SendMessage(GetParent(hHeaderCtrl), WM_NOTIFY, NULL, (LPARAM)&NMHeader);

    to get the list to be sorted by the desired column. however, SendMessage only takes 3 parameters.

    So this is what I try and my program crashes when it tries the SendMessage line.

    LRESULT CMyFileDialog::OnMyMessage(WPARAM, LPARAM)
    {
    CListCtrl* plc = (CListCtrl*)GetParent()->GetDlgItem(lst2)->GetDlgItem(1);


    CHeaderCtrl *pHCtrl = plc->GetHeaderCtrl();

    HWND hHeaderCtrl = pHCtrl->m_hWnd;

    NMHDR pNMHDR;
    pNMHDR.hwndFrom = hHeaderCtrl;
    pNMHDR.idFrom = pHCtrl->GetDlgCtrlID();

    NMHEADER * NMHeader;
    NMHeader->hdr = pNMHDR;
    NMHeader->iItem = 3;
    NMHeader->iButton = 0;

    SendMessage(WM_NOTIFY, NULL, (LPARAM)&NMHeader);


    return 0;
    }

    Can you see where I am having a problem?

    thanks

  12. #27
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    HWND hHeaderCtrl = pHCtrl->m_hWnd;

    NMHDR pNMHDR;
    pNMHDR.hwndFrom = hHeaderCtrl;
    pNMHDR.idFrom = pHCtrl->GetDlgCtrlID();

    NMHEADER * NMHeader;
    NMHeader->hdr = pNMHDR;
    NMHeader->iItem = 3;
    NMHeader->iButton = 0;

    SendMessage(WM_NOTIFY, NULL, (LPARAM)&NMHeader);
    Try this:
    Code:
    HWND hHeaderCtrl = pHCtrl->m_hWnd;
    
    NMHDR nmHDR;
    nmHDR.hwndFrom = hHeaderCtrl;
    nmHDR.idFrom = pHCtrl->GetDlgCtrlID();
    
    NMHEADER  NMHeader;
    NMHeader.hdr = nmHDR;
    NMHeader.iItem = 3;
    NMHeader.iButton = 0;
    
    SendMessage(WM_NOTIFY, NULL, (LPARAM)&NMHeader);

  13. #28
    Join Date
    Jul 2004
    Posts
    105

    Question

    OK, it isn't crashing anymore but nothing is happening. The following line:

    NMHeader->iItem = 3;

    should be telling it to click the Modified area correct (since it is zero based and we have Name, size, type, Modified) Well, nothing is being clicked. Am I missing something in the code I last posted?

  14. #29
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,295
    Try to check if the header with the text "Modified" is the item 3:
    Code:
    HDITEM hdi;
    TCHAR  lpBuffer[256];
    
    hdi.mask = HDI_TEXT;
    hdi.pszText = lpBuffer;
    hdi.cchTextMax = 256;
    bool bFound = false;
    for (int iCol = 0; i < pHCtrl->GetItemCount(); iCol++)
    {
       pHCtrl->GetItem(iCol, &hdi);
       if (strcmp(hdi.pszText, _T("Modified") == 0)
       {
           bFound = true;
           break;
       }
    }
    if(bFound)
    {
            // now iCol - item number
           //  send your message
    }

  15. #30
    Join Date
    Jul 2004
    Posts
    105
    Yes, "Modified" is column(item) 3

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center