-
다이얼로그 박스 - Win32GUI/Window 2009. 1. 29. 00:27대화상자 리소스
ID
Font
Caption
Minimize Box
Maximize Box
서브클래싱
DialogBox()
WM_INITDIALOG
DialogBoxParam()EndDialog()
메뉴는 리소스에서 설정하거나 WM_INITDIALOG에서 SetMenu()
#include <windowsx.h>
#include "resource.h"
//HWND hDlgMain;
// WM_INITDIALOG 메시지 핸들러
BOOL OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
// {
// hDlgMain = hWnd;
// }
return TRUE;
}
/*
void OnClose(HWND hwnd)
{
EndDialog(hwnd,IDCANCEL);
}
*/
// WM_COMMAND 메시지 핸들러
void OnDlgCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
// {
if (id == IDCANCEL)
{
EndDialog(hWnd, IDCANCEL);
}
// }
}
BOOL CALLBACK MainDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
switch (iMessage) {
HANDLE_MSG(hDlg, WM_INITDIALOG, OnInitDialog);
// HANDLE_MSG(hDlg, WM_CLOSE, OnClose);
HANDLE_MSG(hDlg, WM_COMMAND, OnDlgCommand);
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP, MainDlgProc);
return 0;
}
/*
HINSTANCE hInst; // current instance
HWND g_hwndDialog;
int CALLBACK DlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_INITDIALOG:
g_hwndDialog = hwndDlg;
return TRUE;
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case IDCANCEL:
EndDialog( hwndDlg, wParam );
return TRUE;
}
break;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
hInst = hInstance; // Store instance handle in our global variable
DialogBox( hInstance, MAKEINTRESOURCE( IDD_WMADIALOG ), NULL, DlgProc );
return( 0 );
}
*/
#if !defined(AFX_RENDERURLDLG_H__AAF8D025_1149_49BA_8BB0_CB90C01814C0__INCLUDED_)
#define AFX_RENDERURLDLG_H__AAF8D025_1149_49BA_8BB0_CB90C01814C0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// RenderURLDlg.h : header file
//
#define MAX_URL_PATH 32768
/////////////////////////////////////////////////////////////////////////////
// CRenderURLDlg dialog
class CRenderURLDlg
{
// Operations
public:
static BOOL GetOpenURLName(HINSTANCE hInstance, HWND hWndParent, LPTSTR pstrURL, DWORD dwMaxURL);
// Procedures
public:
static BOOL CALLBACK DialogProc(
HWND hwndDlg, // handle to dialog box
UINT uiMsg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
// Implementation
protected:
// member variables
private:
static LPTSTR m_pstrURL;
static DWORD m_dwMaxURL;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_RENDERURLDLG_H__AAF8D025_1149_49BA_8BB0_CB90C01814C0__INCLUDED_)
...
#include "RenderURLDlg.h"
#include "resource.h"
...
// static member variables
LPTSTR CRenderURLDlg::m_pstrURL = NULL;
DWORD CRenderURLDlg::m_dwMaxURL = 0;
/////////////////////////////////////////////////////////////////////////////
// CRenderURLDlg dialog
BOOL CRenderURLDlg::GetOpenURLName(HINSTANCE hInstance, HWND hWndParent, LPTSTR pstrURL, DWORD dwMaxURL)
{
m_pstrURL = pstrURL;
m_dwMaxURL = dwMaxURL;
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_RENDER_URL), hWndParent, DialogProc);
}
/////////////////////////////////////////////////////////////////////////////
// CRenderURLDlg message handlers
INT_PTR CALLBACK CRenderURLDlg::DialogProc(HWND hwndDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch (uiMsg)
{
case WM_INITDIALOG:
{
HKEY hkeyFilter = 0;
int rc = 0;
TCHAR *pszKey = TEXT("Software\\Microsoft\\ActiveMovie\\GraphEdit\\URLPersistList");
// open the registry key ...
rc = RegOpenKey(HKEY_CURRENT_USER, pszKey, &hkeyFilter);
if (rc == ERROR_SUCCESS)
{
DWORD dwSize;
BYTE szURLName[MAX_URL_PATH];
char szStringValue[5];
// get combo box window
HWND hWndCombo = GetDlgItem(hwndDlg, IDC_COMBO_URL);
for (int i=0; i<6; i++) // Graph Editor searches from URL0 to URL5!
{
wsprintf(szStringValue, "URL%d", i);
// query the value
rc = RegQueryValueEx(hkeyFilter, szStringValue,
NULL, NULL, szURLName, &dwSize);
if (rc == ERROR_SUCCESS)
SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)szURLName);
else
break;
}
SendMessage(hWndCombo, CB_SETCURSEL, i-1, 0); // just like graph editor
SendMessage(hWndCombo, CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); // just like graph editor
// close the registry key ...
rc = RegCloseKey(hkeyFilter);
}
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
GetDlgItemText(hwndDlg, IDC_COMBO_URL, m_pstrURL, m_dwMaxURL);
EndDialog(hwndDlg, IDOK);
return TRUE;
case IDCANCEL:
EndDialog(hwndDlg, IDCANCEL);
return TRUE;
}
}
return FALSE;
}
...
#include "RenderURLDlg.h"
...
HRESULT CNullRenderingURLApp::OnFileOpen()
{
// URL 불러오기
// Open URL Dialog ...
// Retrieve the name of the selected file
WCHAR wFileName[MAX_URL_PATH] ;
if (CRenderURLDlg::GetOpenURLName(m_hInstance, m_hWnd, m_szFileName, MAX_URL_PATH) == IDOK)
{
#ifndef UNICODE
MultiByteToWideChar(CP_ACP, 0, m_szFileName, -1, wFileName, MAX_URL_PATH) ;
#else
lstrcpy(wFileName, m_szFileName);
#endif
}
else
return E_ABORT ;
...
return S_OK ;
}
CreateDialog()
CreateDialogParam()
DestroyWindow()
IsDialogMessage()
HWND hMDlg;
BOOL CALLBACK MlessDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam,LPARAM lParam)
{
switch (iMessage) {
/*
case WM_INITDIALOG:
...
return TRUE;
*/
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_CHANGE:
...
return TRUE;
case IDCANCEL:
DestroyWindow(hMDlg);
hMDlg = NULL;
return TRUE;
}
break;
}
return FALSE;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (iMessage) {
/*
case WM_CREATE:
...
return 0;
*/
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
if (!IsWindow(hMDlg)) {
hMDlg = CreateDialog(g_hInst,MAKEINTRESOURCE(IDD_DIALOG1),
hWnd,MlessDlgProc);
ShowWindow(hMDlg,SW_SHOW);
}
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
g_hInst=hInstance;
...
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;
}
모달 다이얼로그의 입력 포커스는 컨트롤이 가지므로 메시지 후킹(WH_KEYBOARD)을 해야 된다.
HWND g_hDlg;
BOOL CALLBACK KeyDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
switch (iMessage) {
case WM_INITDIALOG:
g_hDlg = hDlg;
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDCANCEL:
EndDialog(hDlg, IDCANCEL);
g_hDlg = NULL;
return TRUE;
}
break;
case WM_KEYDOWN:
if (wParam == 'A') {
MessageBox(hDlg,"A키 입력을 받았습니다", "알림", MB_OK);
}
return TRUE;
}
return FALSE;
}
HHOOK hKeyHook;
LRESULT CALLBACK KeyHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0 && IsWindow(g_hDlg)) {
if (wParam == 'A') {
if (HIWORD(lParam) & KF_UP)
SendMessage(g_hDlg2, WM_KEYDOWN, wParam, lParam);
return TRUE;
}
}
return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
switch (iMessage) {
case WM_CREATE:
hWndMain=hWnd;
hKeyHook=SetWindowsHookEx(WH_KEYBOARD,KeyHookProc,NULL,GetCurrentThreadId());
return 0;
case WM_DESTROY:
UnhookWindowsHookEx(hKeyHook);
PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
DialogBox(g_hInst,MAKEINTRESOURCE(IDD_DIALOG1),hWnd,KeyDlgProc);
return 0;
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
참조 사이트: