logo
Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related

WinForm.cpp

00001 //
00002 //  SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation.        
00003 //
00004 //      Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net)
00005 //
00006 
00007 
00008 // WinForm.cpp - Main Window (form) functions.
00009 #pragma warning(disable: 4786) // Dont limit identifier lengths to 250 chars.
00010 
00011 #define IDC_MAIN_EDIT 4
00012 #define ButtonID 99
00013 
00014 #include <windows.h>
00015 #include <map>
00016 #include <ddraw.h>
00017 #include <mmsystem.h>
00018 #include "WinForm.h"
00019 
00020 using namespace std;
00021 
00022 namespace GUIToolkit {
00023 
00024 WinForm::~WinForm() {
00025          // Free the windowsclass allocated in PutInWinForm
00026          // with RegisterClassEx                
00027          UnregisterClass(g_szClassName, GUIControl::GUIInstance->hInstance);
00028 }
00029 
00030 
00031 WinForm::WinForm(int xsize, int ysize, winTypes myTypes, std::string title) {
00032         
00033         //myEventHandler = myEventHD;
00034         //nCmdShow = GUIControl::GUIInstance->nCmdShow;
00035         
00036         strcpy(g_szClassName,GUIControl::GUIInstance->getUniqueName().c_str());
00037         // This handles all the standard initialization of the window.  
00038         // Registering the Window Class
00039     wc.cbSize        = sizeof(WNDCLASSEX);                  // Size of Structure
00040     wc.style         = 0;       
00041     wc.lpfnWndProc   = GUIControl::WndProc; //?
00042     wc.cbClsExtra    = 0;
00043     wc.cbWndExtra    = 0;
00044     wc.hInstance     = GUIControl::GUIInstance->hInstance;
00045     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
00046     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
00047     wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // (HBRUSH)(COLOR_WINDOW+1);
00048     wc.lpszMenuName  = NULL;
00049     wc.lpszClassName = g_szClassName;
00050     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
00051 
00052         // DLLZ
00053         /*
00054         wc.hIcon         = 0;
00055     wc.hCursor       = 0;
00056     wc.hIconSm       = 0;
00057         */
00058 
00059     if(!RegisterClassEx(&wc))
00060     {
00061        
00062         // TODO: We should THROW the error 
00063                 LPVOID lpMsgBuf;
00064                 if (!FormatMessage( 
00065                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00066                         FORMAT_MESSAGE_FROM_SYSTEM | 
00067                         FORMAT_MESSAGE_IGNORE_INSERTS,
00068                         NULL,
00069                         GetLastError(),
00070                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00071                         (LPTSTR) &lpMsgBuf,
00072                         0,
00073                         NULL ))
00074                 {
00075                         // Handle the error.
00076                         return;
00077                 }
00078                 
00079                 // Process any inserts in lpMsgBuf.
00080                 // ...
00081                 
00082                 // Display the string.
00083                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error1", MB_OK | MB_ICONINFORMATION );
00084                 
00085                 // Free the buffer.
00086                 LocalFree( lpMsgBuf );
00087         // TODO: We should THROW the error 
00088                  MessageBox(NULL, "Window Registration Failed1!", g_szClassName,
00089             MB_ICONEXCLAMATION | MB_OK);
00090     }
00091 
00092     // Create the Window
00093     hwnd = CreateWindowEx(
00094         WS_EX_TOOLWINDOW | WS_EX_TOPMOST,  // WS_EX_CLIENTEDGE
00095         g_szClassName,
00096         title.c_str(),
00097         WS_OVERLAPPEDWINDOW,
00098         CW_USEDEFAULT, CW_USEDEFAULT, xsize, ysize,
00099         NULL, NULL, GUIControl::GUIInstance->hInstance, NULL);
00100 
00101     if(hwnd == NULL)
00102     {
00103         MessageBox(NULL, "Window Creation Failed2!", "Error!",
00104             MB_ICONEXCLAMATION | MB_OK);
00105                 LPVOID lpMsgBuf;
00106                 if (!FormatMessage( 
00107                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00108                         FORMAT_MESSAGE_FROM_SYSTEM | 
00109                         FORMAT_MESSAGE_IGNORE_INSERTS,
00110                         NULL,
00111                         GetLastError(),
00112                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00113                         (LPTSTR) &lpMsgBuf,
00114                         0,
00115                         NULL ))
00116                 {
00117                         // Handle the error.
00118                         return;
00119                 }
00120                 
00121                 // Process any inserts in lpMsgBuf.
00122                 // ...
00123                 
00124                 // Display the string.
00125                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
00126                 
00127                 // Free the buffer.
00128                 LocalFree( lpMsgBuf );
00129         // TODO: We should THROW the error 
00130     }
00131 
00132         // Check if this is the first window. Per default, the firstWindow close the application.
00133         if (GUIControl::GUIInstance->firstWindow == 0) {GUIControl::GUIInstance->firstWindow = hwnd;};
00134     
00135 }
00136 
00137 void WinForm::show() {
00138 
00139         ShowWindow(hwnd, GUIControl::GUIInstance->nCmdShow);
00140     UpdateWindow(hwnd);
00141 }
00142 
00143 
00144 
00145 
00146  
00147 
00148 
00149 
00150 DBCanvas::DBCanvas(WinForm * WF, int _x0, int _y0, int _x1, int _y1)
00151 {       ID = (HMENU)GUIControl::GUIInstance->RegisterRaw(this);
00152         topWF = WF;
00153         //WF->special = this;
00154         x0 = _x0; x1 = _x1; y0 = _y0; y1 = _y1; 
00155         PAINTSTRUCT ps;
00156 
00157         // We Need a handle to the window. BeginPaint will do for now.
00158         HDC hdc = BeginPaint(WF->hwnd, &ps);
00159 
00160         // We need a drawing context, and a bitmap for our back-buffer:
00161         OffScreenHdc = CreateCompatibleDC(hdc); 
00162     OffScreen = CreateCompatibleBitmap(hdc, x1, y1);
00163         
00164     // We must associate the Bitmap with the OffScrenHdc.
00165         // The Routine seems to return an 'old' buffer - do we need it?
00166         HBITMAP hbmOldBuffer = (HBITMAP) SelectObject(OffScreenHdc, OffScreen);
00167 
00168         // We havent done any paint, but end it anyways.
00169         EndPaint(WF->hwnd, &ps);        
00170 }
00171 
00172 DBCanvas::~DBCanvas() {
00173         //
00174 }
00175 
00176 void DBCanvas::Paint(HDC hdc) {
00177         BitBlt(hdc, x0, y0, x1, y1, OffScreenHdc, 0, 0, SRCCOPY);       
00178 }
00179 
00180 void DBCanvas::Rectangle(int x0, int y0, int x1, int y1) {
00181     ::Rectangle(OffScreenHdc,x0,y0,x1,y1);
00182 }
00183 
00184 
00185 void DBCanvas::setPixel(int x,int y, int r, int g, int b)
00186 {
00187 
00188         SetPixel(OffScreenHdc, x, y, RGB(r,g,b));
00189 }
00190 
00191 
00192 
00193 void DBCanvas::update() {
00194     UpdateWindow(topWF->hwnd);
00195         RECT * myRect = new RECT();
00196         myRect->left = x0;
00197         myRect->top = y0;
00198         myRect->right =  x0 + x1;
00199         myRect->bottom =  y0 + y1;
00200 
00201         InvalidateRect(topWF->hwnd,myRect,FALSE); // Change to only update relevant area
00202 }
00203 
00204 void DBCanvas::clear() {
00205         BitBlt(OffScreenHdc,0,0,x1,y1,OffScreenHdc,0,0,WHITENESS); 
00206 };
00207 
00208 
00209 // ------------------------------------------------------------------------
00210 /*
00211 
00212 DXCanvas::~DXCanvas() { };
00213 
00214 void DXCanvas::getDC()
00215 {       lpDDSBack -> Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
00216         //lpDDSBack->GetDC(&OffScreenHdc);
00217         //lpDDSBack->ReleaseDC(OffScreenHdc);
00218 }
00219 
00220 void DXCanvas::releaseDC()
00221 {       lpDDSBack -> Unlock(NULL);
00222         //lpDDSBack->GetDC(&OffScreenHdc)
00223         //lpDDSBack->ReleaseDC(OffScreenHdc);
00224 }
00225 
00226 void DXCanvas::setPixel(int x,int y, int r, int g, int b)
00227 {
00228         //getDC();
00229         //SetPixel(OffScreenHdc, x, y, RGB(r,g,b));
00230 
00231         //
00232         //COLORREF col = RGB(r,g,b);
00233 
00234         ((unsigned short*)ddsd.lpSurface)[(y * (ddsd.lPitch >> 1) + 2*x)] 
00235                 =  (unsigned short)r;
00236 
00237         ((unsigned short*)ddsd.lpSurface)[(y * (ddsd.lPitch >> 1) + 2*x +1)] 
00238                 =  (unsigned short)g;
00239 
00240         
00241         //int idx = (y * ddsd.lPitch  + (x * 3));
00242     //    ((BYTE*)ddsd.lpSurface)[idx]   = (BYTE)r;
00243     //       ((BYTE*)ddsd.lpSurface)[idx+1] = (BYTE)g;
00244     //       ((BYTE*)ddsd.lpSurface)[idx+2] = (BYTE)b;
00245 
00246         //releaseDC();
00247         //
00248         // if (lpDDSBack->GetDC(&OffScreenHdc) == DD_OK)
00249         //{
00250         //              SetPixel(OffScreenHdc, x, y, RGB(r,g,b));
00251                 //SetBkMode(hdc2, TRANSPARENT);
00252                 //SetTextColor(hdc2, RGB(0,255,255));
00253                 //TextOut(hdc2, 13, 13, "qweqweqwe", 8);
00254         //      lpDDSBack->ReleaseDC(OffScreenHdc);
00255                 //return TRUE;
00256         // } else MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00257                  ;      //SetPixel(OffScreenHdc, x, y, RGB(r,g,b));
00258                 
00259 }
00260 
00261 void DXCanvas::Paint(HDC hdc)
00262 {
00263     HRESULT ddrval;
00264     RECT rcRectSrc;
00265     RECT rcRectDest;
00266     POINT p;
00267 
00268     // if we're windowed do the blit, else just Flip
00269     if (IsWindowed)
00270     {
00271         // first we need to figure out where on the primary surface our window lives
00272         p.x = 0; p.y = 0;
00273         ClientToScreen(topWF->hwnd, &p);
00274         GetClientRect(topWF->hwnd, &rcRectDest);
00275         //OffsetRect(&rcRectDest, p.x, p.y);
00276         SetRect(&rcRectSrc, 0,0,x1-x0,y1-y0);
00277                 SetRect(&rcRectDest, p.x+x0,p.y+y0,p.x+x1,p.y+y1);
00278         ddrval = lpDDSPrimary->Blt( &rcRectDest, lpDDSBack, &rcRectSrc, DDBLT_WAIT, NULL);
00279     } else {
00280         ddrval = lpDDSPrimary->Flip( NULL, DDFLIP_WAIT); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00281         }
00282    
00283 }
00284 
00285 DXCanvas::DXCanvas(WinForm * WF, int _x0, int _y0, int _x1, int _y1) {
00286         ID = (HMENU)GUIControl::GUIInstance->RegisterRaw(this);
00287         topWF = WF;
00288         //WF->special2 = this;
00289         OffScreenHdc = 0;
00290         x0 = _x0; x1 = _x1; y0 = _y0; y1 = _y1; 
00291         
00292     
00293     DDSCAPS ddscaps;
00294     HRESULT ddrval;
00295 
00296     IsWindowed = true;
00297 
00298 
00299     ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
00300         
00301     if( ddrval != DD_OK )
00302     {
00303       MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00304     }
00305 
00306     // using DDSCL_NORMAL means we will coexist with GDI
00307     ddrval = lpDD->SetCooperativeLevel( topWF->hwnd, DDSCL_NORMAL );
00308     if( ddrval != DD_OK )
00309     {
00310         lpDD->Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00311         //
00312     }
00313 
00314     memset( &ddsd, 0, sizeof(ddsd) );
00315     ddsd.dwSize = sizeof( ddsd );
00316     ddsd.dwFlags = DDSD_CAPS;
00317     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
00318 
00319     // The primary surface is not a page flipping surface this time
00320     ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
00321     if( ddrval != DD_OK )
00322     {
00323         lpDD->Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00324         //
00325     }
00326 
00327     // Create a clipper to ensure that our drawing stays inside our window
00328     ddrval = lpDD->CreateClipper( 0, &lpClipper, NULL );
00329     if( ddrval != DD_OK )
00330     {
00331         lpDDSPrimary->Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00332         lpDD->Release();
00333         //
00334     }
00335 
00336     // setting it to our hwnd gives the clipper the coordinates from our window
00337     ddrval = lpClipper->SetHWnd( 0, topWF->hwnd );
00338     if( ddrval != DD_OK )
00339     {
00340         lpClipper-> Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00341         lpDDSPrimary->Release();
00342         lpDD->Release();
00343         //
00344     }
00345 
00346     // attach the clipper to the primary surface
00347          ddrval = lpDDSPrimary->SetClipper( lpClipper );
00348     if( ddrval != DD_OK )
00349     {
00350         lpClipper-> Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00351         lpDDSPrimary->Release();
00352         lpDD->Release();
00353         //
00354     }
00355 
00356     memset( &ddsd, 0, sizeof(ddsd) );
00357     ddsd.dwSize = sizeof( ddsd );
00358     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
00359     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
00360     ddsd.dwWidth = 640; //x1-x0;
00361     ddsd.dwHeight = 480; //y1-y0;
00362 
00363     // create the backbuffer separately
00364     ddrval = lpDD->CreateSurface( &ddsd, &lpDDSBack, NULL );
00365     if( ddrval != DD_OK )
00366     {
00367         lpClipper-> Release(); MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);
00368         lpDDSPrimary->Release();
00369         lpDD->Release();
00370        //
00371     }
00372         //lpClipper-> Release(); MessageBox(topWF->hwnd, "1", "OK", MB_OK | MB_ICONERROR);
00373     
00374         //if (lpDDSBack->GetDC(&OffScreenHdc) != DD_OK) MessageBox(topWF->hwnd, "1", "Error", MB_OK | MB_ICONERROR);      
00375 
00376 }
00377 
00378 void DXCanvas::update() {
00379     UpdateWindow(topWF->hwnd);
00380         RECT * myRect = new RECT();
00381         myRect->left = x0;
00382         myRect->top = y0;
00383         myRect->right =  x0 + x1;
00384         myRect->bottom =  y0 + y1;
00385 
00386         InvalidateRect(topWF->hwnd,myRect,FALSE); // Change to only update relevant area
00387 }
00388                         // HDC hdcMem = CreateCompatibleDC(hdc);
00389                         //HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall);
00390 
00391                         //GetObject(g_hbmBall, sizeof(bm), &bm);
00392 
00393                         //BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
00394 
00395                         //SelectObject(hdcMem, hbmOld);
00396                         //DeleteDC(hdcMem);
00397 
00398 
00399 void DXCanvas::clear() {
00400         DDBLTFX ddBltFX;
00401         memset(&ddBltFX, 0, sizeof(ddBltFX));
00402         ddBltFX.dwSize = sizeof(ddBltFX);
00403         ddBltFX.dwFillColor = 0;
00404 
00405         HRESULT ddRes = lpDDSBack->Blt(NULL, NULL, NULL, DDBLT_COLORFILL,&ddBltFX);
00406         
00407 }
00408 
00409 void DXCanvas::init() {
00410         //
00411 }
00412 
00413 
00414 
00415 
00416 */
00417 
00418 
00419 }; // end of namespace

Syntopia Project. Visit the web page, or the SourceForge page.
Docs made by Doxygen. Email: Mikael Christensen