subject_17100.htm
来自「一些关于vc的问答」· HTM 代码 · 共 144 行 · 第 1/2 页
HTM
144 行
<blockquote><p>
回复者:liuwenjun 回复日期:2002-10-08 22:00:44
<br>内容:以前有位朋友提过用CWnd::Invalidate函数,我也试过,不过刚才我又试了一下,结果都是一闪一闪的,并且画不出完整的线条。<BR>另外,在OnDraw()OnPaint()进行刷新要如何刷新呢?<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-09 07:10:43
<br>内容:CWnd::InvalidateRect只刷新应该刷新的<BR>void InvalidateRect( LPCRECT lpRect, BOOL bErase = TRUE );<BR><BR>Parameters<BR><BR><BR>void xxxx::OnDraw()(OnPatint()<BR>{<BR><BR> 你画图的程序<BR><BR><BR>}<BR>OnTimer()<BR>{<BR><BR> 修改化图的变量<BR> 计算需要刷新的rect<BR> InvalidateRect(rect);//自动会发Message调用OnDraw()<BR> <BR><BR><BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-09 07:11:43
<br>内容:给点分,鼓励一下
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:luo 回复日期:2002-10-09 11:09:19
<br>内容:我不知道你除了画那个图之外还干什么,干脆我就给你发一份我的画那个图的程序吧!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:liuwenjun 回复日期:2002-10-09 16:23:33
<br>内容:一。照黄飚朋友的指点,我将画图代码放入了OnPatint()中(因为我的程序是基于对话框的,没有OnDraw()函数),再在OnTimer()函数中调用<BR>InvalidateRect函数,结果没有线了。<BR>二。luo,你给我的文件在VC中打开后不是源代码形式,而是<BR>000030 10 00 00 00 54 65 73 74 30 07 74 20 80 2D 00 CA <BR>这样的形式,不知为什么。<BR>另外有谁知道,网上哪里有能实现:显示完图像(不要以位图形式画,而是用CDC类指针画)后自动擦除原图像然后紧接着再画另一幅图像功能的源代码,我下载了一个“贝赛尔纽曼函数绘图“的程序,但其中擦除功能是用按钮实现的,他也是用矩形将<BR>画图区域重涂一遍,但我以前试过这种方法,画不出线。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-09 20:11:25
<br>内容:那你给我代码吧,
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-09 20:36:44
<br>内容:你的问题可能是出在dc上。你应该使用CCLIENTDC创建自己的dc,而不是用CPAINTDC得到的dc
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:luo 回复日期:2002-10-10 12:36:00
<br>内容:那是rar文件,你应该先解开到一个目录下,在用vc打开才行。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-11 12:35:24
<br>内容:// DrawLineDlg.cpp : implementation file<BR>//<BR><BR>#include "stdafx.h"<BR>#include "DrawLine.h"<BR>#include "DrawLineDlg.h"<BR><BR>#ifdef _DEBUG<BR>#define new DEBUG_NEW<BR>#undef THIS_FILE<BR>static char THIS_FILE[] = __FILE__;<BR>#endif<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>// CAboutDlg dialog used for App About<BR><BR>class CAboutDlg : public CDialog<BR>{<BR>public:<BR> CAboutDlg();<BR><BR>// Dialog Data<BR> //{{AFX_DATA(CAboutDlg)<BR> enum { IDD = IDD_ABOUTBOX };<BR> //}}AFX_DATA<BR><BR> // ClassWizard generated virtual function overrides<BR> //{{AFX_VIRTUAL(CAboutDlg)<BR> protected:<BR> virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support<BR> //}}AFX_VIRTUAL<BR><BR>// Implementation<BR>protected:<BR> //{{AFX_MSG(CAboutDlg)<BR> //}}AFX_MSG<BR> DECLARE_MESSAGE_MAP()<BR>};<BR><BR>CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)<BR>{<BR> //{{AFX_DATA_INIT(CAboutDlg)<BR> //}}AFX_DATA_INIT<BR>}<BR><BR>void CAboutDlg::DoDataExchange(CDataExchange* pDX)<BR>{<BR> CDialog::DoDataExchange(pDX);<BR> //{{AFX_DATA_MAP(CAboutDlg)<BR> //}}AFX_DATA_MAP<BR>}<BR><BR>BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)<BR> //{{AFX_MSG_MAP(CAboutDlg)<BR> // No message handlers<BR> //}}AFX_MSG_MAP<BR>END_MESSAGE_MAP()<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>// CDrawLineDlg dialog<BR><BR>CDrawLineDlg::CDrawLineDlg(CWnd* pParent /*=NULL*/)<BR> : CDialog(CDrawLineDlg::IDD, pParent)<BR>{<BR> //{{AFX_DATA_INIT(CDrawLineDlg)<BR> // NOTE: the ClassWizard will add member initialization here<BR> //}}AFX_DATA_INIT<BR> // Note that LoadIcon does not require a subsequent DestroyIcon in Win32<BR> m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);<BR>}<BR><BR>void CDrawLineDlg::DoDataExchange(CDataExchange* pDX)<BR>{<BR> CDialog::DoDataExchange(pDX);<BR> //{{AFX_DATA_MAP(CDrawLineDlg)<BR> // NOTE: the ClassWizard will add DDX and DDV calls here<BR> //}}AFX_DATA_MAP<BR>}<BR><BR>BEGIN_MESSAGE_MAP(CDrawLineDlg, CDialog)<BR> //{{AFX_MSG_MAP(CDrawLineDlg)<BR> ON_WM_SYSCOMMAND()<BR> ON_WM_PAINT()<BR> ON_WM_QUERYDRAGICON()<BR> ON_WM_TIMER()<BR> //}}AFX_MSG_MAP<BR>END_MESSAGE_MAP()<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>// CDrawLineDlg message handlers<BR><BR>BOOL CDrawLineDlg::OnInitDialog()<BR>{<BR> CDialog::OnInitDialog();<BR><BR> // Add "About..." menu item to system menu.<BR><BR> // IDM_ABOUTBOX must be in the system command range.<BR> ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);<BR> ASSERT(IDM_ABOUTBOX < 0xF000);<BR><BR> CMenu* pSysMenu = GetSystemMenu(FALSE);<BR> if (pSysMenu != NULL)<BR> {<BR> CString strAboutMenu;<BR> strAboutMenu.LoadString(IDS_ABOUTBOX);<BR> if (!strAboutMenu.IsEmpty())<BR> {<BR> pSysMenu->AppendMenu(MF_SEPARATOR);<BR> pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);<BR> }<BR> }<BR><BR> // Set the icon for this dialog. The framework does this automatically<BR> // when the application's main window is not a dialog<BR> SetIcon(m_hIcon, TRUE); // Set big icon<BR> SetIcon(m_hIcon, FALSE); // Set small icon<BR> <BR> // TODO: Add extra initialization here<BR> SetTimer(1,500,NULL);//设置定时器 <BR> CRect Rect;<BR> GetClientRect(&Rect);<BR> ScreenToClient(&Rect);<BR><BR> return TRUE; // return TRUE unless you set the focus to a control<BR>}<BR><BR>void CDrawLineDlg::OnSysCommand(UINT nID, LPARAM lParam)<BR>{<BR> if ((nID & 0xFFF0) == IDM_ABOUTBOX)<BR> {<BR> CAboutDlg dlgAbout;<BR> dlgAbout.DoModal();<BR> }<BR> else<BR> {<BR> CDialog::OnSysCommand(nID, lParam);<BR> }<BR>}<BR>void CDrawLineDlg::SetHeightArray()<BR>{<BR><BR> srand((unsigned)time(NULL));<BR> for(int i=0;i<10;i++)<BR> {<BR> CurHeightArray[i]=rand()%100;<BR> }//用随机数函数产生数据<BR><BR><BR>}<BR><BR>// If you add a minimize button to your dialog, you will need the code below<BR>// to draw the icon. For MFC applications using the document/view model,<BR>// this is automatically done for you by the framework.<BR><BR>void CDrawLineDlg::OnPaint() <BR>{<BR> CPaintDC dc(this); // device context for painting<BR><BR> if (IsIconic())<BR> {<BR> CPaintDC dc(this); // device context for painting<BR><BR> SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);<BR><BR> // Center icon in client rectangle<BR> int cxIcon = GetSystemMetrics(SM_CXICON);<BR> int cyIcon = GetSystemMetrics(SM_CYICON);<BR> CRect rect;<BR> GetClientRect(&rect);<BR> int x = (rect.Width() - cxIcon + 1) / 2;<BR> int y = (rect.Height() - cyIcon + 1) / 2;<BR><BR> // Draw the icon<BR> dc.DrawIcon(x, y, m_hIcon);<BR><BR> }<BR> else<BR> {<BR> CDialog::OnPaint();<BR> }<BR><BR> <BR> CPen mypen;<BR> // mypen.CreatePen(PS_SOLID,5,RGB(192,63,63));<BR>// 创建一个红色的补色(要实现红色就要定义其补色的画笔)实线笔<BR>// CPen *pOldPen;<BR>// pOldPen=dc.SelectObject(&mypen);<BR>// dc.BeginPath();<BR><BR> CRect rect; <BR> GetClientRect(&rect);//获得客户区大小<BR> <BR> int m_pointx,m_pointy;//用来标志画线的起始点<BR> int i=0;<BR> m_pointx=rect.left+20;<BR> m_pointy=rect.bottom-60;<BR>//画图代码:<BR> for(i=0;i<10;i++)<BR> {<BR> dc.MoveTo(m_pointx+15*i,m_pointy);<BR> dc.LineTo(m_pointx+15*i,CurHeightArray[i]);<BR> }<BR>}<BR><BR>// The system calls this to obtain the cursor to display while the user drags<BR>// the minimized window.<BR>HCURSOR CDrawLineDlg::OnQueryDragIcon()<BR>{<BR> return (HCURSOR) m_hIcon;<BR>}<BR><BR>void CDrawLineDlg::OnTimer(UINT nIDEvent) <BR>{<BR> // TODO: Add your message handler code here and/or call default<BR> SetHeightArray();//调用数据设置函数 <BR> Invalidate();<BR> CDialog::OnTimer(nIDEvent);<BR>}<BR>不知道解决你的问题没有,你看一下,另外说明
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-11 12:37:01
<br>内容:void CDrawLineDlg::OnPaint() 特别注意<BR>{<BR> if (IsIconic())<BR> {<BR> CPaintDC dc(this); // device context for painting<BR><BR> SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);<BR><BR> // Center icon in client rectangle<BR> int cxIcon = GetSystemMetrics(SM_CXICON);<BR> int cyIcon = GetSystemMetrics(SM_CYICON);<BR> CRect rect;<BR> GetClientRect(&rect);<BR> int x = (rect.Width() - cxIcon + 1) / 2;<BR> int y = (rect.Height() - cyIcon + 1) / 2;<BR><BR> // Draw the icon<BR> dc.DrawIcon(x, y, m_hIcon);<BR><BR> }<BR> else<BR> {<BR> CDialog::OnPaint();<BR> }<BR><BR> <BR> CPaintDC dc(this); // device context for painting<BR> <BR> CPen mypen;<BR>// mypen.CreatePen(PS_SOLID,5,RGB(192,63,63));<BR>// 创建一个红色的补色(要实现红色就要定义其补色的画笔)实线笔<BR>// CPen *pOldPen;<BR>// pOldPen=dc.SelectObject(&mypen);<BR>// dc.BeginPath();<BR><BR> CRect rect; <BR> GetClientRect(&rect);//获得客户区大小<BR> <BR> int m_pointx,m_pointy;//用来标志画线的起始点<BR> int i=0;<BR> m_pointx=rect.left+20;<BR> m_pointy=rect.bottom-60;<BR>//画图代码:<BR> for(i=0;i<10;i++)<BR> {<BR> dc.MoveTo(m_pointx+15*i,m_pointy);<BR> dc.LineTo(m_pointx+15*i,CurHeightArray[i]);<BR> }<BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:liuwenjun 回复日期:2002-10-11 21:19:50
<br>内容:太感谢黄飚和luo两位朋友了,您二位给我的答案给我提供完全满意的结果。但我在读程序时还有些疑问:<BR>两个程序共性的问题:<BR>1用随机函数,不用加入#include "time.h"吗?<BR>2OnTimer中的Invalidate();作用是指刚才调用的OnDraw/OnPaint函数的操作全部无效吗?其中的nIDEvent的意义是什么?<BR>3开始时所有的线均为最长,而不是均为0,我若想让他均为0应该怎么办<BR>4结果有闪烁<BR>5用SetTimer函数在哪个类处设置定时器,就会调用该类相对应的OnTimer函数吗?<BR>6二位的程序中都有dc.BeginPath();而未用,为什么该语句加入后,就画不出线,而且luo的程序中还用了pDC->EndPath();却也能画出线<BR>在黄飚朋友程序中:<BR>1为何将CPaintDC dc(this); 语句加入if{ }else{ }后就画不出线了呢?<BR>在luo的程序中:<BR>1.pOldPen = (CPen*)pDC->SelectObject( &mypen );<BR>pOldPen=pDC->SelectObject(&mypen);此两句的区别<BR>2.R2_NOP Pixel remains unchanged.是保持什么时候的颜色不变?<BR>3.OnInitialUpdate()成员函数是在视图窗口被创建之初被调用的?相当于对话框类中的OnInitialDialog()吗?<BR>4.为什么要设置布尔型变量m_bErase,除了在构造函数中设了一次为false外总是true,<BR><BR>另外,您二位的答案都令我非常满意,我想给二位分,但一个贴子的分只能给一个人,我想我再发个“关于动画”的贴子,您二位中的一位到那里去取我给的分,这样不违反论坛规则吧!因为我觉得您二位确实都绝对应该得到我这张贴子的分,您二位为我废了那么多心,我真不知该怎样感谢你们。<BR><BR>只能用一句谢谢来表达我的谢意了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-12 07:41:07
<br>内容:InvalidateRect()的第二个参数应设为false,这样值刷新图像,不刷新背景了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?