多线程下得invalidaterect和屏幕坐标问题??

CyberLogix 2005-03-24 10:54:36
多线程下得invalidaterect和屏幕坐标问题??

// MultiThreadDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiThreadDemo.h"
#include "MultiThreadDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

typedef struct _tagTHREADINFO
{
HWND hWnd;
long speed;
long xPos;
long yPos;
long ballRadius;
CRect rect;
}_tagTHREADINFO, *PTHREADINFO;

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiThreadDemoDlg dialog

CMultiThreadDemoDlg::CMultiThreadDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMultiThreadDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMultiThreadDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_IsBlue = m_IsGreen = m_IsRed = FALSE;
}

void CMultiThreadDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMultiThreadDemoDlg)
DDX_Control(pDX, IDC_MOVEBOX, m_MoveBox);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultiThreadDemoDlg, CDialog)
//{{AFX_MSG_MAP(CMultiThreadDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonEnd)
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiThreadDemoDlg message handlers

BOOL CMultiThreadDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

DWORD WINAPI BallMove(LPVOID lpParam)
{
PTHREADINFO pThreadInfo = (PTHREADINFO)lpParam;
BOOL xAdd, yAdd;
xAdd = yAdd =TRUE;
while(TRUE)
{
if((pThreadInfo->xPos+pThreadInfo->ballRadius)>=((LPRECT)pThreadInfo->rect)->right)
xAdd = FALSE;
else if((pThreadInfo->xPos-pThreadInfo->ballRadius)<=((LPRECT)pThreadInfo->rect)->left)
xAdd = TRUE;
if((pThreadInfo->yPos + pThreadInfo->ballRadius)>=((LPRECT)pThreadInfo->rect)->bottom)
yAdd = FALSE;
else if((pThreadInfo->yPos - pThreadInfo->ballRadius)<=((LPRECT)pThreadInfo->rect)->top)
yAdd = TRUE;
if(xAdd)
pThreadInfo->xPos++;
else
pThreadInfo->xPos--;
if(yAdd)
pThreadInfo->yPos++;
else
pThreadInfo->yPos--;
//CPoint point(MAKEWORD(pThreadInfo->xPos,pThreadInfo->yPos));
//ClientToScreen(pThreadInfo->hWnd, &point);
CRect rect(pThreadInfo->xPos-pThreadInfo->ballRadius-2,pThreadInfo->yPos-pThreadInfo->ballRadius-2,pThreadInfo->xPos+pThreadInfo->ballRadius+2,pThreadInfo->yPos+pThreadInfo->ballRadius+2);
//CMultiThreadDemoDlg* pMainWnd = (CMultiThreadDemoDlg*)AfxGetMainWnd();
//ScreenToClient(pThreadInfo->hWnd,rect);
//pMainWnd->ScreenToClient(rect);
InvalidateRect(pThreadInfo->hWnd,rect,TRUE);
Sleep(pThreadInfo->speed);
}
return 0;
}

void CMultiThreadDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMultiThreadDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}

if(m_IsGreen)
{
CDC* pDC = m_MoveBox.GetDC();
CBrush brush(RGB(0,255,0));
CBrush* pOldBrush = pDC->SelectObject(&brush);
pDC->Ellipse(m_GreenBall.xPos-((LPRECT)m_GreenBall.rect)->left-m_GreenBall.ballRadius,
m_GreenBall.yPos-((LPRECT)m_GreenBall.rect)->top-m_GreenBall.ballRadius,
m_GreenBall.xPos-((LPRECT)m_GreenBall.rect)->left+m_GreenBall.ballRadius,
m_GreenBall.yPos-((LPRECT)m_GreenBall.rect)->top+m_GreenBall.ballRadius);
pDC->SelectObject(pOldBrush);
}
if(m_IsBlue)
{
CDC* pDC = m_MoveBox.GetDC();
CBrush brush(RGB(0,0,255));
CBrush* pOldBrush = pDC->SelectObject(&brush);
pDC->Ellipse(m_BlueBall.xPos-((LPRECT)m_BlueBall.rect)->left-m_BlueBall.ballRadius,
m_BlueBall.yPos-((LPRECT)m_BlueBall.rect)->top-m_BlueBall.ballRadius,
m_BlueBall.xPos-((LPRECT)m_BlueBall.rect)->left+m_BlueBall.ballRadius,
m_BlueBall.yPos-((LPRECT)m_BlueBall.rect)->top+m_BlueBall.ballRadius);
pDC->SelectObject(pOldBrush);
}
if(m_IsRed)
{
CDC* pDC = m_MoveBox.GetDC();
CBrush brush(RGB(255,0,0));
CBrush* pOldBrush = pDC->SelectObject(&brush);
pDC->Ellipse(m_RedBall.xPos-((LPRECT)m_RedBall.rect)->left-m_RedBall.ballRadius,
m_RedBall.yPos-((LPRECT)m_RedBall.rect)->top-m_RedBall.ballRadius,
m_RedBall.xPos-((LPRECT)m_RedBall.rect)->left+m_RedBall.ballRadius,
m_RedBall.yPos-((LPRECT)m_RedBall.rect)->top+m_RedBall.ballRadius);
pDC->SelectObject(pOldBrush);
}
}


