Smart to achieve hyperlink control

zhaozj2021-02-08  397

When we use MFC to develop applications, we usually place a large number of controls in the window, complete our control by triggering events of these controls. But not every control provides all event messages, for example, we add a static text control (CSTATE class) in the dialog window, at which there is no message in ClassWizard, which is not possible to complete the required control. VC provides us with an API function getWindowRect (). This function can get the location of all controls in the window so that we can utilize location judging in the window of the window to implement event control. Based on the above principle, it is easy to implement hyperlink control and dynamic prompts and functions such as window area event capture. Take hyperlink control as an example, usually we add an email or hyperlink of an email or home page. Hyperlink generally requires the mouse deformation after the focus, and can respond to the onclick event, which briefly introduces its implementation principle. 1. First create a SAMPLE project based on dialog. 2. Introducing the cursor file into "IDC_Hand" in the resource file of the sample project, add a static text control in the Dialog dialog box in the Sample project to "IDC_LINK", and CAPTION is set to "http: / /Netvc.6to23.com "3. Add data member protected: Rect m_PRECTLINK; // for saving static text boxes 4. Obtain control coordinates ONITDIALOG () Add the following code (Sampledlg.cpp): getdlgitem (iDC_LINK) -> getWindowRect (& m_PRECTLINK); // Put the screen coordinates of the static text in m_PRECTLINK SCREENTOCLIENT (& m_PRECTLINK); // Convert the screen coordinates to customer coordinate 5 The transformation mouse shape is used to add an onMouseMove () message using ClassWizard to the dialog. In the function onmousemove (uint nflags, cpoint point), the Point parameter obtains the mouse coordinates corresponding to the upper left corner of the window, and the control can be achieved by judging the algorithm. / / The mouse is set below, set the cursor into small hand IF (Point.x> m_PRECTLINK.RIGHT && POINT.Y> m_PRECTLINK.TOP & POINT.Y loadCursor (IDC_HAND); // Set the mouse as a small hand-shaped setcursor (hcursor);} // The following statement is used to set the default (arrow) The shape of the mouse, the general mouse removal window automatically restores the default mouse shape, and add if (...) // as appropriate, add the mouse not in the static text area, this example does not add {hcursor hcursor; hcursor = AFXGetApp () -> loadStandardCursor (IDC_IBEAM); // Set the cursor to the default value (arrow) setCursor (HCURSOR);} 6. Add a mouse click Event Use classwizard to add online to the dialog to add online, parameter and onmousemove () same.

转载请注明原文地址:https://www.9cbs.com/read-822.html

New Post(0)