독립 개발자 네트워크 포럼 메인 :: 자유게시판 | 질문과 답변 | 개발 노하우 | 개발 팁 | Win8
여기는 포럼입니다. 오래된 글에 댓글 다는걸 주저하지 마세요.

All times are UTC + 9 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: atl용 하이퍼 링크 static control
PostPosted: 29 Jun 2011, 10:28 
Offline
User avatar

Joined: 19 Nov 2010, 15:37
Posts: 462
mfc 의 CStatic 을 상속받아서 만든 hyper link 컨트롤이나 wtl 용은 많지만

atl의 CWindow 기반은 많지 않은듯해서 만들었습니다.

Attachment:
Cap 2011-06-29 10-24-32-166.png
Cap 2011-06-29 10-24-32-166.png [ 21.95 KiB | Viewed 1425 times ]



예제는 static 컨트롤의 크기, 색상, 두께등을 바꾸는 샘플을 포함하고 있으며,

마우스 클릭시 홈페이지가 열리는 XHyperLinkStatic 클래스도 있습니다.

코드는 아래와 같이 간다하며, 복잡한 옵션(색상 조절, 홈페이지 여는 방법..)을

조절하는 기능을 제공하기 보다, 가져다 쓰는 사람이 직접 수정해서 쓰면 유용할껍니다.


Code:
class XHyperLinkStatic : public CWindowImpl<XHyperLinkStatic, CWindow>
{
public :
   XHyperLinkStatic()
   {
      m_hWndParent = NULL;
      m_bTracking = NULL;
      m_font = NULL;
      m_fontU = NULL;
      m_cursorHand = ::LoadCursor(0, MAKEINTRESOURCE(IDC_HAND));
   }

   virtual ~XHyperLinkStatic()
   {
      if(m_fontU) ::DeleteObject(m_fontU);
   }

   BOOL   Attach(HWND hWndParent, HWND hWndStatic, CString url)
   {
      if(__super::SubclassWindow(hWndStatic)==FALSE)  { ASSERT(0); return FALSE;}

      m_hWndParent = hWndParent;
      m_url = url;
      GetWindowText(m_text);

      // 폰트 만들기
      m_font = (HFONT)::SendMessage(m_hWnd, WM_GETFONT, 0,0);
      LOGFONT lfont;
      GetObject(m_font, sizeof(lfont), &lfont);
      lfont.lfUnderline = TRUE;
      m_fontU = ::CreateFontIndirect(&lfont);

      return TRUE;
   }

public :
   BEGIN_MSG_MAP(XHyperLinkStatic)
      MESSAGE_HANDLER(WM_NCHITTEST, OnNcHitTest)
      MESSAGE_HANDLER(WM_PAINT, OnPaint)
      MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
      MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
      MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave)
      MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
   END_MSG_MAP()

private :
   LRESULT      OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      return HTCLIENT;
   }
   LRESULT      OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      bHandled = FALSE;
      ::InvalidateRect(m_hWnd, NULL, FALSE);
      ::SetCursor(m_cursorHand);

      if(m_bTracking==FALSE)
      {
         TRACKMOUSEEVENT tme;
         tme.cbSize = sizeof(tme);
         tme.hwndTrack = m_hWnd;
         tme.dwFlags = TME_LEAVE|TME_HOVER;
         tme.dwHoverTime = 1;
         m_bTracking = _TrackMouseEvent(&tme);         // _TrackMouseEvent ?
      }

      return 0;
   }
   LRESULT      OnMouseLeave(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      bHandled = FALSE;
      m_bTracking = FALSE;
      ::InvalidateRect(m_hWnd, NULL, FALSE);

      return 0;
   }
   LRESULT      OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      ::ShellExecute(NULL, _T("open"), m_url, NULL, NULL, SW_SHOW);
      return 0;
   }
   LRESULT      OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      bHandled = FALSE;
      __super::UnsubclassWindow();
      return 0;
   }
   LRESULT      OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      PAINTSTRUCT ps;
      HDC dc = ::BeginPaint(m_hWnd, &ps);
      CRect rClient;
      HFONT oldFont;

      GetClientRect(&rClient);
      ::SetBkColor(dc, ::GetSysColor(COLOR_BTNFACE));

      if(m_bTracking)
      {
         ::SetTextColor(dc, RGB(255,0,0));
         oldFont = (HFONT)SelectObject(dc, m_fontU);
      }
      else
      {
         ::SetTextColor(dc, RGB(0,0,255));
         oldFont = (HFONT)SelectObject(dc, m_font);
      }

      // Center Image ?
      BOOL center = GetWindowLong(GWL_STYLE) & SS_CENTERIMAGE ? TRUE : FALSE;
      DWORD dtFormat = DT_LEFT;
      if(center) dtFormat |= DT_VCENTER | DT_SINGLELINE;

      ::DrawText(dc, m_text, -1, rClient, dtFormat);

      SelectObject(dc, oldFont);
      ::EndPaint(m_hWnd, &ps);
      return 0;
   }
   void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
   {
      COLORREF clrPrev = ::SetBkColor(hDC, clr);
      ::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
      ::SetBkColor(hDC, clrPrev);
   }


private :
   HWND      m_hWndParent;
   CString      m_url;
   CString      m_text;
   HFONT      m_font;
   HFONT      m_fontU;
   BOOL      m_bTracking;
   HCURSOR      m_cursorHand;
};



* vs2008/zlib license


Attachments:
ControlTest.7z [56.76 KiB]
Downloaded 92 times
Top
 Profile  
 
 Post subject: Re: atl용 하이퍼 링크 static control
PostPosted: 18 Sep 2012, 18:09 
Offline
User avatar

Joined: 19 Nov 2010, 15:37
Posts: 462
버그도 고치고 버전업 했습니다.

Attachment:
Cap 2012-09-18 18-00-49-153.png
Cap 2012-09-18 18-00-49-153.png [ 14.92 KiB | Viewed 533 times ]


사용 방법은 간단합니다.

Code:
#include "XControlUtil.h"
...
XHyperLinkStatic   m_link;
...
m_link.Attach(m_hWnd, GetDlgItem(IDC_STATIC_XXXX), URL);


실제 프로젝트에는 아래처럼 사용을 했습니다.

Attachment:
Cap 2012-09-18 18-06-20-896.png
Cap 2012-09-18 18-06-20-896.png [ 19.94 KiB | Viewed 533 times ]


Attachments:
XControlUtilTest.7z [65.48 KiB]
Downloaded 51 times
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group