怎样用VC++的MFC画矩形?
很急!!谢谢!!
正确答案有追加分数!!
我需要详细的源代码!~谢谢! 展开
相应WM_PAINT消息,然后在里面加入绘制矩形的代码。
例如:
CClientDC dc(this);//选定当前画图环境
CPen pen(PS_SOLID,1,#ff0000);//做一支红色粗细为1的笔
dc.SelectObject(&pen);//将pen放到dc上
dc.Rectangle(100,100,200,200);//画一个矩形
参考资料:
我不使用mfc,所以给你做了个Win32 C程序的例子,把这个理解了,mfc就是小儿科,
找个文档查询下对应的mfc类就可以完成你的目标了。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow) {
static TCHAR szAppName[] = TEXT("Rectangle");
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
if (!RegisterClass(&wndClass)) {
MessageBox(NULL, TEXT("This Program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hWnd = CreateWindow (szAppName, TEXT("Device Capabilities"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static int cxChar, cyChar, cxCaps;
TCHAR szBuffer[10];
HDC hdc;
int i;
PAINTSTRUCT ps;
switch (message) {
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Rectangle(hdc, 10, 10, 50, 200);//
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
效果图:
//WM_LBUTTONDOWN消息响应函数
void CMFCView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
this->m_OrigPoint=point;
CView::OnLButtonDown(nFlags, point);
}
//WM_LBUTTONUP消息响应函数
void CMFCView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.Rectangle(CRect(m_OrigPoint,point));
CView::OnLButtonUp(nFlags, point);
}
2、在OnMouseMove里来判断按下状态并调用CDC的方法Rectangle来画矩形。