1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

WinApi32 GUI example program - DecToBin converter

Discussion in 'C' started by david_BS, Apr 9, 2012.

  1. david_BS

    david_BS New Member

    Joined:
    Apr 5, 2012
    Messages:
    16
    Likes Received:
    3
    Trophy Points:
    0
    I want to share my little program src. it uses winapi32 GUI but it is a simple base converter. decimal to binary and vice versa

    Code:
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    //
    // UTN FRGP
    // 2012
    // David Riedel
    // EMAIL: david_bs@live.com
    //
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    //#define WINVER 0x0500 //WINVER as 0x0500 enables features specific to Win 98 and 2000
    //#define VC_EXTRALEAN
    
    #include <windows.h>
    #include <stdio.h>
    //gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 
    
    #include "ansii_conversions.h"
    #include "convertir.h"
    
    /////////////////////////
    // Globales
    
    HWND EditControl1;
    //HWND EditControl2;
    //HWND EditControl3;
    HWND ListControl1;
    HWND ListControl2;
    HWND StaticControl1;
    HWND CheckBoxControl1;
    HWND CheckBoxControl2;
    
    const int ID_EDIT1=1, ID_LIST1=2, ID_CHECK1=3, ID_CHECK2=4;
    const int ID_OK = 6;
    const int ID_CANCEL = 7;
    const int ID_LABEL1 = 0;
    
    /////////////////////////////
    
    void getCentradoGlobal(int vXY[2], int h, int w);
    
    ///////////////
    // Controles de ventana
    
    void RegistrarControlesDeVentanaPrincipal(HWND hWin)
    {
    	static char* t1 =  TEXT("Ingrese 1 nĂºmero");
    	
    	unsigned long EstiloHijo1 = WS_VISIBLE | WS_CHILD;
    	unsigned long EstiloHijo2 = WS_CHILD | WS_VISIBLE | WS_BORDER;
    	unsigned long EstiloHijo3 = WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON;
    	unsigned long EstiloHijo4 = WS_CHILD | WS_VISIBLE | LBS_NOTIFY | LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP;
    
    	unsigned long editstyle =WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP;
    	unsigned long liststyle =WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP| LBS_NOTIFY;
    
    	int offs = 30;
    	int X=20;
    	int Y=10;
    	int W=200;
    	int H=25;
    
    	StaticControl1 = CreateWindow( TEXT("static"),
    		                           t1,
    								   EstiloHijo1,
    								   20,10,180,20,
    								   hWin,
    								   (HMENU)ID_LABEL1,
    								   NULL,
    								   NULL);
    
    	EditControl1 = CreateWindow( TEXT("Edit"),
    		                         NULL,
    								 editstyle,
    								 X,
    								 Y+20,
    								 W+63,
    								 H,
    								 hWin,
    								 (HMENU)ID_EDIT1,
    								 NULL,
    								 NULL);
    
    	ListControl1 = CreateWindow( TEXT("listbox"),
    		                         NULL,
    								 liststyle,
    								 X,
    								 Y+20+(offs*1),
    								 W+63,
    								 H-5,
    								 hWin,
    								 (HMENU)ID_LIST1,
    								 NULL,
    								 NULL);
    
    	CheckBoxControl1 = CreateWindow( TEXT("button"),
    		                         "BinToDec",
    								 WS_VISIBLE | WS_CHILD  | BS_CHECKBOX |BS_AUTOCHECKBOX,
    								 X+160,90,84,20,
    								 hWin,
    								 (HMENU)ID_CHECK1,
    								 NULL,
    								 NULL);
    
    	CreateWindow( TEXT("button"),
    		          TEXT("Convertir"),
    				  EstiloHijo1,
    				  X,90,80,H,
    				  hWin,
    				  (HMENU) ID_OK,
    				  NULL,
    				  NULL);
    
    	CreateWindow( TEXT("button"),
    		          TEXT("Salir"),
    				  EstiloHijo1,
    				  X+100,90,50,H,
    				  hWin,
    				  (HMENU)ID_CANCEL,
    				  NULL,
    				  NULL);
    }
    
    //////////
    // CallBack
    
    INT_PTR CALLBACK WinProc(HWND hWin, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(uMsg)
    	{
    		case WM_CREATE:
    		{
    			RegistrarControlesDeVentanaPrincipal(hWin);
    
    			SetFocus(EditControl1);
    		}
    		break;
    
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    				case ID_OK:
    				{
    					char chBuffer1[128];
    					memset(chBuffer1,0,sizeof(chBuffer1));
    					GetDlgItemText( hWin,ID_EDIT1, chBuffer1, sizeof( chBuffer1 ) );
    					BOOL checked = IsDlgButtonChecked(hWin, ID_CHECK1);
    					int l=strlen(chBuffer1);
    					if( (checked&&!EsBin(chBuffer1))  ||  (!checked&&!EsDec(chBuffer1))) {
    						MessageBox(0,"Errores de ingreso!","Convertidor",0);
    						return FALSE;
    					}
    					if(l<1){
    						MessageBox(0,"Ingrese algo!","Convertidor",0);
    						return FALSE;
    					}
    					long l_res_pre;
    					if(l==1) l_res_pre = atol_char(chBuffer1[0]);
    					else l_res_pre = atol_string(chBuffer1);
    					long l_res;
    					if(!checked) l_res = Bin(l_res_pre,2,10);//dec a bin
    					else l_res = Bin(l_res_pre,10,2);//bin a dec
    					char* ch_res = ltoa_string(l_res);
    					SendDlgItemMessage(hWin, ID_LIST1, LB_RESETCONTENT, 0, 0);
    					int nNum=0;
    					char* buf1 = (char*)GlobalAlloc(GPTR,strlen(ch_res)+1);
    					strcpy(buf1, ch_res);
    					free(ch_res);
    					int index1 = SendDlgItemMessage(hWin, ID_LIST1, LB_ADDSTRING, 0, (LPARAM)buf1);
    					SendDlgItemMessage(hWin, ID_LIST1, LB_SETITEMDATA, (WPARAM)index1, (LPARAM)nNum);
    					GlobalFree((HANDLE)buf1);
    				}
    				break;
    
    				case ID_CANCEL:
    				{
    					SendMessage(hWin, WM_CLOSE, 0, 0);
    					return TRUE;
    				}
    				break;
    			}
    		}
    		break;
    
    		case WM_CLOSE:
    		{
    			DestroyWindow(hWin);
    			return TRUE;
    		}
    		/////////////////////////////
    	    case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			return TRUE;
    		}
    
    		default:
    			return DefWindowProc(hWin,uMsg,wParam,lParam);
    
    	}
    
    	return 0;
    	//return DefWindowProc(hWin,uMsg,wParam,lParam);
    }
    
    
    ////////////
    // Clase de ventana
    
    void RegistrarClaseVentana(WNDCLASS* wc)
    {
    	//	HICON hIconImg = (HICON)LoadImage(NULL, "BS.ico", IMAGE_ICON, 542, 449, LR_LOADFROMFILE);
    	wc->lpszClassName = TEXT("Window");
    	wc->hInstance     = GetModuleHandle(NULL);
        //wc->hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    	wc->hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
        wc->lpfnWndProc   = (WNDPROC)WinProc;
    	wc->style         =  CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
        wc->cbClsExtra    =  0;
        wc->cbWndExtra    =  0;
        wc->hIcon         =  LoadIcon(NULL,"IDI_ICON1");
    //	wc->hIcon         =  LoadIcon(NULL,"IDI_WINLOGO");
        wc->hCursor       =  LoadCursor(NULL,IDC_ARROW);
        wc->lpszMenuName  =  NULL;
    	RegisterClass((struct tagWNDCLASSA *)wc);
    }
    
    //////////////
    // Punto de entrada ('WinMain' : must be '__stdcall')
    
    int mb=0;
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE h0, LPSTR lpCmdLine, int nCmdShow){
    
    	HWND hWin;
    	WNDCLASS wc = {0};
    
    	RegistrarClaseVentana(&wc);
    
    	int ancho=300;
    	int alto=156;
    	int dim[2];
    	getCentradoGlobal(dim, ancho, alto);
    
    	unsigned long Estilo1 = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
    
    	hWin = CreateWindow( wc.lpszClassName,
    		                 "Convertidor a Binario",
    						 Estilo1,
    						 dim[0],
    						 dim[1],
    						 ancho,
    						 alto,
    						 NULL,
    						 NULL,
    						 hInst,
    						 NULL);
    
    //	ShowWindow(hWin,nCmdShow);
    	ShowWindow(hWin, SW_SHOWDEFAULT);
    
    	//UpdateWindow(hWin);
    
    	MSG msg;
    	while(GetMessage(&msg, NULL, 0, 0)>0) {
    		if(!mb) {
    		  MessageBox(hWin, "By BS","Convertidor a Binario",MB_OK);
    		  mb=1;
    		}
    		if (!IsDialogMessage(hWin, &msg)) {
    		  TranslateMessage(&msg);
    		  DispatchMessage(&msg);
    
    		}
    	}
    
        return msg.wParam;
    }
    
    //===
    
    void getCentradoGlobal(int vXY[2], int h, int w)
    {
    	RECT rcP;
    	GetWindowRect(GetDesktopWindow(), &rcP);
    	int centerX = (rcP.right/2)-(w/2)-(w/4);
    	int centerY = (rcP.bottom/2)-(h/2)+(h/6);
    	vXY[0]=centerX;
    	vXY[1]=centerY;
    }
    
    To make the conversion
    Code:
    unsigned long Bin(unsigned long n1,int base1,int base2)
    {
    	unsigned long alg,mult=1,n2=0;
    	while(n1 > 0) {
    		alg = n1 % base1;
    		n1 /= base1;
    		n2 += (alg*mult);
    		mult *= base2;
    	}
    	return n2;
    }
    
     

    Attached Files:

Share This Page