firewing
一般會員
發表:23 回覆:47 積分:19 註冊:2002-12-13
發送簡訊給我
|
我寫了以下的程式,發覺有 WM_PAINT 事件後,視窗會被清除,不會自動重畫,請問為何位如此呢?那該如何解決,謝謝各位的指導.
#include
//*.cpp
#pragma hdrstop #include "Unit1.h"
//--------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
endpoints=0;
pendown=0;
StartX=0;
StartY=0;
CurX=0;
CurY=0;
EndX=0;
EndY=0;
theta=0;
wflag=0;
pMode=R2_BLACK;
hCurrentPen=this->Canvas->Pen;
hRedPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
hGreenPen=CreatePen(PS_SOLID,1,RGB(0,255,0));
hBluePen=CreatePen(PS_SOLID,1,RGB(0,0,255));
maxX=GetSystemMetrics(SM_CXSCREEN);
maxY=GetSystemMetrics(SM_CYSCREEN);
hdc=GetDC(this->Handle);
memdc=CreateCompatibleDC(hdc);
hbit=CreateCompatibleBitmap(hdc,maxX,maxY);
SelectObject(memdc,hbit);
SelectObject(memdc,hCurrentBrush);
PatBlt(memdc,0,0,maxX,maxY,PATCOPY);
SelectObject(memdc,hCurrentPen);
ReleaseDC(this->Handle,hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if(Button==mbRight){
endpoints=1;
StartX=X;
StartY=Y;
CurX=X;
CurY=Y;
eX=StartX;
eY=StartY;
}
else if(Button==mbLeft){
pendown=1;
StartX=X;
StartY=Y;
CurX=X;
CurY=Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if(Button==mbRight){
endpoints=0;
EndX=X;
EndY=Y;
}
else if(Button==mbLeft){
pendown=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
Label1->Caption="(X:" IntToStr(X) "," "Y:" IntToStr(Y) ")";
if(pendown){
hdc=GetDC(this->Handle);
SelectObject(hdc,hCurrentPen);
MoveToEx(memdc,StartX,StartY,NULL);
MoveToEx(hdc,StartX,StartY,NULL);
LineTo(memdc,X,Y);
LineTo(hdc,X,Y);
StartX=X;
StartY=Y;
ReleaseDC(this->Handle,hdc);
}
if(endpoints){
hdc=GetDC(this->Handle);
hOldPen=(HPEN)SelectObject(hdc,hRedPen);
pMode=SetROP2(hdc,R2_NOTXORPEN);
hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(HOLLOW_BRUSH));
Rectangle(hdc,StartX,StartY,eX,eY);
Rectangle(hdc,StartX,StartY,X,Y);
SelectObject(hdc,hOldBrush);
SelectObject(hdc,hOldPen);
SetROP2(hdc,pMode);
eX=X;
eY=Y;
ReleaseDC(this->Handle,hdc);
} }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
int i;
static int j=0;
if(wflag){
hdc=BeginPaint(this->Handle,&paintstruct);
i=SetGraphicsMode(hdc,GM_ADVANCED);
rect=(RECT)GetClientRect();
tf.eM11=5;//(float)cos(theta*3.1416/180);
tf.eM12=5;//(float)sin(theta*3.1416/180);
tf.eM21=0;//(float)-sin(theta*3.1416/180);
tf.eM22=0;//(float)cos(theta*3.1416/180);
tf.eDx=(float)rect.right/2;
tf.eDy=(float)rect.bottom/2;
i=SetWorldTransform(hdc,&tf);
i=BitBlt(hdc,-rect.right/2,-rect.bottom/2,rect.right,rect.bottom,memdc,0,0,SRCCOPY);
EndPaint(this->Handle,&paintstruct);
j ;
Label2->Caption="repaint:" IntToStr(j);
}
wflag=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
theta-=30;
wflag=1;
InvalidateRect(this->Handle,NULL,0); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
theta =30;
wflag=1;
InvalidateRect(this->Handle,NULL,1); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
wflag=1;
InvalidateRect(this->Handle,NULL,1); }
//--------------------------------------------------------------------
//--------------------------------------------------------------------//*.h #ifndef Unit1H
#define Unit1H
//--------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include <math.h> //--------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TButton *Button1;
TButton *Button2;
TButton *Button3;
TLabel *Label2;
void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift, int X,
int Y);
void __fastcall FormPaint(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
int StartX,StartY,CurX,CurY,EndX,EndY,eX,eY;
int pendown,endpoints;
int pMode,theta,wflag;
int maxX,maxY;
HDC hdc,memdc;
HPEN hCurrentPen,hOldPen;
HPEN hRedPen,hGreenPen,hBluePen;
HBRUSH hOldBrush,hCurrentBrush;
HBITMAP hbit;
XFORM tf;
RECT rect;
PAINTSTRUCT paintstruct;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
|