我正在创建一个用于学习的应用程序。我有一个用于创建窗口的类。 我正在创建一个单独的类,可以帮助在窗口上绘制不同的形状。但是,我无法在窗户上画画。当我们将所有代码保存在同一个类中时,它工作正常。但我想把课程分开。
这是代码: MainFrame.h
#include "afxwin.h"
class CMainFrame : public CFrameWnd
{
public:
CMainFrame(void);
~CMainFrame(void);
static CRect GetClientRectangle();
protected:
int OnCreate(LPCREATESTRUCT);
void OnShowWindow(BOOL, UINT);
void OnActivate(UINT, CWnd*, BOOL);
void OnPaint();
void OnSize(UINT, int, int);
void OnSizing(UINT, LPRECT);
void OnMove(int, int);
void OnMoving(UINT, LPRECT);
void OnClose();
void OnDestroy();
void OnLButtonUp(UINT, CPoint);
void DrawRectangle(CDC*);
DECLARE_MESSAGE_MAP()
};
MainFrame.cpp
#include "MainFrame.h"
#include "Shapes.h"
#include <sstream>
CMainFrame::CMainFrame(void)
{
Create(NULL, _T("Windows Application Tester"),WS_OVERLAPPEDWINDOW,CRect(120,100,700,480),NULL);
}
CMainFrame::~CMainFrame(void)
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// Call the base class to create the window
if (CFrameWnd::OnCreate(lpCreateStruct) == -1) {
MessageBox(_T("The window was not created!!!"));
return -1;
}
else {
MessageBox(_T("The window has been created!!!"));
return 0;
}
}
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWnd::OnShowWindow(bShow, nStatus);
// TODO: Add your message handler code here
//ShowWindow(SW_MAXIMIZE);
}
void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
// TODO: Add your message handler code here
switch( nState )
{
case WA_ACTIVE:
SetWindowText(_T("This window has been activated, without the mouse!"));
break;
case WA_INACTIVE:
SetWindowText(_T("This window has been deactivated and cannot be changed now!!"));
break;
case WA_CLICKACTIVE:
SetWindowText(_T("This window has been activated using the mouse!!!"));
break;
}
}
void CMainFrame::OnPaint()
{
CFrameWnd::OnPaint();
CShapes shape;
CPaintDC dc(this);
CRect client;
GetClientRect(&client);
shape.DrawRectangle(&dc, client);
SetWindowText(_T("The window has been painted<==>"));
}
void CMainFrame::OnSize(UINT nType, int cx, int cy) //nType-> window state; cx,cy->new width,height
{
CFrameWnd::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
char *MsgToShow = new char[20];
char *MsgCoord = new char[20];
switch(nType) {
case SIZE_MINIMIZED:
strcpy(MsgToShow, "Minimized ");
break;
case SIZE_MAXIMIZED:
strcpy(MsgToShow, "Maximized ");
break;
case SIZE_RESTORED:
strcpy(MsgToShow, "Restored ");
break;
case SIZE_MAXHIDE:
strcpy(MsgToShow, "Maximized Not Me ");
break;
case SIZE_MAXSHOW:
strcpy(MsgToShow, "Restored Not Me ");
break;
}
sprintf(MsgCoord, "Left = %d | Top = %d", cx, cy);
strcat(MsgToShow, MsgCoord);
SetWindowText(CString(MsgToShow));
}
void CMainFrame::OnSizing(UINT nType, LPRECT lpRect)
{
CFrameWnd::OnSizing(nType,lpRect);
// TODO: Add your message handler code here
SetWindowText(_T("Resized"));
}
void CMainFrame::OnMove(int x, int y)
{
CFrameWnd::OnMove(x, y);
// TODO: Add your message handler code here
std::stringstream ss;
ss<<"Window moved to " << x << ", " << y;
std::string s = ss.str();
SetWindowText(CString(s.c_str()));
}
void CMainFrame::OnMoving(UINT nSide, LPRECT lpRect)
{
CFrameWnd::OnMoving(nSide, lpRect);
// TODO: Add your message handler code here
std::stringstream ss;
ss<<"Window moving to " << lpRect->top << ", " << lpRect->left;
std::string s = ss.str();
SetWindowText(CString(s.c_str()));;
}
void CMainFrame::OnClose()
{
CFrameWnd::OnClose();
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
MessageBox(_T("Window Destroyed"));
// TODO: Add your message handler code here
}
void CMainFrame::OnLButtonUp(UINT nFlags, CPoint point) {
CFrameWnd::OnLButtonUp(nFlags, point);
MessageBox(_T("Hello"));
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_WM_CREATE()
ON_WM_SHOWWINDOW()
ON_WM_ACTIVATE()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_SIZING()
ON_WM_MOVE()
ON_WM_MOVING()
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
这是形状部分:
Shape.h
class CShapes
{
public:
CShapes(void);
~CShapes(void);
//void DrawArc(CDC*);
void DrawRectangle(CDC*, CRect);
/*void DrawRoundRectangle(CDC*);
void DrawEllipse(CDC*);
void DrawChord(CDC*);*/
};
Shape.cpp
#include "Shapes.h"
CShapes::CShapes(void)
{
}
CShapes::~CShapes(void)
{
}
void CShapes::DrawRectangle(CDC* dc, CRect client)
{
dc->DrawText(_T("Rectangle with 1 call to CDC::Rectangle()"),-1,&client,
DT_BOTTOM | DT_SINGLELINE | DT_CENTER );
client.DeflateRect(100,100);
dc->Rectangle(&client);
}
这是Main.cpp文件,它创建用于初始化窗口的对象。
#include "MainFrame.h"
class CIntroToMFCApp : public CWinApp {
BOOL InitInstance()
{
CMainFrame *Frame = new CMainFrame();
m_pMainWnd = Frame;
Frame->ShowWindow(SW_NORMAL);
Frame->UpdateWindow();
//AfxGetMainWnd()->PostMessageW(WM_CLOSE);
return TRUE;
}
};
CIntroToMFCApp MyApplication;
请告诉我需要进行更改的地方!
答案 0 :(得分:2)
编辑:回答重写
好的,我在VS2010中使用全新的MFC项目进行了快速测试。
取消行
CFrameWnd::OnPaint();
有了它,没有文字/矩形。 没有它,text / recatngle。
当您使用“向导”生成项目时,您会注意到OnPaint处理程序包含:
// Do not call CWnd::OnPaint() for painting messages
答案 1 :(得分:0)
在调用CDC :: Rectangle之前,用笔绘制矩形并填充您选择的画笔。但是你还没有提供这些。这里有一个例子:
http://msdn.microsoft.com/en-us/library/vstudio/8w4fzfxf.aspx
DrawText会遇到类似的问题,在绘图之前没有设置文本颜色或背景模式。
我不确定IInspectable提出的问题,但是注释掉对CFrameWnd :: OnPaint()的调用不会造成任何伤害。它无论是无用的还是有害的。