BOOL CMultiThreadDemoDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class

return CDialog::Create(IDD, pParentWnd);
}

...全文
124 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CyberLogix 2005-03-24
  • 打赏
  • 举报
回复
这个程序运行一段时间后,屏幕的坐标就回出现问题,并且红色小求的显示有问题,我想是坐标转化的问题,可是不止到问题出现在那里???
CyberLogix 2005-03-24
  • 打赏
  • 举报
回复
void CMultiThreadDemoDlg::OnButtonStart()
{
DWORD dwThreadId;
DWORD dwCode;
CRect rcRect;
GetClientRect(rcRect);
ClientToScreen(rcRect);
m_MoveBox.GetClientRect(m_MoveRect);
m_MoveBox.ClientToScreen(m_MoveRect);
//m_MoveRect-= rcRect.TopLeft();

// create green ball thread
if(!m_IsGreen)
{
m_GreenBall.hWnd = m_hWnd;
m_GreenBall.ballRadius = 8;
m_GreenBall.speed = 10;
m_GreenBall.xPos = ((LPRECT)m_MoveRect)->left+m_GreenBall.ballRadius;
m_GreenBall.yPos = ((LPRECT)m_MoveRect)->top + m_GreenBall.ballRadius;
m_GreenBall.rect = m_MoveRect;
m_IsGreen = TRUE;
}
if(!GetExitCodeThread(m_hGreenThread,&dwCode)||dwCode!=STILL_ACTIVE)
m_hGreenThread = CreateThread(NULL,0,BallMove,&m_GreenBall,0,&dwThreadId);
if(m_hGreenThread ==NULL)
{
AfxMessageBox("Create green ball thread failed!");
m_IsGreen = FALSE;
}
// create blue ball thread
if(!m_IsBlue)
{
m_BlueBall.hWnd = m_hWnd;
m_BlueBall.ballRadius = 7;
m_BlueBall.speed = 15;
m_BlueBall.xPos = ((LPRECT)m_MoveRect)->left+m_BlueBall.ballRadius+30;
m_BlueBall.yPos = ((LPRECT)m_MoveRect)->top + m_BlueBall.ballRadius+10;
m_BlueBall.rect = m_MoveRect;
m_IsBlue = TRUE;
}
if(!GetExitCodeThread(m_hBlueThread,&dwCode)||dwCode!=STILL_ACTIVE)
m_hBlueThread = CreateThread(NULL,0,BallMove,&m_BlueBall,0,&dwThreadId);
if(m_hBlueThread ==NULL)
{
AfxMessageBox("Create blue ball thread failed!");
m_IsBlue = FALSE;
}
// create red ball thread
if(!m_IsRed)
{
m_GreenBall.hWnd = m_hWnd;
m_RedBall.ballRadius = 9;
m_RedBall.speed = 20;
m_RedBall.xPos = ((LPRECT)m_MoveRect)->left+m_RedBall.ballRadius+50;
m_RedBall.yPos = ((LPRECT)m_MoveRect)->top + m_RedBall.ballRadius+120;
m_RedBall.rect = m_MoveRect;
m_IsRed = TRUE;
}
if(!GetExitCodeThread(m_hRedThread,&dwCode)||dwCode!=STILL_ACTIVE)
m_hGreenThread = CreateThread(NULL,0,BallMove,&m_RedBall,0,&dwThreadId);
if(m_hRedThread ==NULL)
{
AfxMessageBox("Create red ball thread failed!");
m_IsRed = FALSE;
}
}

void CMultiThreadDemoDlg::OnButtonEnd()
{
DWORD dwCode;
if(GetExitCodeThread(m_hGreenThread,&dwCode))
if(dwCode == STILL_ACTIVE)
{
TerminateThread(m_hGreenThread,0);
CloseHandle(m_hGreenThread);
}


if(GetExitCodeThread(m_hBlueThread,&dwCode))
if(dwCode == STILL_ACTIVE)
{
TerminateThread(m_hBlueThread,0);
CloseHandle(m_hBlueThread);
}
if(GetExitCodeThread(m_hRedThread,&dwCode))
if(dwCode == STILL_ACTIVE)
{
TerminateThread(m_hRedThread,0);
CloseHandle(m_hRedThread);
}
}

void CMultiThreadDemoDlg::OnOK()
{
// TODO: Add extra validation here
OnButtonEnd();
CDialog::OnOK();
}

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