2007年3月20日 (火)

テキストのドロップ

クリップボードを介して別のウィンドウにテキストデータをドロップするサンプルコード。OLE のドラッグ & ドロップをサポートしていない素のエディットコントロールへのドロップ操作を実現する。

#ifdef _UNICODE
    #define CF_MYTEXT   CF_UNICODETEXT
#else
    #define CF_MYTEXT   CF_TEXT
#endif

void CMyDlg::Copy(LPCTSTR pszText)
{
    if (OpenClipboard())
    {
        ::EmptyClipboard();
        DWORD dwBytes = (lstrlen(pszText)+1) * sizeof(TCHAR);
        HGLOBAL hMem = ::GlobalAlloc(GMEM_DDESHARE, dwBytes);
        LPTSTR pMem = (LPTSTR)::GlobalLock(hMem);
        lstrcpy(pMem, pszText);
        ::GlobalUnlock(hMem);
        ::SetClipboardData(CF_MYTEXT, hMem);
        ::CloseClipboard();
    }
}

void CMyDlg::Paste(HWND hWnd)
{
    if (::IsClipboardFormatAvailable(CF_MYTEXT))
        ::PostMessage(hWnd, WM_PASTE, 0, 0);
}

void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
    SetCapture();
    GetDlgItemText(IDC_EDIT1, m_sText);
    
    CDialog::OnLButtonDown(nFlags, point);
}

void CMyDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
    if (GetCapture() == this)
        ::SetCursor(AfxGetApp()->LoadCursor(IDC_POINTER_COPY));

    CDialog::OnMouseMove(nFlags, point);
}

void CMyDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
    if (GetCapture() == this)
    {
        ClientToScreen(&point);
        HWND hWnd = ::WindowFromPoint(point);
        if (hWnd)
        {
            ::ScreenToClient(hWnd, &point);
            LRESULT nSel = ::SendMessage(hWnd,
                EM_CHARFROMPOS, 0, MAKELPARAM(point.x, point.y));
            if (nSel != -1)
            {
                nSel = LOWORD(nSel);
                ::SendMessage(hWnd, EM_SETSEL, nSel, nSel);
                ::SetFocus(hWnd);
                Copy(m_sText);
                Paste(hWnd);             }         }         ReleaseCapture();     }         CDialog::OnLButtonUp(nFlags, point); }

|

2007年3月 9日 (金)

GDI+ を使う

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

が必要。さらに、MFC で開発するには、下記の情報が重要になる。

リファレンスはこちら。

|

2007年2月22日 (木)

Win2k 以降かどうかの判定

BOOL IsWindows2000orLater()
{
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    return (::GetVersionEx(&osvi) && (osvi.dwMajorVersion >= 5));
}

【参考資料】
Getting the System Version
GetVersionEx

|

2007年2月 2日 (金)

互換性

Minimum system requiredMacros to define
Windows Vista _WIN32_WINNT>=0x0600
WINVER>=0x0600
Windows Server 2003 _WIN32_WINNT>=0x0502
WINVER>=0x0502
Windows XP _WIN32_WINNT>=0x0501
WINVER>=0x0501
Windows 2000 _WIN32_WINNT>=0x0500
WINVER>=0x0500
Windows NT 4.0 _WIN32_WINNT>=0x0400
WINVER>=0x0400
Windows Me _WIN32_WINDOWS=0x0500
WINVER>=0x0500
Windows 98 _WIN32_WINDOWS>=0x0410
WINVER>=0x0410
Windows 95 _WIN32_WINDOWS>=0x0400
WINVER>=0x0400
Internet Explorer 7.0 _WIN32_IE>=0x0700
Internet Explorer 6.0 SP2 _WIN32_IE>=0x0603
Internet Explorer 6.0 SP1 _WIN32_IE>=0x0601
Internet Explorer 6.0 _WIN32_IE>=0x0600
Internet Explorer 5.5 _WIN32_IE>=0x0550
Internet Explorer 5.01 _WIN32_IE>=0x0501
Internet Explorer 5.0, 5.0a, 5.0b _WIN32_IE>=0x0500
Internet Explorer 4.01 _WIN32_IE>=0x0401
Internet Explorer 4.0 _WIN32_IE>=0x0400
Internet Explorer 3.0, 3.01, 3.02 _WIN32_IE>=0x0300

【資料】
Using the Windows Headers

|

2006年12月12日 (火)

フォント名とサイズの列挙

資料のリンクです。

エディタの「使用フォントの設定」メニューの実装で必要になったので勉強してみました。フォントの設定は CFontDialog を使うのが楽なのですが、今回は、CPropertySheet を使うため、最初は何とか貼り付けられないかと考えていたのですが、あまり、いい方法がないのと、たとえ苦労して貼り付けても、フォント名とサイズぐらいの設定しか必要ないので、それなら、自分で取得した方がスマートということになりました。やってみると、以外にこっちの方が楽だったりしますね。結構、癖の強い API だとは思いますが・・・。

|

2005年11月 5日 (土)

INI ファイルの処理

// ini ファイルからキーを削除
::WritePrivateProfileString(
    szSection, szKey, NULL, pszFileName);
// ini ファイルからセクション全体を削除
::WritePrivateProfileString(
    szSection, NULL, NULL, pszFileName);
// MFC の場合
AfxGetApp()->WriteProfileString(pszSection, pszKey, NULL);
AfxGetApp()->WriteProfileString(pszSection, NULL, NULL);

【参考資料】
CWinApp::WriteProfileString (MFC)

|