1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
//Main.cpp
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <cstdio>
#include "Main.h"
LRESULT CALLBACK fnSecondThread_WndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
CREATESTRUCT* pCreateStruct = (CREATESTRUCT*)lParam;
HWND hMain = (HWND)pCreateStruct->lpCreateParams;
HINSTANCE hIns = ((LPCREATESTRUCT)lParam)->hInstance;
SetWindowLongPtr(hWnd,0*sizeof(void*),(LONG_PTR)hMain);
CreateWindowEx(0,L"button",L"Retrieve Text",WS_CHILD|WS_VISIBLE,50,40,200,30,hWnd,(HMENU)IDC_RETRIEVE_TEXT,hIns,0);
return 0;
}
case WM_COMMAND:
{
if(LOWORD(wParam)==IDC_RETRIEVE_TEXT && HIWORD(wParam)==BN_CLICKED)
{
HWND hMain=(HWND)GetWindowLongPtr(hWnd,0*sizeof(void*));
HWND hEdit=GetDlgItem(hMain,IDC_EDIT);
wchar_t szBuffer[128];
HANDLE hEvent=(HANDLE)GetWindowLongPtr(hMain,3*sizeof(void*));
PostMessage(hEdit,WM_GETTEXT,(WPARAM)127,(LPARAM)szBuffer);
if(hEvent)
{
DWORD dwWait=0;
dwWait=MsgWaitForMultipleObjectsEx(1,&hEvent,INFINITE,QS_ALLINPUT,0);
if(dwWait>=WAIT_OBJECT_0 && dwWait<=1)
GetWindowText(hEdit,szBuffer,127);
ResetEvent(hEvent);
}
MessageBox(hWnd,szBuffer,L"Text From Edit Control...",MB_OK);
}
return 0;
}
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
DWORD _stdcall SecondGuiThread(LPVOID pVoid)
{
MSG messages;
HWND hSecond=CreateWindowEx(0,L"Second_Thread",L"Second Thread Window",WS_OVERLAPPEDWINDOW|WS_VISIBLE,675,275,320,150,HWND_DESKTOP,0,GetModuleHandle(NULL),pVoid);
while(GetMessage(&messages,hSecond,0,0)>0)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return TRUE;
}
LRESULT __stdcall fnEditControl_WndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
HANDLE hEvent=NULL;
LRESULT lReturn=0;
HWND hMain=NULL;
if(msg==WM_GETTEXT)
{
hMain=GetParent(hwnd);
hEvent=(HANDLE)GetWindowLongPtr(hMain,3*sizeof(void*));
lReturn=CallWindowProc((WNDPROC)GetWindowLongPtr(hMain,2*sizeof(void*)), hwnd, msg, wParam, lParam);
BOOL blnSetEvent=SetEvent(hEvent);
}
else
{
hMain=GetParent(hwnd);
lReturn=CallWindowProc((WNDPROC)GetWindowLongPtr(hMain,2*sizeof(void*)), hwnd, msg, wParam, lParam);
}
return lReturn;
}
long fnWndProc_OnCreate(WndEventArgs& Wea)
{
wchar_t szClassName[]=L"Second_Thread";
WNDPROC fnEditProc=NULL;
HANDLE hEvent=NULL;
HWND hCtl=NULL;
WNDCLASSEX wc;
Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
hCtl=CreateWindowEx(WS_EX_CLIENTEDGE,L"edit",L"Here Is Some Hello, World Like Text!",WS_CHILD|WS_VISIBLE,25,25,250,30,Wea.hWnd,(HMENU)IDC_EDIT,Wea.hIns,0);
fnEditProc=(WNDPROC)SetWindowLongPtr(hCtl,GWLP_WNDPROC,(LONG_PTR)fnEditControl_WndProc);
SetWindowLongPtr(Wea.hWnd,2*sizeof(void*),(LONG_PTR)fnEditProc);
hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
SetWindowLongPtr(Wea.hWnd,3*sizeof(void*),(LONG_PTR)hEvent);
CreateWindowEx(0,L"button",L"Create Second Thread",WS_CHILD|WS_VISIBLE,50,90,200,30,Wea.hWnd,(HMENU)IDC_BUTTON,Wea.hIns,0);
memset(&wc,0,sizeof(wc));
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnSecondThread_WndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.hInstance=Wea.hIns;
wc.hCursor=LoadCursor(NULL,IDC_ARROW), wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
wc.cbWndExtra=sizeof(void*);
RegisterClassEx(&wc);
return 0;
}
long fnWndProc_OnCommand(WndEventArgs& Wea)
{
if(LOWORD(Wea.wParam)==IDC_BUTTON && HIWORD(Wea.wParam)==BN_CLICKED)
{
DWORD lpThreadId=0;
HANDLE hThread=CreateThread(NULL,0,SecondGuiThread,Wea.hWnd,0,&lpThreadId);
if(hThread)
{
SetWindowLongPtr(Wea.hWnd,0*sizeof(void*),(LONG_PTR)hThread);
SetWindowLongPtr(Wea.hWnd,1*sizeof(void*),(LONG_PTR)lpThreadId);
}
}
return 0;
}
long fnWndProc_OnDestroy(WndEventArgs& Wea)
{
HANDLE hEvent=NULL;
PostQuitMessage(0);
hEvent=(HANDLE)GetWindowLongPtr(Wea.hWnd,3*sizeof(void*));
CloseHandle(hEvent);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].iMsg==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"Threads7";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
memset(&wc,0,sizeof(wc));
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.hInstance=hIns;
wc.hCursor=LoadCursor(NULL,IDC_ARROW), wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
wc.cbWndExtra=4*sizeof(void*);
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,275,475,320,200,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|