Can I programmatically move a control in a window in Visual C++?

Question:
I’m beginning the design of an application where it would be advantageous to be able to programmatically move the position of a control on its form. Is it possible to do this using Visual C++?

Answer:
Yes, all control classes (ie CEdit, CListBox, CStatic, etc) in Visual C++ are derived from CWnd, the base class containing all the fundamental functionality of a Window. Please keep in mind that neither CWnd nor any of its decendents are Window Controls but are merely C++ classes that strongly wrap Window API functions that permit communication with Window Controls such as sending a message to control to reposition itself. Hence having said that, to reposition a control on a window using Visual C++ with MFC, you could call either CWnd:: MoveWindow or CWnd:: SetWindowPos depending your needs. MoveWindow allows you to change the position and dimensions of a specified Window Control, and SetWindowPos allows you to change the position, dimensions and Z-order of a child, popup and top level windows. Z-order is the order of window on a stack of windows that reside on the Z axis which is an imaginary line that sticks straight out from the screen. For example if you want to move a button control on a CFormView derived class 26 screen units (pixels) down the Y axis from its current position you could do the following: 1. void CMyFormView::OnInitialUpdate() 2. { 3. CFormView::OnInitialUpdate(); 4. 5. // Retrieve a pointer to the button control. 6. CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); 7. ASSERT(pBtn); 8. // Structure used to store the button control’s dimensions. 9. RECT rect; 10. // Get button control’s dimensions with respect to the screen. 11. pBtn->GetWindowRect(&rect); 12. // Convert the button control’s dimensions with respect to its parent window. 13. ScreenToClient(&rect); 14. // Adjust button control’s dimensions +26 pixels down the Y axis. 15. rect.top +=26; 16. rect.bottom +=26; 17. 18. // Move the button control 26 pixels down the Y axis. 19. pBtn->MoveWindow(&rect); 20. }

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may