This example is composed of three files:
This example is composed of three files:
//============================================================================= //RESOURCES: IMAGES - Copyright © 2000,2005 Ken Fitlike //============================================================================= //API functions used: CreateWindowEx,DefWindowProc,DispatchMessage,GetMessage, //GetSystemMetrics,LoadImage,MessageBox,PostQuitMessage,RegisterClassEx, //ShowWindow,UpdateWindow,TranslateMessage,UnregisterClass,WinMain. //============================================================================= //This example demonstrates the use of image resources. Only bitmaps, cursors //and icons are natively supported. The bitmap is used to create a window class //brush for painting the window's client area, the cursor is used as the window //class cursor and the icon as the window class icon. //============================================================================= #include <windows.h> //include all the basics #include <tchar.h> //string and other mapping macros #include <string> #include "resources.h" //define an unicode string type alias typedef std::basic_string<TCHAR> ustring; //============================================================================= //message processing function declarations LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //non-message function declarations inline int ErrMsg(const ustring&); //============================================================================= int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd) { ustring classname=_T("SIMPLEWND"); WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class' wcx.cbSize = sizeof(WNDCLASSEX); wcx.lpfnWndProc = WndProc; //wnd Procedure pointer wcx.hInstance = hInst; //app instance //use 'LoadImage' to load wnd class icon and cursor as it supersedes the //obsolete functions 'LoadIcon' and 'LoadCursor', although these functions will //still work. Once the window is destroyed, gdi resources (brush, cursor, icon) //will automatically be freed by the system; you can free these resources //manually if you prefer by calling UnregisterClass once the window is //destroyed. wcx.hIcon = reinterpret_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,0,0, LR_DEFAULTCOLOR)); wcx.hCursor = reinterpret_cast<HICON>(LoadImage(hInst, MAKEINTRESOURCE(IDC_CURSOR1), IMAGE_CURSOR,0,0, LR_DEFAULTCOLOR)); //load the bitmap resource and create a brush from it which will be used to //paint the window background. HBITMAP hBmp=reinterpret_cast<HBITMAP>(LoadImage(hInst, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP,0,0, LR_CREATEDIBSECTION)); wcx.hbrBackground = CreatePatternBrush(hBmp); DeleteObject(hBmp); //release bitmap resources wcx.lpszClassName = classname.c_str(); //the window 'class' (not c++ class) has to be registered with the system //before windows of that 'class' can be created if (!RegisterClassEx(&wcx)) { ErrMsg(_T("Failed to register wnd class")); return -1; } int desktopwidth=GetSystemMetrics(SM_CXSCREEN); int desktopheight=GetSystemMetrics(SM_CYSCREEN); HWND hwnd=CreateWindowEx(0, //extended styles classname.c_str(), //name: wnd 'class' _T("Resources: Images"), //wnd title WS_OVERLAPPEDWINDOW, //wnd style desktopwidth/4, //position:left desktopheight/4, //position: top desktopwidth/2, //width desktopheight/2, //height 0, //parent wnd handle 0, //menu handle/wnd id hInst, //app instance 0); //user defined info if (!hwnd) { ErrMsg(_T("Failed to create wnd")); return -1; } ShowWindow(hwnd,nCmd); UpdateWindow(hwnd); //start message loop - windows applications are 'event driven' waiting on user, //application or system signals to determine what action, if any, to take. Note //that an error may cause GetMessage to return a negative value so, ideally, //this result should be tested for and appropriate action taken to deal with //it(the approach taken here is to simply quit the application). MSG msg; while (GetMessage(&msg,0,0,0)>0) { TranslateMessage(&msg); DispatchMessage(&msg); } UnregisterClass(classname.c_str(),hInst); return static_cast<int>(msg.wParam); } //============================================================================= LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); //signal end of application return 0; default: //let system deal with msg return DefWindowProc(hwnd,uMsg,wParam,lParam); } } //============================================================================= inline int ErrMsg(const ustring& s) { return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION); } //=============================================================================
//============================================================================= //Resource script constants for Resources: Images example. //============================================================================= #define IDD_DIALOG1 200 #define IDB_BITMAP1 300 #define IDC_CURSOR1 400 #define IDI_ICON1 500
//============================================================================= //RESOURCES: IMAGES - Copyright (c) 2006 Ken Fitlike //resource script //============================================================================= #if !defined __BORLANDC__ #include <afxres.h> #endif #include "resources.h" //============================================================================= //Resource languages: Codes for languages and sub-languages are declared in //winnt.h eg. for US english replace SUBLANG_ENGLISH_UK with SUBLANG_ENGLISH_US //eg. for FRENCH replace LANG_ENGLISH with LANG_FRENCH and then replace //SUBLANG_ENGLISH_UK with either SUBLANG_FRENCH,SUBLANG_FRENCH_BELGIAN, //SUBLANG_FRENCH_CANADIAN,SUBLANG_FRENCH_SWISS,SUBLANG_FRENCH_LUXEMBOURG, //SUBLANG_FRENCH_MONACO depending on which national or regional variation of //the language corresponds best with your requirements. // LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK //============================================================================= //Dialog //============================================================================= IDD_DIALOG1 DIALOGEX 0, 0, 252, 149 STYLE DS_CENTER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME CAPTION "Dialog Box: Modal" FONT 8, "MS Sans Serif" { DEFPUSHBUTTON "OK",IDOK,22,62,50,14 PUSHBUTTON "Cancel",IDCANCEL,111,63,50,14 } //============================================================================= //Bitmap //============================================================================= IDB_BITMAP1 BITMAP "tile.bmp" //============================================================================= //Cursor //============================================================================= IDC_CURSOR1 CURSOR "bluearrow.cur" //============================================================================= //Icon //============================================================================= IDI_ICON1 ICON "icon1.ico" //=============================================================================