Click Messages
Site Map Feedback
Up Controls Files Moving Data
Making the MsgWnd Project indicated that most people will only want to send a couple of messages in their applications.
In that Project WM_NOTIFY and SendMessage are used.
Here a WM_COMMAND PostMessage is sent.
So if you have, for example, a Subclassed Button resource that needs to tell its Owner that it has been clicked, first add the following bools to your control's header:
  bool LButtonDown; // Keeps track of the mouse for Click Messages.
  bool MouseOver;   // true if the mouse is over the window.
In the control's constructor put:
  LButtonDown=false;
  MouseOver=false;
Now use ClassWizard to add OnMouseMove, OnLButtonDown, OnLButtonUp and make them look like this:
void CMsgWnd::OnMouseMove(UINT nFlags, CPoint point) {
  if(!MouseOver) {
    MouseOver=true;
    SetCapture();
  }else{
    CRect Rect;
    GetClientRect(Rect);
    if(Rect.PtInRect(point)) {
      MouseOver=false;
      ReleaseCapture();
    }else{
      CWnd::OnMouseMove(nFlags, point);
      return;
  } }
  RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
  CWnd::OnMouseMove(nFlags, point);
}

void CMsgWnd::OnLButtonDown(UINT nFlags, CPoint point) {
  if(MouseOver) LButtonDown=true;
  else CWnd::OnLButtonDown(nFlags, point);
}

void CLinkButton::OnLButtonUp(UINT nFlags, CPoint point) {
  if(MouseOver && LButtonDown) {
    GetOwner()->PostMessage(WM_COMMAND, GetDlgCtrlID());
    MouseOver=false;
    RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
  }else CButton::OnLButtonUp(nFlags, point);
  LButtonDown=false;
}

The dialog that owns this control can now have an ON_BN_CLICKED event handler which will be fired when the Button is clicked. If you want to have the Mouse Cursor change while over the button, look at the MsgWnd Project which has a neat way of doing this. The same applies if you want to change the font.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1286) Last modified: Fri, 15 May 2009 15:27:08 +0100