|
//---------------------------------------------------------------------------
//MAIN.CPP
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "Sub.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSDIMainForm *SDIMainForm;
//---------------------------------------------------------------------------
__fastcall TSDIMainForm::TSDIMainForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TSDIMainForm::WMUserMessage(TMessage &msg)
{
CreateSubForm();
}
//---------------------------------------------------------------------------
void __fastcall TSDIMainForm::CreateSubForm(void)
{
m_FormNumber++;
AnsiString str = "SDIアプリサンプル["+IntToStr(m_FormNumber)+"]";
TSDISubForm *sForm;
sForm = new TSDISubForm(this);
sForm->Show();
sForm->Caption = str;
}
//---------------------------------------------------------------------------
void __fastcall TSDIMainForm::FormCreate(TObject *Sender)
{
m_FormNumber = 0;
CreateSubForm();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------
//MAIN.H
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#define message WM_USER + 50
//---------------------------------------------------------------------------
class TSDIMainForm : public TForm
{
__published: // IDE 管理のコンポーネント
void __fastcall FormCreate(TObject *Sender);
private: // ユーザー宣言
void __fastcall WMUserMessage(TMessage &msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(message, TMessage, WMUserMessage)
END_MESSAGE_MAP(TForm);
public: // ユーザー宣言
__fastcall TSDIMainForm(TComponent* Owner);
int m_FormNumber;
void __fastcall CreateSubForm(void);
};
//---------------------------------------------------------------------------
extern PACKAGE TSDIMainForm *SDIMainForm;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//SUB.CPP
#include <vcl.h>
#pragma hdrstop
#include "Sub.h"
#include "main.h"
#include <Windows.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSDISubForm *SDISubForm;
//---------------------------------------------------------------------------
__fastcall TSDISubForm::TSDISubForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
bool __fastcall TSDISubForm::IsLastForm(TForm* form)
{
bool bRst = true;
TForm *sForm;
for(int idx = 0; idx < Screen->FormCount ; idx++)
{
sForm = Screen->Forms[idx];
if((sForm != form)&&(sForm->Visible == true)&&
(sForm->ParentWindow == 0 || sForm->HandleAllocated() || (IsChild(sForm->Handle,sForm->ParentWindow)==false))) {
bRst = false;
break;
}
}
return bRst;
}
void __fastcall TSDISubForm::subNewClick(TObject *Sender)
{
SDIMainForm->CreateSubForm();
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subCloseClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subAllCloseClick(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
// ウィンドウ復帰
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::RestoreWind(void)
{
int nWin = Screen->FormCount;
HWND hWnd;
for(int idx = 0; idx < Screen->FormCount; idx++)
{
hWnd = Screen->Forms[idx]->Handle;
//WindowがNormalではない場合
if(Screen->Forms[idx]->WindowState != wsNormal) {
if(GetClassNameStr(hWnd) == this->ClassName()) {
DefWindowProc(hWnd,WM_SYSCOMMAND,SC_RESTORE,0);
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subCascadeClick(TObject *Sender)
{
RestoreWind();
int nWin = Screen->FormCount;
HWND *WndHandleList;
WndHandleList = new HWND[nWin];
for(int idx = 0; idx < Screen->FormCount; idx++)
{
WndHandleList[idx] = Screen->Forms[idx]->Handle;
SetForegroundWindow(WndHandleList[idx]);
}
CascadeWindows(0,
NULL,//MDITILE_SKIPDISABLED
NULL,
Screen->FormCount,
WndHandleList);
SetForegroundWindow(Handle);
delete [] WndHandleList;
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subTileVerticalClick(TObject *Sender)
{
RestoreWind();
int nWin = Screen->FormCount;
HWND *WndHandleList;
WndHandleList = new HWND[nWin];
for(int idx = 0; idx < Screen->FormCount; idx++)
{
WndHandleList[idx] = Screen->Forms[idx]->Handle;
SetForegroundWindow(WndHandleList[idx]);
}
TileWindows( 0,
MDITILE_VERTICAL,//MDITILE_SKIPDISABLED
NULL,
Screen->FormCount,
WndHandleList
);
delete [] WndHandleList;
SetForegroundWindow(Handle);
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subTileHorizontalClick(TObject *Sender)
{
RestoreWind();
int nWin = Screen->FormCount;
HWND *WndHandleList;
WndHandleList = new HWND[nWin];
for(int idx = 0; idx < Screen->FormCount; idx++)
{
WndHandleList[idx] = Screen->Forms[idx]->Handle;
SetForegroundWindow(WndHandleList[idx]);
}
TileWindows(0,
MDITILE_HORIZONTAL,//MDITILE_SKIPDISABLED
NULL,
Screen->FormCount,
WndHandleList
);
delete [] WndHandleList;
SetForegroundWindow(Handle);
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::subAllMinClick(TObject *Sender)
{
HWND hWindow;
for(int idx = 0; idx < Screen->FormCount; idx++)
{
hWindow = Screen->Forms[idx]->Handle;
if(GetClassNameStr(hWindow) == this->ClassName()) {
PostMessage(hWindow,WM_SYSCOMMAND,SC_MINIMIZE,0);
}
}
}
//---------------------------------------------------------------------------
String __fastcall TSDISubForm::GetClassNameStr(HWND hWindow)
{
int Len;
char *Buffer;
String Result;
Buffer = StrAlloc(MAX_PATH);
try {
Len = GetClassName(hWindow,Buffer,StrBufSize(Buffer));
Result = Buffer;
}__finally{
StrDispose(Buffer);
return Result;
}
}
void __fastcall TSDISubForm::FormClose(TObject *Sender,
TCloseAction &Action)
{
if(IsLastForm(this)) {
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::FormShow(TObject *Sender)
{
ShowWindow(Application->Handle,SW_HIDE);
if(!NotFirstForm) {
NotFirstForm = true;
switch(CmdShow)
{
case SW_SHOWMINNOACTIVE:
WindowState = wsMinimized;
break;
case SW_SHOWMAXIMIZED:
WindowState = wsMaximized;
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TSDISubForm::CreateParams(TCreateParams &Params)
{
TForm::CreateParams(Params);
if(FormStyle == fsNormal || FormStyle == fsStayOnTop) {
if(BorderStyle == bsSingle || BorderStyle == bsSizeable) {
Params.ExStyle = Params.ExStyle |WS_EX_APPWINDOW;
Params.WndParent = 0;
}
}
}
//SUB.h
//---------------------------------------------------------------------------
#ifndef SubH
#define SubH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TSDISubForm : public TForm
{
__published: // IDE 管理のコンポーネント
TMainMenu *MainMenu1;
TMenuItem *MenuFile;
TMenuItem *subNew;
TMenuItem *subClose;
TMenuItem *subAllClose;
TMenuItem *N3;
TMenuItem *Show1;
TMenuItem *subCascade;
TMenuItem *subTileVertical;
TMenuItem *subTileHorizontal;
TMenuItem *subAllMin;
void __fastcall subNewClick(TObject *Sender);
void __fastcall subCloseClick(TObject *Sender);
void __fastcall subAllCloseClick(TObject *Sender);
void __fastcall subCascadeClick(TObject *Sender);
void __fastcall subTileVerticalClick(TObject *Sender);
void __fastcall subTileHorizontalClick(TObject *Sender);
void __fastcall subAllMinClick(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall FormShow(TObject *Sender);
private: // ユーザー宣言
bool __fastcall TSDISubForm::IsLastForm(TForm *form);
void __fastcall TSDISubForm::RestoreWind(void);
protected:
String __fastcall GetClassNameStr(HWND hWindow);
void __fastcall CreateParams(TCreateParams ¶ms);
public: // ユーザー宣言
__fastcall TSDISubForm(TComponent* Owner);
bool NotFirstForm;
};
//---------------------------------------------------------------------------
extern PACKAGE TSDISubForm *SDISubForm;
//---------------------------------------------------------------------------
#endif
//PROJ
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("prjSdi.res");
USEFORM("main.cpp", SDIMainForm);
USEFORM("Sub.cpp", SDISubForm);
//---------------------------------------------------------------------------
BOOL IsPrevAppExist(const char * mutextname)
{
bool bRst = false;
CreateMutex(NULL, true, mutextname);
if (GetLastError() == ERROR_ALREADY_EXISTS) {
bRst = true;
}
return bRst;
}
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE mutex;
HWND hWindow;
try
{
const char * mutexname= "SDIFORM";
if(IsPrevAppExist(mutexname)) {
hWindow = FindWindow("TSDIMainForm",NULL);
SetForegroundWindow(hWindow);
SendMessage(hWindow, WM_USER+50,0,0);
}
else {
SetProp(Application->Handle,PChar(mutexname),mutex);
Application->Initialize();
Application->CreateForm(__classid(TSDIMainForm), &SDIMainForm);
Application->ShowMainForm = false;
Application->Run();
RemoveProp(Application->Handle, PCHAR(mutexname));
}
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
ReleaseMutex(mutex);
return 0;
}
//---------------------------------------------------------------------------
안녕하세요? 공윤경이라고 합니다.
프로그램의 테스크메니져로부터의 종료시 문제
테스크메니져서에 프로세스에서는 하나의 EXE화일이 보이고
어플리케이션에서 동적으로 생성한 폼의 이름이 보여서.
어플리케이션부에서 하나씩 클릭해서 종료가능한 프로그램을 작성중입니다.
코드에 이상이 있나요? 실행 + 종료등 아무에러가 없는데
테스트매니져서에서 프로그램을 종료시키면 에러가 발생하고 프로그램이 전부 종료됩니다.
메인폼과 서브폼으로 이루어져서 실행시 메인폼은 보이지않고
서브폼이 뜹니다. 그이후에도 서브폼으로부터 서브폼을 추가하는 형태의 프로그램입니다.
뭔가 제가 잘못생각한 부분이 있는지...
잘부탁드립니다 ^^
|