.h
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
{
protected:
BOOL m_bTracking;
BOOL m_bCaptureEnabled;
CPoint m_ptFrom;
CPoint m_ptTo;
HCURSOR m_hCursorArrow;
HCURSOR m_hCursorIBeam;
void InvertLine(CDC *pDC,CPoint ptFrom,CPoint ptTo);
public:
CMainWindow();
protected:
afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
afx_msg void OnLButtonUp(UINT nFlags,CPoint point);
afx_msg void OnMouseMove(UINT nFlags,CPoint point);
afx_msg void OnNcLButtonDown(UINT nHitTest,CPoint point);
afx_msg void OnSize(UINT nType,int cx,int cy);
afx_msg BOOL OnSetCursor(CWnd* pWnd,UINT nHitTest,UINT message);
DECLARE_MESSAGE_MAP()
};
.cpp
#include <afxwin.h>
#include "MouseCap.h"
CMyApp myApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_NCLBUTTONDOWN()
ON_WM_SIZE()
ON_WM_SETCURSOR()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
m_hCursorArrow=::LoadCursor(NULL,IDC_ARROW);
m_hCursorIBeam=::LoadCursor(NULL,IDC_IBEAM);
m_bTracking=FALSE;
m_bCaptureEnabled=TRUE;
//Register a WNDCLASS
CString strWndClass=AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_CROSS),(HBRUSH)(COLOR_WINDOW+1),AfxGetApp()->LoadStandardIcon(IDI_WINLOGO));
//Create a window
Create(strWndClass,_T("Mouse Capture Demo(Capture Enabled)"));
}
void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
{
m_ptFrom=point;
m_ptTo=point;
m_bTracking=TRUE;
if(m_bCaptureEnabled)
{
SetCapture();
}
}
void CMainWindow::OnMouseMove(UINT nFlags,CPoint point)
{
if(m_bTracking)
{
CClientDC dc(this);
InvertLine(&dc,m_ptFrom,m_ptTo);
InvertLine(&dc,m_ptFrom,point);
m_ptTo=point;
}
}
void CMainWindow::OnLButtonUp(UINT nFlags,CPoint point)
{
if(m_bTracking)
{
m_bTracking=FALSE;
if(GetCapture()==this)
::ReleaseCapture();
CClientDC dc(this);
InvertLine(&dc,m_ptFrom,m_ptTo);
CPen pen(PS_SOLID,16,RGB(255,0,0));
dc.SelectObject(&pen);
dc.MoveTo(m_ptFrom);
dc.LineTo(point);
}
}
void CMainWindow::OnNcLButtonDown(UINT nHitTest,CPoint point)
{
if(nHitTest==HTCAPTION)
{
m_bCaptureEnabled=m_bCaptureEnabled?FALSE:TRUE;
SetWindowText(m_bCaptureEnabled?_T("Mouse Capture Demo(Capture Enabled)"):_T("Mouse Capture Demo(Capture Disabled)"));
}
CFrameWnd::OnNcLButtonDown(nHitTest,point);
}
void CMainWindow::InvertLine(CDC *pDC,CPoint ptFrom,CPoint ptTo)
{
int nOldMode=pDC->SetROP2(R2_NOT);
pDC->MoveTo(ptFrom);
pDC->LineTo(ptTo);
pDC->SetROP2(nOldMode);
}
void CMainWindow::OnSize(UINT nType,int cx,int cy)
{
CFrameWnd::OnSize(nType,cx,cy);
}
BOOL CMainWindow::OnSetCursor(CWnd *pWnd,UINT nHitTest,UINT message)
{
if(nHitTest==HTCLIENT)
{
DWORD dwPos=::GetMessagePos();
CPoint point(LOWORD(dwPos),HIWORD(dwPos));
ScreenToClient(&point);
CRect rect;
GetClientRect(&rect);
::SetCursor((point.y<rect.Height()/2)?m_hCursorArrow:m_hCursorIBeam);
return TRUE;
}
}
//DEL void CMainWindow::OnPaint()
//DEL {
//DEL CFrameWnd::OnPaint();
//DEL }
很明显,代码中并没有处理OnPaint,但是当我点击窗口最大化,移动边框让窗口变大或变小的时候客户区中的图形仍然是存在的,并没有重画而导致消失。。。。难道是没有触发WM_PAINT消息?
当客户区中的图形被遮挡住再移开图形确消失了。
WINDOWS程序设计书中有这样的话语:
在发生下面几种事件之一时,窗口消息处理程序会接收到一个WM_PAINT消息:
在使用者移动窗口或显示窗口时,窗口中先前被隐藏的区域重新可见。
使用者改变窗口的大小(如果窗口类别样式有着CS_HREDRAW和CS_VREDRAW位旗标的设定)。
程序使用ScrollWindow或ScrollDC函数滚动显示区域的一部分。
程序使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息。
这里最大化,移动边框等等操作导致了WM_SIZE,按照书上所说也应该触发WM_PAINT消息才对,然后客户区重画,图形消失。然而事实并非如此,所以怀疑MFC对这个做了特殊处理吗?还是?特此求助,再附上一个简单的SDK例子吧:
// 3.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY3, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY3);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY3);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY3;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
[COLOR="red"]case WM_LBUTTONDOWN:
{
HDC hdc=GetDC(hWnd);
MoveToEx(hdc,20,30,NULL);
LineTo(hdc,300,400);
ReleaseDC(hWnd,hdc);
}
break;[/COLOR] case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
这个SDK中点击最大化,移动边框让窗口变大变小都会导致客户区的图形消失的。
对于上述问题该如何解释呢?
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课