#ifndef __LESSON_06_H__
#define __LESSON_06_H__
#ifdef LESSON_06_EXPORTS
#define LessonApi __declspec(dllexport)
#else
#define LessonApi __declspec(dllimport)
#endif
extern "C" LessonApi void HelloWord();
class LessonApi CXUIWindow
{
public:
CXUIWindow();
virtual~CXUIWindow();
public:
HWND Create(HWND hParentWnd, LPCTSTR pstrName, DWORD dwStyle, const RECT& rc, HINSTANCE hInst);
void ShowWindow(int nCmdShow);
void UpdateWindow();
protected:
virtual bool RegisterWindowClass();
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
virtual LPCTSTR GetClassName();
virtual LRESULT HandleMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
protected:
private:
HWND m_hWnd;
HINSTANCE m_hInst;
};
#endif
#include "stdafx.h"
#include "Lesson_06.h"
void HelloWord()
{
MessageBox(NULL, L"Hello Word!", L"动态库", MB_OK);
}
CXUIWindow::CXUIWindow()
{
m_hWnd = NULL;
}
CXUIWindow::~CXUIWindow()
{
}
HWND CXUIWindow::Create(HWND hParentWnd, LPCTSTR pstrName, DWORD dwStyle, const RECT& rc, HINSTANCE hInst)
{
m_hInst = hInst;
if (!RegisterWindowClass())
return NULL;
CreateWindow(GetClassName(), pstrName, dwStyle, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hParentWnd, NULL, hInst,this);
return m_hWnd;
}
bool CXUIWindow::RegisterWindowClass()
{
WNDCLASS wc = { 0 };
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = NULL;
wc.lpfnWndProc = WndProc;
wc.hInstance = m_hInst;
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = GetClassName();
::RegisterClass(&wc);
return true;
}
LRESULT CALLBACK CXUIWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
CXUIWindow* pThis = NULL;
if (message == WM_NCCREATE)
{
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pThis = static_cast<CXUIWindow*>(lpcs->lpCreateParams);
pThis->m_hWnd = hWnd;
::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis));
}
else
{
pThis = reinterpret_cast<CXUIWindow*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
if (message == WM_DESTROY)
::SetWindowLongPtr(hWnd, GWLP_USERDATA, 0);
}
if (pThis == NULL)
return ::DefWindowProc(hWnd, message, wParam, lParam);
else
return pThis->HandleMessage(hWnd, message, wParam, lParam);
}
LPCTSTR CXUIWindow::GetClassName()
{
return L"XUIWindow";
}
LRESULT CXUIWindow::HandleMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void CXUIWindow::ShowWindow(int nCmdShow)
{
::ShowWindow(m_hWnd, nCmdShow);
}
void CXUIWindow::UpdateWindow()
{
::UpdateWindow(m_hWnd);
}