|
설명(님이 기술한 내용과 비교하면서 보세요)
FindWindow()에서 구한 HWND는 Window의 Handle로 fmMain->Handle와 같습니다.
③의 경우 Handle은 님의 Form Class 내부이므로 fmMain->Handle와 동일합니다.
그러므로 ②, ③, ④는 동일한 Window의 DC를 얻어 냅니다.
다만 DC는 Window Handle처럼 유일하지 않기 때문에 그 값은 서로 다를 수 있습니다.
Canvas->Handle은 Window의 Handle이 아닌 Canvas에 대한 DC입니다.
그러므로 ⑤의 GetDC(fmMain->Canvas->Handle)은 성립될 수 없는 코드입니다.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "tHandle.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmMain *fmMain;
//---------------------------------------------------------------------------
__fastcall TfmMain::TfmMain(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::FormPaint(TObject *Sender)
{
static bool bNoErr = true;
HDC hDc = NULL;
HWND hWnd = FindWindow( NULL, "fmMain" ); // ①
// ② Window Handle을 찾아서 Ellipse 그리기
if( hWnd != NULL ) {
hDc = GetDC( hWnd );
if( hDc != NULL ) {
::Ellipse( hDc, 10, 10, 100, 100 );
::ReleaseDC( hWnd, hDc ); // GetDC()로 얻은 DC 사용이 끝나면 반환해야 합니다.
}
}
// ③ Handle로 Ellipse 그리기.
hDc = GetDC( Handle );
if( hDc != NULL ) {
::Ellipse( hDc, 110, 10, 200, 100 );
::ReleaseDC( hWnd, hDc );
}
// ④ Form->Handle로 Ellipse 그리기.
hDc = GetDC( fmMain->Handle );
if( hDc != NULL ) {
::Ellipse( hDc, 210, 10, 300, 100 );
::ReleaseDC( hWnd, hDc );
}
// ⑤ GetDC(fmMain->Canvas->Handle) 잘못된 조합입니다.
if( bNoErr ) {
hDc = GetDC(fmMain->Canvas->Handle);
if( hDc == NULL ) {
bNoErr = false;
ShowMessage( "GetDC(fmMain->Canvas->Handle)는 안되는 조합임" );
}
else {
::ReleaseDC( fmMain->Canvas->Handle, hDc );
}
}
// ⑥ Canvas->Handle로 Ellipse 그리기.
// Canvas->Handle은 TCanvas에서 관리하므로 반환하지 않습니다.
// 실제 TCanvas내부엔 아래와 유사한 코드가 들어 있습니다.
::Ellipse( Canvas->Handle, 310, 10, 400, 100 );
// ⑦ Canvas에 Ellipse 그리기.
Canvas->Ellipse( 410, 10, 500, 100 );
}
//---------------------------------------------------------------------------
김종기 님이 쓰신 글 :
: HWND hWnd;
: HDC hdc;
:
: ① hWnd = FindWindow(NULL,"fmMain"); <--- 여기와 비교해서 보세요.
: ② hdc = GetDC(hWnd);
: ③ hdc = GetDC(Handle);
: ④ hdc = GetDC(fmMain->Handle);
: ⑤ hdc = GetDC(fmMain->Canvas->Handle);
:
: ①해서 ②하면 윈 핸들과 DC핸들이 구해지는데
: 생략하고 ③, ④, ⑤을 하면 실행중 에러가 발생해요.
: 물론 컴파일,링크 에러는 없구요.
:
: 이유를 알고 싶습니다.
:
: * 어디서 실행프로그램의 핸들은 폼의 핸들이라고 들었는데(?) 아닌가요.
:
: 고수님 한말씀 부탁해요...
:
|