๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

๋ชจ๋“  Windows ํ”„๋กœ๊ทธ๋žจ์€ ๋ฉ”์‹œ์ง€ ๋ฃจํ”„(message loop)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€ ๋ฃจํ”„๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” ๋ถ€๋ถ„์„ ๋œปํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์ด๋‚˜ ๊ธฐํƒ€ ํ–‰๋™์„ ๋ฐ›์•„ ๋ฉ”์‹œ์ง€ ํ์— ๋„ฃ๊ณ , ๊ฐ ํ”„๋กœ์„ธ์Šค๋Š” ๋ฉ”์‹œ์ง€๋“ค์„ ์ง€์†์ ์œผ๋กœ ์ฝ์–ด ๋“ค์ด๋ฉด์„œ ๊ฐ ๋ฉ”์‹œ์ง€์— ๋Œ€์‘ํ•˜๋Š” ํ–‰๋™์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

MFC๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. MFC์—์„œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์œˆ๋„์šฐ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” SendMessage ์™€ PostMessage API๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

๋‘ API์˜ ์›ํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

LRESULT SendMessage(
  HWND   hWnd,
  UINT   Msg,
  WPARAM wParam,
  LPARAM lParam
);

BOOL PostMessageA(
  HWND   hWnd,
  UINT   Msg,
  WPARAM wParam,
  LPARAM lParam
);

๋ฐ˜ํ™˜ํ˜•๊ณผ ํ•จ์ˆ˜ ์ด๋ฆ„๋งŒ ๋‹ค๋ฅผ ๋ฟ ์ธ์ž๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ค ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
โ€ป ์˜ˆ์ œ ํŒŒ์ผ์ด ๋ณธ๋ฌธ ํ•˜๋‹จ์— ์žˆ์Šต๋‹ˆ๋‹ค.


SendMessage & PostMessage

SendMessage๋Š” ์œˆ๋„์šฐ ํ”„๋กœ์‹œ์ €(window procedure)๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋ฉฐ, ํ”„๋กœ์‹œ์ €๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ•จ์ˆ˜ ๋‚ด์— ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๊ทธ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•  ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋“ฏ์ด ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

PostMessage๋Š” ํ˜ธ์ถœ๋œ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฉ”์‹œ์ง€ ํ์— ๋“ค์–ด๊ฐ€๋ฉฐ, ์œˆ๋„์šฐ ํ”„๋กœ์‹œ์ €์—์„œ ์ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€, ๋ฉ”์‹œ์ง€๊ฐ€ ์ฆ‰๊ฐ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ GetMessage()์— ์˜ํ•ด ํ•ด์„๋œ ๋ฉ”์‹œ์ง€๊ฐ€ DispatchMessage()์— ์˜ํ•ด ์œˆ๋„์šฐ ํ”„๋กœ์‹œ์ €๋กœ ์ „๋‹ฌ๋˜์–ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ PostMessage์— ์˜ํ•ด ์ „๋‹ฌ๋œ ๋ฉ”์‹œ์ง€๋Š” ์–ธ์ œ ์ฒ˜๋ฆฌ๋  ์ง€ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.

๋‘ ํ•จ์ˆ˜์˜ ์ฐจ์ด๋ฅผ ์š”์•ฝํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 SendMessage

PostMessage

์œˆ๋„์šฐ ํ”„๋กœ์‹œ์ €๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋ฉฐ, ํ”„๋กœ์‹œ์ €๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 ๋ฉ”์‹œ์ง€ ํ์— ๋ฉ”์‹œ์ง€๊ฐ€ ์‚ฝ์ž…๋˜๋ฉฐ, ์œˆ๋„์šฐ ํ”„๋กœ์‹œ์ €์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ํ•ด๋‹น ๋ฉ”์‹œ์ง€๊ฐ€ ์–ธ์ œ ์ฒ˜๋ฆฌ๋  ์ง€ ์˜ˆ์ธก์ด ์–ด๋ ต๋‹ค.

 ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ(sequentially)

๋น„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ(not sequentially)

 ๋™๊ธฐ ๋ฐฉ์‹(synchronous)

๋น„๋™๊ธฐ ๋ฐฉ์‹(asynchronous) 


์ƒ˜ํ”Œ ์ฝ”๋“œ ๋™์ž‘

๋‘ API๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
SendMessage ๋ฒ„ํŠผ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

void CExamMessageDlg::OnBnClickedButtonSendmessage()
{
	m_wndListBox.AddString(_T("Enter Send Message Button"));
	SendMessage(EVENT_SENDMESSAGE);
	m_wndListBox.AddString(_T("Leave Send Message Button"));
}

PostMessage ๋ฒ„ํŠผ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

void CExamMessageDlg::OnBnClickedButtonPostmessage()
{
	m_wndListBox.AddString(_T("Enter Post Message Button"));
	PostMessage(EVENT_POSTMESSAGE);
	m_wndListBox.AddString(_T("Leave Post Message Button"));
}


๊ฐ ๋ฉ”์‹œ์ง€ ๋ฒ„ํŠผ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, SendMessage๋Š” ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฐ ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ๋ฉ”์‹œ์ง€๊ฐ€ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๊ณ , PostMessage๋Š” ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฐ ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์ฒ˜๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SendMessage์™€ PostMessage์˜ ๋™์ž‘

ExamMessage.zip


์ฐธ๊ณ  ๋งํฌ:

MessageLoop(Wiki): https://ko.wikipedia.org/wiki/%EB%A7%88%EC%9D%B4%ED%81%AC%EB%A1%9C%EC%86%8C%ED%94%84%ED%8A%B8_%EC%9C%88%EB%8F%84%EC%9A%B0%EC%9D%98_%EB%A9%94%EC%8B%9C%EC%A7%80_%EB%A3%A8%ED%94%84

SendMessage(MSDN):https://docs.microsoft.com/ko-kr/windows/win32/api/winuser/nf-winuser-sendmessage

PostMessage(MSDN):https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea

SendMessage & PostMessage 1: https://stackoverflow.com/questions/18526961/sendmessage-and-postmessage-which-one-is-better-to-use/18528227

SendMessage & PostMessage 2: https://www.codeproject.com/Tips/1029254/SendMessage-and-PostMessage

๋ฐ˜์‘ํ˜•