WINAPI - 모델리스형 대화상자
내장형 하드웨어/WINAPI / 2011. 10. 6. 15:25
- 모델리스형은 대화상자를 열어놓은 채로 메인 윈도우를 조작할 수 있기 때문에 모달형 대화상자보다 더 복잡하며 사용하기도 어렵다.
- 여러가지 문제가 발생할 수도 있으며 대화상자를 닫지 않은 채로 값을 변경하고 메인 윈도우가 변경된 값을 즉시 인지할 수 있어야 하는 부담까지 지니고 있다.
- MsgProc.cpp
- main.cpp
- 여러가지 문제가 발생할 수도 있으며 대화상자를 닫지 않은 채로 값을 변경하고 메인 윈도우가 변경된 값을 즉시 인지할 수 있어야 하는 부담까지 지니고 있다.
- MsgProc.cpp
#include "MsgProc.h"
#include "resource.h"
HWND hWndMain;
int x;
int y;
TCHAR str[128];
HWND hMDlg;
LRESULT OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
hWndMain = hWnd;
x=100;
y=100;
lstrcpy(str, TEXT("String"));
return 0;
}
LRESULT OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
if(!IsWindow(hMDlg))
{
hMDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, MlessDlgProc);
ShowWindow(hMDlg, SW_SHOW);
}
return 0;
}
LRESULT OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, x, y, str, lstrlen(str));
EndPaint(hWnd, &ps);
return 0;
}
LRESULT OnDestroy(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PostQuitMessage(0);
return 0;
}
BOOL OnInitDialog(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
SetDlgItemText(hDlg, IDC_STR, str);
SetDlgItemInt(hDlg, IDC_X, x, FALSE);
SetDlgItemInt(hDlg, IDC_Y, y, FALSE);
return TRUE;
}
BOOL OnDlgCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case ID_CHANGE:
GetDlgItemText(hDlg, IDC_STR, str, 128);
x = GetDlgItemInt(hDlg, IDC_X, NULL, FALSE);
y = GetDlgItemInt(hDlg, IDC_Y, NULL, FALSE);
InvalidateRect(hWndMain, NULL, TRUE);
EndDialog(hDlg, IDOK);
case IDCANCEL:
case ID_CLOSE:
DestroyWindow(hMDlg);
hMDlg = NULL;
return TRUE;
}
return FALSE;
}
#include "resource.h"
HWND hWndMain;
int x;
int y;
TCHAR str[128];
HWND hMDlg;
LRESULT OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
hWndMain = hWnd;
x=100;
y=100;
lstrcpy(str, TEXT("String"));
return 0;
}
LRESULT OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
if(!IsWindow(hMDlg))
{
hMDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, MlessDlgProc);
ShowWindow(hMDlg, SW_SHOW);
}
return 0;
}
LRESULT OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, x, y, str, lstrlen(str));
EndPaint(hWnd, &ps);
return 0;
}
LRESULT OnDestroy(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PostQuitMessage(0);
return 0;
}
BOOL OnInitDialog(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
SetDlgItemText(hDlg, IDC_STR, str);
SetDlgItemInt(hDlg, IDC_X, x, FALSE);
SetDlgItemInt(hDlg, IDC_Y, y, FALSE);
return TRUE;
}
BOOL OnDlgCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case ID_CHANGE:
GetDlgItemText(hDlg, IDC_STR, str, 128);
x = GetDlgItemInt(hDlg, IDC_X, NULL, FALSE);
y = GetDlgItemInt(hDlg, IDC_Y, NULL, FALSE);
InvalidateRect(hWndMain, NULL, TRUE);
EndDialog(hDlg, IDOK);
case IDCANCEL:
case ID_CLOSE:
DestroyWindow(hMDlg);
hMDlg = NULL;
return TRUE;
}
return FALSE;
}
- main.cpp
//main.cpp
#include<windows.h>
#include "MsgProc.h" // add
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("Mless");
// add
typedef struct MESSAGEMAP
{
UINT iMessage;
LRESULT (*lpfnMsgProc)(HWND, WPARAM, LPARAM);
}MESSAGEMAP;
//CALLBACKAboutDlgProc
typedef struct DLGMESSAGEMAP
{
UINT iMessage;
BOOL (*lpfnMsgProc)(HWND, WPARAM, LPARAM);
}DLGMESSAGEMAP;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst = hInstance;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = lpszClass;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, hInstance, NULL);;
ShowWindow(hWnd, nCmdShow);
while(GetMessage(&Message, NULL, 0, 0))
{
if(!IsWindow(hMDlg)||!IsDialogMessage(hMDlg, &Message))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
return (int)Message.wParam;
}
BOOL CALLBACK MlessDlgProc(HWND hMDlg,UINT iMessage, WPARAM wParam, LPARAM lParam)
{
int i;
static DLGMESSAGEMAP DlgMessageMaps[] = {
{WM_INITDIALOG, OnInitDialog},
{WM_COMMAND, OnDlgCommand}
};
for(i=0; i<sizeof(DlgMessageMaps)/sizeof(DlgMessageMaps[0]); ++i)
{
if(DlgMessageMaps[i].iMessage == iMessage)
return (*DlgMessageMaps[i].lpfnMsgProc)(hMDlg, wParam, lParam);
}
return FALSE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
//add
int i;
static MESSAGEMAP MessageMaps[] = {
{WM_CREATE, OnCreate},
{WM_LBUTTONDOWN, OnLButtonDown},
{WM_PAINT, OnPaint},
{WM_DESTROY, OnDestroy}
};
for(i=0; i<sizeof(MessageMaps)/sizeof(MessageMaps[0]);++i)
{
if(MessageMaps[i].iMessage == iMessage)
{
return (*MessageMaps[i].lpfnMsgProc)(hWnd, wParam, lParam);
}
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
→ 출력 결과#include<windows.h>
#include "MsgProc.h" // add
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("Mless");
// add
typedef struct MESSAGEMAP
{
UINT iMessage;
LRESULT (*lpfnMsgProc)(HWND, WPARAM, LPARAM);
}MESSAGEMAP;
//CALLBACKAboutDlgProc
typedef struct DLGMESSAGEMAP
{
UINT iMessage;
BOOL (*lpfnMsgProc)(HWND, WPARAM, LPARAM);
}DLGMESSAGEMAP;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst = hInstance;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = lpszClass;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, hInstance, NULL);;
ShowWindow(hWnd, nCmdShow);
while(GetMessage(&Message, NULL, 0, 0))
{
if(!IsWindow(hMDlg)||!IsDialogMessage(hMDlg, &Message))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
return (int)Message.wParam;
}
BOOL CALLBACK MlessDlgProc(HWND hMDlg,UINT iMessage, WPARAM wParam, LPARAM lParam)
{
int i;
static DLGMESSAGEMAP DlgMessageMaps[] = {
{WM_INITDIALOG, OnInitDialog},
{WM_COMMAND, OnDlgCommand}
};
for(i=0; i<sizeof(DlgMessageMaps)/sizeof(DlgMessageMaps[0]); ++i)
{
if(DlgMessageMaps[i].iMessage == iMessage)
return (*DlgMessageMaps[i].lpfnMsgProc)(hMDlg, wParam, lParam);
}
return FALSE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
//add
int i;
static MESSAGEMAP MessageMaps[] = {
{WM_CREATE, OnCreate},
{WM_LBUTTONDOWN, OnLButtonDown},
{WM_PAINT, OnPaint},
{WM_DESTROY, OnDestroy}
};
for(i=0; i<sizeof(MessageMaps)/sizeof(MessageMaps[0]);++i)
{
if(MessageMaps[i].iMessage == iMessage)
{
return (*MessageMaps[i].lpfnMsgProc)(hWnd, wParam, lParam);
}
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
'내장형 하드웨어 > WINAPI' 카테고리의 다른 글
영상 처리 - 복제 화면 띄우기, RGB값 변화에 따른 화면 출력 (1) | 2011.11.23 |
---|---|
WINAPI - sokoban (0) | 2011.10.10 |
WINAPI - DlgCheck (0) | 2011.09.29 |
WINAPI - 컨트롤과의 통신 (0) | 2011.09.28 |
WINAPI - 대화상자 (0) | 2011.09.27 |