ConHwnd.cpp
上传用户:hyb6888
上传日期:2016-01-24
资源大小:5186k
文件大小:9k
- #include "stdafx.h"
- #include "stdio.h"
- #include "ConHwnd.h"
- #include "windows.h"
- #include "CMyWnd.h"
- #include "tool\mytool.h"
- ConHwnd::ConHwnd()
- {
- POINT pp[100];
- fCanMove=0;
- WinStartPos=myResM.ReadRegPos("ConHwnd");
- WinHeight=myResM.WinHeight;
- WinWidth=myResM.WinWidth;
- ControlHeight=myResM.ControlHeight;
- pp[0].x=20+3; pp[0].y=WinHeight+ControlHeight;
- pp[1].x=20+3; pp[1].y=ControlHeight+40-4;
- pp[2].x=0; pp[2].y=ControlHeight+20-4;
- pp[3].x=0; pp[3].y=WinHeight+ControlHeight;
-
- WinRGN1=CreatePolygonRgn(pp,4,1);
- WinRGN2=CreatePolygonRgn(pp,4,1);
- hUIFont = CreateFont(20, 0, 0, 0, FW_NORMAL,
- FALSE, FALSE, FALSE, ANSI_CHARSET,
- OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
- DEFAULT_PITCH, "宋体");
- myHwndParent=NULL;
- }
- ConHwnd::~ConHwnd()
- {
- SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
- DestroyWindow(m_hWnd);
- // MessageBox(0,"~ConHwnd",0,0);
- m_hWnd=NULL;
- //如果不消毁窗口当主程序,退出时窗口依然在运行,会引起系统崩溃
- DeleteObject(hUIFont);
- DeleteObject (WinRGN1);
- }
- BOOL ConHwnd::Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent , DWORD dwStyle ,DWORD dwExStyle, HMENU hMenu )
- {
- // 初始化全局字串
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_IME;
- wcex.lpfnWndProc = InitProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 12;
- wcex.hInstance = hInstance;
- wcex.hIcon = 0;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = 0;
- wcex.lpszClassName = szClassName;
- wcex.hIconSm = 0;
- hInst=hInstance;
- RegisterClassEx(&wcex);
- {
- m_hWnd=CreateWindowEx( 0,
- szClassName,NULL,
- WS_POPUP|WS_DISABLED,// | WS_DISABLED,WS_SIZEBOX
- WinStartPos.x,WinStartPos.y,WinWidth,WinHeight,
- hWndParent,NULL,hInstance,this);
- ShowWindow(m_hWnd,SW_SHOWNOACTIVATE);
- UpdateWindow(m_hWnd);
- }
- myHwndParent=hWndParent;
- return m_hWnd != NULL;
- }
- LRESULT WINAPI ConHwnd::WindowProc(UINT uMsg,UINT wParam,LONG lParam)
- {
- POINT pt;
- int st;
- long x,y;
- static int moveflage;
- static int tt;
- char ss[100];
- try {
- sprintf(ss,"%d",uMsg);
- TRACE("Paste received = '%s'rn", ss);
- switch (uMsg)
- {
- case WM_CREATE:
- myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
- initalizeWin();
- myset.mySetWindow(m_hWnd,RGB(255,255,255),200,LWA_ALPHA|LWA_COLORKEY);
- break;
- case WM_PAINT://绘制窗口
- myPaintTxt(0);
- break;
- case WM_SETCURSOR:
- if ((HIWORD(lParam) == WM_MOUSEMOVE))
- {
- GetCursorPos( &pt );
- GetWindowRect(m_hWnd,&WinRect);
- WinSIZE.cx = WinRect.right - WinRect.left;
- WinSIZE.cy = WinRect.bottom - WinRect.top;
- Cursordif.x = pt.x - WinRect.left;//得到相对位置
- Cursordif.y = pt.y - WinRect.top;
- SetCapture(m_hWnd);
- //SetWindowLong(m_hWnd,4,1);//GWL_HINSTANCE DWL_MSGRESULT
- //SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
- }
- else
- return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
- break;
- case WM_LBUTTONDOWN:
- fCanMove = TRUE;
- moveflage=0;
- break;
- case WM_LBUTTONUP:
- GetCursorPos( &pt );//取得光标位置
- y=lParam/0x10000;
- x=lParam%0x10000;
- if( fCanMove)
- {
- GetCursorPos( &pt );
- MoveWindow(m_hWnd,pt.x - Cursordif.x,pt.y - Cursordif.y,WinSIZE.cx,WinSIZE.cy,TRUE);
- myResM.WriteRegPos("ConHwnd",pt);
- fCanMove = FALSE;
- }
- if(moveflage==0)
- {
- //确定选择区域
- st=getRgnFlage(x,y);
- TranStr(st);
- }
- myPaintTxt(0);
- break;
-
- case WM_MOUSEMOVE:
- moveflage=1;
- GetCursorPos( &pt );//取得光标位置
- y=lParam/0x10000;
- x=lParam%0x10000;
- if(fCanMove==TRUE)
- {
- MoveWindow(m_hWnd,pt.x - Cursordif.x, pt.y - Cursordif.y, WinSIZE.cx,WinSIZE.cy,TRUE);
- myPaintTxt(0);
- //使用SetWindowPos作移动时会改变集焦。可用MoveWindow解决。
- //在不移动时可以使用它。
- }
- else
- {
- st=getRgnFlage(x,y);
- if(st==0)
- SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_SIZEALL )));
- else
- SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
- myPaintTxt(st);
- }
- //测试鼠标是否移出窗口
- if(PtInRegion(WinRGN2,x,y)==0)
- {
- myPaintTxt(0);
- }
- if(fCanMove==FALSE )
- {
- //SetWindowLong(m_hWnd,4,0);
- ReleaseCapture();
- }
-
- break;
- case WM_DESTROY:
- //SendMessage(m_hWnd,WM_CLOSE,0,0);
- if(myHwndParent==NULL)
- {
- //PostQuitMessage(0);
- }
- else
- {
- SetWindowLong(m_hWnd,GWL_WNDPROC,(long)endProc);
- DestroyWindow(m_hWnd);
- }
- break;
- default:
- return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
- break;
- }
- } catch (...) {
- // delete (CMyWnd*)Main_CWnd;
- // SetWindowLong(*Main_hWnd,GWL_WNDPROC,(long)endProc);
- // DestroyWindow(*Main_hWnd);
- // SetWindowLong(*inpu_hWnd,GWL_WNDPROC,(long)endProc);
- // DestroyWindow(*inpu_hWnd);
- // SetWindowLong(*Cont_hWnd,GWL_WNDPROC,(long)endProc);
- // DestroyWindow(*Cont_hWnd);
- // MessageBox(0,"Error","有错误发生",0);
- }
- return 0;
- }
- //确定指定位置所在的区域
- int ConHwnd::getRgnFlage(long x,long y)
- {
- int ret=0;
- long rgb,r,g,b;
- rgb=GetPixel(BackDC,x,y-ControlHeight+120);
- r=GetRValue(rgb);
- g=GetGValue(rgb);
- b=GetBValue(rgb);
- if(r==255 && g==0 && b==0)
- ret=1;
- if(r==0 && g==255 && b==0)
- ret=2;
- if(r==0 && g==0 && b==255)
- ret=3;
- return ret;
- }
- //窗口的初始化
- ConHwnd::initalizeWin()
- {
- WindDC=GetDC(m_hWnd);
- Texthdc = CreateCompatibleDC(WindDC);
- Texthdcbmp=CreateCompatibleBitmap(WindDC,WinWidth,WinHeight);
- SelectObject(Texthdc, hUIFont);
- DeleteObject(SelectObject(Texthdc,Texthdcbmp));
- SetBkMode(Texthdc,TRANSPARENT);
- SelectObject(WindDC, hUIFont);
- TextH=GetTabbedTextExtent(WindDC,"1",1,0,0)/0x10000;
- SetWindowRgn(m_hWnd,WinRGN1,TRUE);
- BackhBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCE(IDB_Back));
- BackDC = CreateCompatibleDC(WindDC);
- DeleteObject(SelectObject(BackDC,BackhBmp));
- }
- int ConHwnd::myPaintTxt(int select )
- {
- PAINTSTRUCT ps;
- HDC hdc;
- //清除掉文本区
- hdc=Texthdc;
- if(select==0)
- BitBlt(hdc,0,ControlHeight,WinWidth,WinHeight-ControlHeight,BackDC,0,0,SRCCOPY);
- else
- BitBlt(hdc,0,ControlHeight,WinWidth,WinHeight-ControlHeight,BackDC,select*25,125,SRCCOPY);
- BeginPaint(m_hWnd, &ps);
- BitBlt(WindDC,0,0,WinWidth,WinHeight,hdc,0,0,SRCCOPY);
- EndPaint(m_hWnd, &ps);
- }
- int ConHwnd::RedrawWin()
- {
- RedrawWindow(m_hWnd,0,0,RDW_VALIDATE|RDW_NOCHILDREN|RDW_INVALIDATE|RDW_ERASENOW|RDW_ERASE);//RDW_ERASE
- return 0;
- }
- int ConHwnd::TranStr(int selet)
- {
- HIMC hUICurIMC;
- char buf[1001]="";
- char ss[1001]="";
- mytool jjj;
- int tt=0,rr=0;
- hUICurIMC= (HIMC)GetWindowLong(myHwndParent,IMMGWL_IMC);
- if(hUICurIMC)
- {
- jjj.TranComStr(selet,buf);
- sprintf(ss,"%d",strlen(buf));
- //MessageBox(0,ss,"d",0);
- //buf[80]=0;
- IMESenChiApp(hUICurIMC,buf);
- }
- return 0;
- }
- /////////////////////////////////////////////////////////////
- //下面为函数部分
- //用于向系统发送消息
- BOOL ConHwnd::GenerateMessage(HIMC hIMC, LPDWORD lpdwTransKey,LPGENEMSG lpGeneMsg)
- {
- LPINPUTCONTEXT lpIMC;
- if( (lpIMC = ImmLockIMC(hIMC)) == NULL )
- return FALSE;
-
- if (IsWindow(lpIMC->hWnd))
- {
- LPDWORD lpdw;
- if (!(lpIMC->hMsgBuf = ImmReSizeIMCC(lpIMC->hMsgBuf,
- sizeof(DWORD) * (lpIMC->dwNumMsgBuf +1) * 3)))
- return FALSE;
-
- if (!(lpdw = (LPDWORD)ImmLockIMCC(lpIMC->hMsgBuf)))
- return FALSE;
-
- lpdw += (lpIMC->dwNumMsgBuf) * 3;
- *((LPGENEMSG)lpdw) = *lpGeneMsg;
- ImmUnlockIMCC(lpIMC->hMsgBuf);
- lpIMC->dwNumMsgBuf++;
- ImmGenerateMessage(hIMC);
- }
- ImmUnlockIMC(hIMC);
- return TRUE;
- }
- //把转换后的汉字发到接收程序中使用内部机制.
- BOOL ConHwnd::IMESenChiApp(HIMC hIMC, char *ss)
- {
- //内部发送在首次发送时可能失效,因此暂时不用
- if(strlen(ss)<2)
- {
- GENEMSG GnMsg;
- LPCOMPOSITIONSTRING lpCompStr;
- LPINPUTCONTEXT lpIMC;
- lpIMC = ImmLockIMC(hIMC);
- lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);
- if(strlen(ss))
- {
- strcpy(GETLPRESULTSTR(lpCompStr),ss);
- //_tcscpy(GETLPRESULTSTR(lpCompStr),"高点");
- lpCompStr->dwResultStrLen =strlen(ss);
- {
- char sss[100];
- sprintf(sss,"%d",strlen(ss));
- // MessageBox(0,sss,0,0);
- }
- GnMsg.msg = WM_IME_COMPOSITION;//
- GnMsg.wParam = 0;
- GnMsg.lParam = GCS_RESULTSTR;
- GenerateMessage(hIMC, 0,(LPGENEMSG)&GnMsg);
- }
- }
- else
- {
- //向获得焦点的窗口发送字符串
- //发送过多的字符有可能失去挂钩
- int i;
- for(i=0;i<(int)strlen(ss);)
- {
- if(ss[i]<0)
- {
- SendMessage(GetFocus(), WM_IME_CHAR, MAKEWORD(ss[i+1],ss[i]), 0);
- i+=2;
- }
- else
- {
- SendMessage(GetFocus(), WM_IME_CHAR, MAKEWORD(0,ss[i]), 0);
- if(ss[i]==13&&ss[i+1]==10)
- i++;
- i++;
- }
- }
- }
- return TRUE;
- }