2009/06/16 13:44 Neo's Study/C#


흠.. 일반적으로 닷넷에서 정해진 폼이 있다. 하지만 원하는 메세지들 내용 등을 바꾸자면 아래 예제 코드와 같이 가능하다.

*예제 코드*

public class MessageBoxChange
    {
        delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetWindowsHookEx(int hook, HookProc callback, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll")]
        static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern IntPtr GetDlgItem(IntPtr hDlg, DialogResult nIDDlgItem);

        [DllImport("user32.dll")]
        static extern bool SetDlgItemText(IntPtr hDlg, DialogResult nIDDlgItem, string lpString);

        [DllImport("kernel32.dll")]
        static extern uint GetCurrentThreadId();

        static IntPtr g_hHook;

         string yes;
         string no;

        //OK
        public DialogResult Show(string text, string caption, string ok)
        {
            this.yes = yes;
            this.no = no;
            g_hHook = SetWindowsHookEx(5, new HookProc(HookWndProc), IntPtr.Zero, GetCurrentThreadId());
            return MessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Question);
        }

        //YES NO
        public DialogResult Show(string text, string caption, string yes, string no)
        {
            this.yes = yes;
            this.no = no;
            g_hHook = SetWindowsHookEx(5, new HookProc(HookWndProc), IntPtr.Zero, GetCurrentThreadId());
            return MessageBox.Show(text, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        public int HookWndProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            IntPtr hChildWnd;

            if (nCode == 5)
            {
                hChildWnd = wParam;

                if (GetDlgItem(hChildWnd, DialogResult.OK) != null)
                    SetDlgItemText(hChildWnd, DialogResult.OK, this.yes);

                if (GetDlgItem(hChildWnd, DialogResult.Yes) != null)
                    SetDlgItemText(hChildWnd, DialogResult.Yes, this.yes);

                if (GetDlgItem(hChildWnd, DialogResult.No) != null)
                    SetDlgItemText(hChildWnd, DialogResult.No, this.no);

                UnhookWindowsHookEx(g_hHook);
            }
            else
                CallNextHookEx(g_hHook, nCode, wParam, lParam);

            return 0;
        }
    }

당연히 주의해야할 점은 네임스페이스다.

이들은 Forms를 참조하는 다이얼로그와 P-INVOKE를 이용하기 위한 InteropServices 의 네임스페이스를 참조해야한다.

이를 꼭 명심하자!!

저작자 표시 비영리 변경 금지
이올린에 북마크하기(0) 이올린에 추천하기(0)
posted by Neo's 맨땅헤딩인생
2009/06/16 13:29 Neo's Study/C#
1. BackgroundWorker는 ComponentModel 네임스페이스를 이용해야 쓸 수 있다.

2. BackgroundWorker bw = new BackgroundWorker(); 로 생성한다.

3. bw.WorkerSupportsCancellation = true; 설정을 해 준다.

4. bw.DoWork += new DoWorkEventHandler(bw_DoWork); 의 이벤트 핸들러를 생성한다.

5. bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);의 이벤트 핸들러를 생성한다.

6. DoWork 내부에 쓰레드에 이용할 내용을 첨부한다.

7. RunWorkerCompleted 내부에 쓰레드 종료시 이용할 내용을 첨부한다.

if (e.Error != null){} 에러 발생 시 처리
else if (e.Cancelled){bw.CancelAsync();}사용자에 의해 종료 되었을 시 처리
else{}정상 종료 되었을 시 처리

물론 이 BackgroundWorker를 실행하기 위해서는 bw.RunWorkerAnsync(); 처리를 해 줘야한다.

게시글 중 하나 이와 유사한 글이 있다. 그 부분은 단시 쓰는 방법에 대한 것만 나와있다.

쓰레드가 완료 된 처리 내용에 대한 것은 없는 껍데기에 불과 하다는 이야기다.

앞으로 좀 더 잘 알아보고 블로깅을 해야지.. ㅠ_ㅠ 이거 원..
저작자 표시 비영리 변경 금지
이올린에 북마크하기(0) 이올린에 추천하기(0)
posted by Neo's 맨땅헤딩인생
2009/06/12 14:37 Neo's Study/C#

흔히들 검색하다보면 한글자 치면 주르륵 내용이 나온다

그와 같이 콤보 박스도 가능하다.

물론 텍스트 박스도 TextChange 이벤트를 이용하여 가능하다.

하지만 데이터베이스와 연결이 되어있어야한다는 치명적인.. 약점이 ...

절차는 다음과 같다

1. AutoCompleteCustomSource에 컬렉션을 등록한다.

2. AutoCompleteMode에서 원하는 성격의 방식을 선택한다. 3가지 방식이 있다.

3. AutoCompleteSource에서 CustomSource를 선택한다.

그렇게 하면!! 된다!!

프레임워크 상에서 편하게 제공해주니.. -_- 그저 감사할 뿐이다 ㅎㅎ
저작자 표시 비영리 변경 금지
이올린에 북마크하기(0) 이올린에 추천하기(0)
posted by Neo's 맨땅헤딩인생
 <PREV 1 2 3 4 5 ... 45    NEXT>