制作自定义窗口类
存在的问题:
制作的窗口不能移动
如果要改进,可以参见“无标题窗口的移动”
在...App的 InitInstance() 中添加以下内容
BOOL ...App::InitInstance()
{
// TODO: 在此执行任意逐线程初始化
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup( &gdiplusToken , &gdiplusStartupInput , NULL );
this->m_pMainWnd = new ...Wnd();
this->m_pMainWnd->ShowWindow( this->m_nCmdShow );
this->m_pMainWnd->UpdateWindow();
// 注意一定要 return TRUE;
// 否则的话,程序就要退出啦
return TRUE;
}
// 自定义窗口的实现
// ...Wnd.h
#pragma once
// ...Wnd
class ...Wnd : public CWnd
{
DECLARE_DYNAMIC(puzzleWnd)
public:
...Wnd();
virtual ~...Wnd();
protected:
DECLARE_MESSAGE_MAP()
public:
void userCreate();
public:
afx_msg void OnPaint();
};
// ...Wnd.cpp : 实现文件
//
#include "stdafx.h"
#include "...App.h"
#include "Resource.h"
#include "./...wnd.h"
// ...Wnd
IMPLEMENT_DYNAMIC(...Wnd, CWnd)
...Wnd::...Wnd()
{
this->userCreate();
}
puzzleWnd::~puzzleWnd()
{
}
BEGIN_MESSAGE_MAP(puzzleWnd, CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
// 用用户定义函数
//建立窗口
void ...Wnd::userCreate()
{
char szTitle[] = "窗口标题";
CreateEx(
0 ,
::AfxRegisterWndClass(
0 ,
::LoadCursor( NULL , IDC_ARROW ) ,
NULL ,
::LoadIcon( NULL , MAKEINTRESOURCE( IDI_ICONWND) )
) ,
szTitle ,
WS_POPUP ,
200 , 200 , mainWindowWidth , mainWindowHeight ,
NULL ,
NULL
);
}
// ...Wnd 消息处理程序
// WM_PAINT消息处理函数
void ...Wnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CWnd::OnPaint()
Graphics graphics( dc.m_hDC );
//主界面背景色,为 #6699ff 到 #ffffff 线性渐变色
LinearGradientBrush RtoWBrush(
Point( 0 , 0 ) ,
Point( mainWindowWidth * 11 / 10 , 0 ) ,
Color( 255 , 0x66 , 0x99 , 0xff ) ,
Color( 255 , 0xff , 0xff , 0xff )
);
graphics.FillRectangle( &RtoWBrush , 0 , 0 , mainWindowWidth , mainWindowHeight );
}
//