성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Reordering on an Alpha processor ;...
[정성태] 공유 감사합니다. ^^ 참고로, WPF에서 WindowsF...
[Tom Lee] 답변 감사합니다. 나름의 해결책 연구해보고 여기에도 공유해봅니다...
[정성태] 아래의 글을 보면, MoveWindow 하면 될 듯한데요. ^^...
[Tom Lee] 안녕하세요 올려주신 글 참고하여 WPF 어플리케이션 안에 Uni...
[정성태] A graphical depiction of the steps ...
[정성태] 질문을 주셔서 출판사 측에 문의를 했습니다. 약 한 달 정도 후...
[Thorondor
] @정성태 개인 블로그인데도 거의 커뮤니티 급 인 것 같아요. 요...
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - 환경 변수의 변화를 알리는 WM_SETTINGCHANGE Win32 메시지 사용법</h1> <p> WM_SETTINGCHANGE 메시지는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > WM_SETTINGCHANGE message ; <a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange'>https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange</a> </pre> <br /> 시스템의 환경 설정이 바뀌었을 때 발생하는 메시지입니다. 이 메시지를 닷넷에서, 가령 Windows Forms에서는 일반 윈도우 메시지와 같은 방식으로 다루면 되기 때문에 다음과 같이 받아 처리할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { <span style='color: blue; font-weight: bold'>const uint WM_SETTINGCHANGE = 0x001a;</span> public Form1() { InitializeComponent(); } protected override unsafe void <span style='color: blue; font-weight: bold'>WndProc</span>(ref Message m) { <span style='color: blue; font-weight: bold'>if (m.Msg == WM_SETTINGCHANGE && m.LParam != IntPtr.Zero) { char* ptr = (char*)m.LParam.ToPointer(); string text = new string(ptr); MessageBox.Show("WM_SETTINGCHANGE: " + text); }</span> base.WndProc(ref m); } } } </pre> <br /> 물론, 원한다면 우리도 WM_SETTINGCHANGE 이벤트를 발생시킬 수 있습니다. 가령, 시스템 전역 환경 변수를 레지스트리에 설정한 후 현재 실행 중인 프로세스들 중에 (예를 들어 위에서 구현했던 WinForm 예제처럼) 그 변화에 대해 관심이 있는 프로세스에 알리고 싶다면 WM_SETTINGCHANGE 메시지를 발생시킬 수 있습니다.<br /> <br /> "<a target='tab' href='https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange'>WM_SETTINGCHANGE message</a>" 문서에 그 방법이 간단하게 소개돼 있는데요,<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> To effect a change in the environment variables for the system or the user, broadcast this message with lParam set to the string "Environment".<br /> </div><br /> <br /> 이와 관련한 예제 코드는 stackoverflow에도 나와 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Setting global environment variables programmatically ; <a target='tab' href='https://stackoverflow.com/questions/48928002/setting-global-environment-variables-programmatically'>https://stackoverflow.com/questions/48928002/setting-global-environment-variables-programmatically</a> </pre> <br /> 이것을 C#으로 옮기면 다음과 같이 코딩할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Runtime.InteropServices; namespace ConsoleApp1 { [Flags] enum SendMessageTimeoutFlags : uint { SMTO_NORMAL = 0x0, SMTO_BLOCK = 0x1, SMTO_ABORTIFHUNG = 0x2, SMTO_NOTIMEOUTIFNOTHUNG = 0x8, SMTO_ERRORONEXIT = 0x20 } internal class Program { [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr SendMessageTimeout( IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult); const uint WM_SETTINGCHANGE = 0x001a; static void Main(string[] args) { UIntPtr result = new UIntPtr(); IntPtr hwndBroadcast = new IntPtr(0xffff); SendMessageTimeout(hwndBroadcast, WM_SETTINGCHANGE, UIntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result); } } } </pre> <br /> 따라서, 처음 소개한 WinForm 예제를 실행 후 위의 SendMessageTimeout 코드를 실행하면 WinForm 예제에서 WM_SETTINGCHANGE 메시지에 대해 반응하는 것을 확인할 수 있습니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1920&boardid=331301885'>첨부 파일은 이 글의 예제 프로젝트를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1570
(왼쪽의 숫자를 입력해야 합니다.)