My first "real" windows app

This is a discussion on My first "real" windows app within the Windows Programming forums, part of the Platform Specific Boards category; Couple things before I post my code... 1) Im only using what I've learned so far in Petzolds windows book. ...

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    My first "real" windows app

    Couple things before I post my code...

    1) Im only using what I've learned so far in Petzolds windows book.
    2) There are some quick fixes and a bug where the data output is, the letters go off the client area (im stuck there). I got it to go to the next line after every 38 letters but if you change the window size you can see they are still there.
    3) The encryption algorithm is crap I just added it to keep the spot (a bench warmer) lol. I'm working on an advanced algorithm right now so thats what ill be using.


    Is there anything I should fix?
    Last edited by JoshR; 07-28-2005 at 08:39 AM.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <windows.h>
    #include <string>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    void showOptions (HWND, HDC, PAINTSTRUCT);
    void showPass (HWND, HDC, PAINTSTRUCT);
    void showData (HWND, HDC, PAINTSTRUCT);
    void EncryptDecrypt (const std::string &, std::string &);
    
    /* Global Variables */
    int cyChar, cxChar;
    std::string password, data;
    const std::string title = "Please select an option from the list";
    const std::string line  = "-------------------------------------";
    const std::string opt1  = "1) Encrypt text";
    const std::string opt2  = "2) Decrypt text";
    const std::string enc1  = "Enter Password: ";
    const std::string dat1  = "Data: ";
    const std::string ins1  = "Instructions:";
    const std::string ins2  = "  Enter your password and data, then";
    const std::string ins3  = "press enter to see the result.";
    /* End Global Variables */
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    						  PSTR szCmdLine, int iCmdShow) 
    {
    	static TCHAR* szAppName = TEXT("EncDec");
    	HWND			hwnd;
    	MSG			msg;
    	WNDCLASS		wndclass;
    	
    	wndclass.style				= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc		= WndProc;
    	wndclass.cbClsExtra 		= 0;
    	wndclass.cbWndExtra		= 0;
    	wndclass.hInstance		= hInstance;
    	wndclass.hIcon				= LoadIcon (NULL, IDI_APPLICATION);
    	wndclass.hCursor			= LoadCursor (NULL, IDC_ARROW);
    	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
    	wndclass.lpszMenuName 	= NULL;
    	wndclass.lpszClassName 	= szAppName;
    	
    	if (!RegisterClass (&wndclass)) 
    	{
    		MessageBox (NULL, TEXT("ERROR This Program Requries NT!"), 
    						szAppName, MB_ICONERROR);
    		return 0;
    	}
    	
    	hwnd = CreateWindow (szAppName, TEXT("Pea - Encryption / Decryption"), WS_OVERLAPPED | WS_CAPTION | 
    								WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX,
    								CW_USEDEFAULT, CW_USEDEFAULT, 310, 200,
    								NULL, NULL, hInstance, NULL);
    	
    	ShowWindow (hwnd, iCmdShow);
    	UpdateWindow (hwnd);
    	
    	while (GetMessage (&msg, NULL, 0, 0)) 
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    	
    	return msg.wParam;
    }
    	
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    	static short int choice = 0;
    	static bool pass = false, done = false;
    	HDC		   hdc;
    	TEXTMETRIC  tm;
    	PAINTSTRUCT ps;
    	
    	switch (message) 
    	{
    		case WM_CREATE:
    			hdc = GetDC (hwnd);
    			/* First set the font we want */
    			SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
    			/* Get the verticle size of char */
    		   GetTextMetrics (hdc, &tm);
    			cyChar = tm.tmHeight + tm.tmExternalLeading;
    			cxChar = tm.tmAveCharWidth;
    		   /* End */
    			ReleaseDC (hwnd, hdc);
    			return 0;
    			
    		case WM_CHAR:
    			if (!choice)
    			{   
    				switch (wParam) 
    				{	
    					case '1': choice = 1; break;
    					case '2': choice = 2; break;
    					default:
    						MessageBox (hwnd, TEXT("ERROR: Invalid Choice"), 
    									  TEXT("Pea"), MB_ICONERROR);
    				}
    			}
    			else if (choice == 1 || choice == 2 && !done) 
    			{
    				if (!pass) // If password has not been entered
    				{
    					if (wParam == '\r') // If enter is pressed
    					{
    						if (password.empty())
    						{
    							MessageBox (hwnd, TEXT("ERROR: Password is empty"), 
    											TEXT("Pea"), MB_ICONERROR);
    							break;
    						}
    						pass = true;
    					}
    					else if (wParam == '\b') // If backspace is pressed
    					{
    						if (password.empty()) break;
                      password.erase(password.size() - 1);
    					}
                   else
    						password += wParam;
    				}
    				else
    			   {
    					if (wParam == '\b') // If backspace is pressed
    					{
    						if (data.empty()) break;
                      data.erase(data.size() - 1);
    					}
    					else if (wParam == '\r')  // If enter is pressed
    					{	
    						done = true, choice = 3;
    						EncryptDecrypt (password, data);
    					}
    					else
    					   data += wParam;
    				}
    			}
    			else if (done) // Wait for key press to restart
    			{
    				choice = 0, done = false, pass = false;
    				password.erase(); data.erase();
    			}
    			
    			InvalidateRect (hwnd, NULL, TRUE); // Update client area
    			return 0;
    			
    	   case WM_PAINT:
    			switch (choice)
    			{
    			   case 0: showOptions (hwnd, hdc, ps); break;
    				case 1: (!pass) ? showPass (hwnd, hdc, ps) : showData (hwnd, hdc, ps); break;
    				case 2: (!pass) ? showPass (hwnd, hdc, ps) : showData (hwnd, hdc, ps); break;
    				case 3: showData (hwnd, hdc, ps); break;
    				default:
    					MessageBeep (0);
    					MessageBox (hwnd, TEXT("INTERNAL ERROR!"), TEXT("Pea"), MB_ICONERROR);
    					break;
    			}
    			return 0;
    			
    		case WM_DESTROY:
    			PostQuitMessage (0);
    			return 0;
    	}
    	return DefWindowProc (hwnd, message, wParam, lParam);
    }
    
    void showOptions (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
    	hdc = BeginPaint (hwnd, &ps);
    	
    	/* Chose font */
    	SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
    	/* Set background mode to blend with background color */
    	SetBkMode (hdc, TRANSPARENT);
    	/* Print text */
    	TextOut (hdc, 0, 0, title.c_str(), title.size());
    	TextOut (hdc, 0, cyChar, line.c_str(), line.size());
    	TextOut (hdc, 0, cyChar * 2, opt1.c_str(), opt1.size());
    	TextOut (hdc, 0, cyChar * 3, opt2.c_str(), opt2.size());
    	TextOut (hdc, 0, cyChar * 6, line.c_str(), line.size());
    	TextOut (hdc, 0, cyChar * 7, ins1.c_str(), ins1.size());
    	TextOut (hdc, 0, cyChar * 8, ins2.c_str(), ins2.size());
    	TextOut (hdc, 0, cyChar * 9, ins3.c_str(), ins3.size());
    
    	EndPaint(hwnd, &ps);
    }
    
    void showPass (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
    	std::string mask;
    	hdc = BeginPaint (hwnd, &ps);
    	
    	/* Chose font */
    	SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
    	/* Set background mode to blend with background color */
    	SetBkMode (hdc, TRANSPARENT);
    	/* Print text */
    	TextOut (hdc, 0, 0, enc1.c_str(), enc1.size());
    	/* Mask password */
    	mask.append (password.size(), '*');
    	TextOut (hdc, cxChar * enc1.size(), 0, mask.c_str(), mask.size());
    	
    	EndPaint (hwnd, &ps);
    }
    
    void showData (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
    	int loop = 0, index = 1;
       hdc = BeginPaint (hwnd, &ps);
    	
    	/* Chose font */
    	SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
    	/* Set background mode to blend with background color */
    	SetBkMode (hdc, TRANSPARENT);
    	/* Print text */
    	TextOut (hdc, 0, 0, dat1.c_str(), dat1.size());
    	
       /* Print data */
    	do 
    	{
    		TextOut (hdc, 0, cyChar * index, data.c_str() + loop, 
    					data.size() - loop);
    		loop += 38, index++; // 38 characters fit in the window
    		if (index == 12) 
    		{	
    			MessageBox (hwnd, TEXT("ERROR: Max characters reached"), 
    											  TEXT("Pea"), MB_ICONERROR);
    		   data.erase(data.size() - 1);
    			InvalidateRect (hwnd, NULL, TRUE);
    		}
    	}
    	while (loop < data.size());
    	
    	EndPaint (hwnd, &ps);
    }
    
    void EncryptDecrypt (const std::string &pass, std::string &data) {
    	for (int loop = 0; loop < data.size(); loop++)
    		data[loop] ^= (pass[loop % pass.size()] % 6);
    }

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    Sorry about the crazy looking if and else statements, the boards messed up my spacing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Windows App from Scratch
    By kingneb in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2008, 06:21 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 06:56 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21