Thread: creating a skin

    #1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    creating a skin


    I want to make the background of my window be set to .bmp file my friend made for me. i have been scouring the web for how to do this, and all i have encountered is a vague description of using CWndImage that i really cant understand too well. From what i've been able to comprehend, it seems as though i have to create a seperate dialog box just to do this??? Can i not just set the background to that bmp without doing that??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    ... you seem to be using MFC ... i can only tell you the WinAPI way, but maybe this still helps:

    you load the bitmap via LoadBitmap(). In the WndProc, you capture WM_PAINT and there you BitBlt()/StretchBlt() the bitmap to the DC you get via GetDC(handle) where "handle" is your windowīs HWND.
    (maybe select() first? not sure - i am at home and donīt have any code about this around and i am too lazy right now to look it up on the MSDN)

    ...donīt forget to ReleaseDC() the device context after this
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    thnks for ur response, i am still kind of confused though b/c i am new to api programming. here is my code could u tell me a little more info plz..

    PHP Code:
    // cMortMain.cpp  -- main file for mortgage calculator

    #include <fstream.h>
    #include <iomanip.h>
    #include "cMortgage.h"
    #include <cstdlib>
    #include <afxwin.h>
    #include "resource.h"
    #include <commdlg.h>  // for color dialog
    // - - - prototype - - - 

    LRESULT CALLBACK MyWndProc (HWND handleUINT msg,
                                 
    WPARAM wPLPARAM lP) ;

    BOOL    CALLBACK DialogProc HWND handleUINT msg,
                                 
    WPARAM wPLPARAM lP) ;

    HINSTANCE hInst;
    cMort Mortgage1;

    int WINAPI WinMain (HINSTANCE hInstanceHINSTANCE hPrevInstance,
                        
    PSTR sCmdLineint iCmdShow)
    {
      
    HWND         hWnd ;
      
    MSG          msg ;
      
    WNDCLASS     wc // This is just a struct
      
    TCHAR wcName[] = TEXT("Mortgage Calculator") ;

      
    hInst hInstance;
      
    // Step 1: Register a window 'class'
      
    wc.lpszClassName wcName ;
      
    wc.lpfnWndProc   MyWndProc// Name of function that handles messages
                                       // to windows based on this 'class'
      
    wc.style         CS_HREDRAW CS_VREDRAW CS_DBLCLKS  ;
      
    wc.hIcon         LoadIcon   (NULLIDI_APPLICATION) ;
      
    wc.hCursor       LoadCursor (NULLIDC_ARROW) ;
      
    // Option 1 for specifying a white window background
      // wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
      // Option 2 for specifying a nice window background color 
       
    HBRUSH  hBrush;
       
    hBrush CreateSolidBrush (RGB(25,5,150));
       
    wc.hbrBackground hBrush
      
    // End window background options
      
    wc.lpszMenuName  MAKEINTRESOURCE (IDR_MENU1);
      
    wc.hInstance     hInstance ;
      
    wc.cbClsExtra    ;
      
    wc.cbWndExtra    ;

      if (!
    RegisterClass (&wc)) return 0;

      
    // Step 2: Create window based on above registered 'class'
      
    hWnd CreateWindow
             
    (
              
    wcName,        // The name specifying the registed class
              
    TEXT ("Mortgage Calculator"), // The window caption
              
    WS_OVERLAPPEDWINDOW// window style
              
    20,             // window position, left edge
              
    30,             // window position, top edge
              
    500,            // initial window width
              
    300,            // initial window height
              
    NULL,           // parent window handle
              
    NULL,           // window menu handle
              
    hInstance,      // program instance handle
              
    NULL            // creation parameters
             
    ) ;                     
         
      
    // Step 3: Show and update the window
      
    ShowWindow (hWndiCmdShow) ;
      
    UpdateWindow (hWnd) ;

      
    // Step 4: Retrieve messages from message queue.
      
    while (GetMessage (&msgNULL00))
      {
         
    TranslateMessage (&msg) ;
         
    DispatchMessage (&msg) ;// Give message back to OS.
                                 // Sends to appro 'WndProc'
      
    }
      
    DeleteObject (hBrush); // Comment out 
                             // if use background option 1
      
    return msg.wParam ;
    }


    // Note: Variables global so both
    // MyWndProc() and DialogProc() can access them.
    #define   IDC_MYLISTBOX    764


    LRESULT CALLBACK MyWndProc (HWND hWndUINT message,
                                
    WPARAM wParamLPARAM lParam)
    {
      static 
    COLORREF mTextColor RGB(255,0,0);
      static 
    HFONT    hFont;
      static 
    LOGFONT  lf;  // struct hold font details
      
    static HWND hListBox1;
      
    // - - - - - - - - -
            
      
    UINT           NotificationCode;
      
    int            idx;    
      
    CString        tS;   // temp string
      
    HDC            hDC ;  // 
      
    PAINTSTRUCT    ps  ;  // Invalid window info
      
    double pay1 0IntAmt 0Owe 0Balance 0;
      
    int t 1;
      
    char s[100], tSv[100];
      
    ofstream fout;
         
      switch (
    message)
      {
      case 
    WM_CREATE:
           
    hListBox1 CreateWindow (TEXT("listbox"), NULL,
                                
    WS_CHILD WS_VISIBLE |WS_BORDER |WS_VSCROLL |LBS_NOTIFY,
                                
    40,30400200,
                                
    hWnd, (HMENU)IDC_MYLISTBOXhInstNULL);
                            
           return 
    ;
              
      case 
    WM_PAINT:  // Code must update the window COMPLETELY
           
    hDC BeginPaint (hWnd, &ps) ; // Get the device context handle
           
    TextOut (hDC,40,10,"Payment Schedule",16);
           
    SelectObject (hDChFont);
           
    SetTextColor (hDCmTextColor);
           
    SetBkMode (hDC,TRANSPARENT) ; 
           
    EndPaint (hWnd, &ps) ;
           return 
    ;

      case 
    WM_CHAR:   
           
    InvalidateRect (hWndNULLTRUE); // send WM_PAINT msg
           
    return 0;

      case 
    WM_KEYDOWN
           return 
    0;

      case 
    WM_LBUTTONDOWN :
           return 
    0;
      case 
    WM_COMMAND:
          
    // LOWORD(wParam) is id of menu option
          
    switch (LOWORD (wParam))
          {
          case 
    ID_EXIT:    
              
    SendMessage(hWndWM_DESTROY0,0);
              return 
    0;
          case 
    ID_OPTIONS_ENTER:
              
    DialogBox(hInstMAKEINTRESOURCE(IDD_DIALOG1),
                        
    hWndDialogProc);
              
    InvalidateRect(hWndNULL,TRUE);
              
    pay1 Mortgage1.calcPayment();
              
    sprintf(s,"%-10s %-12s %-10s %-12s %-14s""Pay #""Interest($)""Owe($)""Payment($)"
                        
    "New Balance($)");
              
    tS s;
              
    SendMessage (hListBox1LB_ADDSTRING0,(LPARAM)(LPCTSTR)tS);
              
    tS "--------------------------------------------------------------------------------------------------";
              
    SendMessage (hListBox1LB_ADDSTRING0,(LPARAM)(LPCTSTR)tS);
              
    Balance Mortgage1.getBorrowed();
              for (
    t<= (Mortgage1.getNumPayments()*Mortgage1.getYears()); t++)
              {
                
    IntAmt = (1.0/(Mortgage1.getNumPayments())*Mortgage1.getRate()*Balance);
                
    Owe Balance IntAmt;
                
    Balance Owe pay1;
                
    sprintf(s,"%10i %12.2f %10.2f %12.2f %14.2f"tIntAmtOwepay1Balance);
                
    tS s;
                
    SendMessage (hListBox1LB_ADDSTRING0,(LPARAM)(LPCTSTR)tS);
              }

              return 
    0;
          case 
    ID_CLEAR:
              
    SendMessage (hListBox1LB_RESETCONTENT0,0);
              return 
    0;
          case 
    ID_OPTIONS_SAVE:  //*****RIGHT HERE IS WHERE FILE IS SAVED***********************
              
    fout << setprecision(2)<< setiosflags(ios::fixed);
              
    fout.open ("LoanSchedule.txt");
              
    fout << "  Loan Repayment Schedule\n";
              
    fout << "\n            Amount borrowed ? " << Mortgage1.getBorrowed();
              
    fout << "\n       Annual interest rate ? " << Mortgage1.getRate();
              
    fout << "\nNumber of payments per year ? " << Mortgage1.getNumPayments();
              
    fout << "\n            Number of years ? " << Mortgage1.getYears();
              
    fout << "\n\n Each payment is : " << Mortgage1.getPayment() << "\n";
              
    Balance Mortgage1.getBorrowed();
              
    fout << "\nPay #   Interest    Owe      Payment    New balance\n";
              
    fout << setprecision(2)<< setiosflags(ios::fixed);

              for ( 
    1<= (Mortgage1.getNumPayments()*Mortgage1.getYears()); t++)
              {
                
    IntAmt = (1.0/(Mortgage1.getNumPayments())*Mortgage1.getRate()*Balance);
                
    Owe Balance IntAmt;
                
    Balance Owe pay1;
                
    fout << "\n" << setw(4) << t
                     
    << setw(10) << IntAmt 
                     
    << setw(12) << Owe
                     
    << setw(10) << Mortgage1.getPayment()
                     << 
    setw(12) << Balance;
              }
              
    MessageBox(hWnd,"File has been saved""Message",MB_OK);
              return 
    0;
          case 
    IDC_MYLISTBOX:
              
    NotificationCode HIWORD(wParam);
              if (
    NotificationCode ==LBN_SELCHANGE )
              {
                
    idx SendMessage (hListBox1LB_GETCURSEL,0,0);
                
    SendMessage (hListBox1LB_GETTEXTidx, (LPARAM)tSv);
              }
              return 
    0;

          }
      case 
    WM_DESTROY:
           
    PostQuitMessage (0);
           
    fout.close();
           return 
    ;
      }

      
      return 
    DefWindowProc (hWndmessagewParamlParam) ;
    }

    BOOL    CALLBACK DialogProc HWND hDlgUINT msg,
                                 
    WPARAM wPLPARAM lP)
    {
      
    char   tmpStr[100];        //temp string
      
    const int Z 0;            //zero constant
      
    char *dPointer NULL;    //pointer for atof()
      
    double tempDub 0;        //temp variables for utility
      
    int tempInt 0;

      switch (
    msg)
      {
      case 
    WM_INITDIALOG:        //zero all edit boxes
        
    SetDlgItemInt  (hDlgIDC_MBORROWEDZTRUE);
        
    SetDlgItemInt  (hDlgIDC_MRATEZTRUE    );
        
    SetDlgItemInt  (hDlgIDC_MPAYMENTSZTRUE);
        
    SetDlgItemInt  (hDlgIDC_MYEARSZTRUE   );
        return 
    TRUE;

      case 
    WM_COMMAND:
        switch(
    LOWORD(wP))
        {
         case 
    IDOK :            //extract values from edit boxes to mortgage class variables
           
    GetDlgItemText (hDlg,IDC_MBORROWEDtmpStr100);
           
    dPointer tmpStr;
           
    tempDub atof(dPointer);
           
    Mortgage1.setBorrowed(tempDub);
           
    GetDlgItemText (hDlg,IDC_MRATEtmpStr100);  
           
    dPointer tmpStr;
           
    tempDub atof(dPointer);
           
    Mortgage1.setRate(tempDub);
           
    tempInt GetDlgItemInt (hDlg,IDC_MPAYMENTSNULL100);
           
    Mortgage1.setNumPayments(tempInt);
           
    tempInt GetDlgItemInt (hDlg,IDC_MYEARSNULL100);
           
    Mortgage1.setYears(tempInt);
           
    EndDialog (hDlg,TRUE);
           return 
    TRUE;

         case 
    IDCANCEL:
           
    EndDialog (hDlg,FALSE);
           return 
    TRUE;
        }
        break;
      }
      return 
    FALSE;

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    So it is WinAPI and not MFC that you are doing :)

    hWnd = CreateWindow ...
    this is the HWND i refered to
    case WM_PAINT: // Code must update the window COMPLETELY
    hDC = BeginPaint (hWnd, &ps) ; // here you have the HDC already, no need for getDC() then.
    // here goes the function to paint the bmp to the DC
    TextOut (hDC,40,10,"Payment Schedule",16);
    SelectObject (hDC, hFont);
    SetTextColor (hDC, mTextColor);
    SetBkMode (hDC,TRANSPARENT) ; // not sure if/how this could interfere with the bitmap... ?
    EndPaint (hWnd, &ps) ;
    return 0 ;
    [/quote]
    I modifed the comments...

    The blitter functions (BitBlt, StretchBlt) look confusing at the first view because of the many parameters. But they are IMHO self-explaining on the second view.

    Load the bitmap in the WM_CREATE part and free it in the WM_DESTROY part.
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i appreciate ur help, but excuse my stupidity plz. i thought that i had to go to file<open and open the bmp, but when i try to do this it says " the file has been loaded correctly but b/c it contains more than 256 colors it cannot be loaded into the bitmap editor."
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    I am not sure how you are trying to load the bitmap (you did not tell yet which environment you are using, but i guess MSVC++)
    The "256 color error" though makes me think that you are using BC++ because borlandīs bitmap editor didnīt support 24bit bitmaps for a long time (not sure if it does today...)

    On the other hand, this is not really important because:
    LoadBitmap() is a GDI function. It returns a HBITMAP that iirc can be used in BitBlt() / StretchBlt() functions. If it does not work, you can use GetDC(hBitmap) to get a device context to the handle and BitBlit()/StretchBlt() from that DC to your windowīs DC.

    Could you make it work like that?

    I have to add that i am doing very few WinAPI stuff - only when i really need to. Mostly i am doing Delphi (the WinAPI is important there too for problems like yours, but itīs much less complicated as i get DCs and handles automatically in Delphi. But other than that, it is the very same as in C).

    If you canīt get it to work, "worstcase" would be to wait till monday. Either one of the C pros here will have answered or I am at work and have access to the programs i did there so i can post some "real" code. I just sent a reminder to myself :)

    I wonīt reply today anymore, itīs 00:00 here and iīm going to bed now... good night! and good luck.
  12. #7
  13. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    yes i am using msvc++. i was going to file menu, and then selecting open and selecting my bmp file. i am not even sure if this is the correct way to do this. In order to use the LoadBitmap() function i was assuming that i had to have the bmp already loaded as a resource. I can't get it to load as a resource, or perhaps i am doing it incorrectly b/c i have never done this b4. Thanks for ur help, i do appreciate it. If anyone else out there knows what im doing wrong please tell me the correct way.
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ahh finally i have figured this out. a little more complex than i thought it would be. i ended up needing a few more functions but i found a tutorial that explained it pretty well. MHirsch thanks for ur help it got me pointed in the right direction.
  16. #9
  17. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    >> SelectObject (hDC, hFont);

    Here you have a memory leak each paint msg.



    Also I can not see any init/create for the font.

    You must 'catch' any GDI resource (and replace ) returned from a SelectObject() call.

    Any drawing GDI resource or BMP can not be deleted/released while selected into a HDC.

    >>Load the bitmap in the WM_CREATE part and free it in the WM_DESTROY part.

    Make sure you make its handle static.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo