CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189

    Swap Button Positions

    Hi Guys,

    I have here a simple problem that I could not figure how to do it. I have two buttons, lets say button 1 and 2, I need to swap these button to each others position. This means That button 1 will take button 2 position in the dialog and button 2 will take button 1 position.

    For the time being, I changing the text on the button and it looks like its swapping but its just changing the text.

    PHP Code:
    void CPLAYDlg::OnButton(UINT uID
    {
        
    // TODO: Add your control notification handler code here
        
    SetDlgItemText(uID,"0");
        
    CString str;
        
    str.Format ("%d",uID-1000);
        
    SetDlgItemText(IDC_BUTTON0,str);    

    in the above code, I changed the text of button 0 with the button with uID as ID. Any one can help me on this????
    Thank You.
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    Do you need to do this permanently (i.e. from start-up) or are you saying that you want to the buttons to be swapping positions while the program is actually running?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189
    Originally posted by John E
    Do you need to do this permanently (i.e. from start-up) or are you saying that you want to the buttons to be swapping positions while the program is actually running?
    Tha buttons is supposed to swap when one of the buttons is clicked. And it should not be permanent as as I may add more buttons and add the same functionality to swap between each other.Is that possible??
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    Max - I've heard of some great ways to confuse your users but that one deserves a prize...!

    Try something like this for each of the buttons (I haven't tested the code but it should work)

    GetDlgItem(IDC_BUTTONx)->MoveWindow(newXPos, newYPos, newWidth, newHeight);
    GetDlgitem(IDC_BUTTONx)->ShowWindow(SW_HIDE);
    GetDlgitem(IDC_BUTTONx)->ShowWindow(SW_SHOWNA);

    You might have to experiment with 'SW_SHOWNA'. If it doesn't work, try SW_SHOW or SW_SHOWNORMAL

    Good luck!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Code:
    void FOO_DIALOG::SwapButtons(CButton& button1, CButton& button2)
    {
    	CRect rect1, rect2;
    	button1.GetWindowRect(&rect1);
    	button2.GetWindowRect(&rect2);
    	ScreenToClient(&rect1);
    	ScreenToClient(&rect2);
    	Button1.MoveWindow(&rect2);
    	Button2.MoveWindow(&rect1);
    }
    Didn't test it
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189
    Originally posted by John E
    Max - I've heard of some great ways to confuse your users but that one deserves a prize...!


    Originally posted by souldog

    code:--------------------------------------------------------------------------------
    void FOO_DIALOG::SwapButtons(CButton& button1, CButton& button2)
    {
    CRect rect1, rect2;
    button1.GetWindowRect(&rect1);
    button2.GetWindowRect(&rect2);
    ScreenToClient(&rect1);
    ScreenToClient(&rect2);
    Button1.MoveWindow(&rect2);
    Button2.MoveWindow(&rect1);
    }
    --------------------------------------------------------------------------------


    Didn't test it
    That just what I wanted. Thanks both of you.
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    Pleasure - incidentally, I've sometimes found that changing a window's style or position - e.g. MoveWindow(), SetWindowPos(), ModifyStyle() etc - can't be relied upon to work immediately in all flavours of Windows. Sometimes the window isn't moved or re-styled until it next gets re-drawn - therefore it's a good idea to force a re-draw by hiding the window and re-showing it again.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  8. #8
    Join Date
    Jun 2002
    Posts
    395
    You could also use SetDlgCtrlID() and swap the control id's. Combine this with swapping the button text, it might be easier that moving everything around. Plus, this method should preserve the tab ordering or the buttons.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured