By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

TextOut in a Win32 Application

100+
P: 114
Does anyone know how to use this?
I was browsing a few tutorials on the net, and I saw one that shows how to display stuff on the screen.
I added a few events.
This is my code:
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h> // FOR ANY DIFFICULTIES, REFER TO THE MSDN. IT IS VERY USEFUL.
  2.  
  3. HWND hwndMain;
  4.  
  5. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);//CALLBACK function prototype
  6.  
  7.  
  8. int WINAPI WinMain(HINSTANCE hInstance, 
  9.                    HINSTANCE hPrevInstance, 
  10.                    LPSTR lpCmdLine, 
  11.                    INT nCmdShow)
  12. {
  13.     MessageBox(NULL,"This is a test of Lesson 02", "Lesson 02 Test",MB_OK);
  14.  
  15.     MSG msg; //var to store messages
  16.     WNDCLASSEX window; //WINDOW class information
  17.  
  18.     //initialize all values of window struct to zero
  19.     ZeroMemory(&window,sizeof(WNDCLASSEX));
  20.  
  21.     window.cbSize = sizeof(WNDCLASSEX);// Window Size. Must always be sizeof(WNDCLASSEX)
  22.  
  23.     window.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles. These are necessary for a proper functioning window
  24.  
  25.     window.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure
  26.  
  27.     window.cbClsExtra = 0; //Specifies the number of extra bytes to allocate following the window-class structure. The system initializes the bytes to zero. 
  28.  
  29.     window.cbWndExtra = 0; //Specifies the number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application uses WNDCLASSEX to register a dialog box created by using the CLASS directive in the resource file, it must set this member to DLGWINDOWEXTRA. 
  30.  
  31.     window.hInstance = hInstance; //Instance of the application. Notice that it is the same one from the WinMain prototype
  32.  
  33.     window.hIcon = NULL; // Icon of the window class
  34.  
  35.     window.hCursor = LoadCursor(NULL, IDC_ARROW);// Class cursor.
  36.  
  37.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW); /*Handle to the class background brush. 
  38.                                                 This member can be a handle to the physical brush to be used for painting the background, or it can be a color value. 
  39.                                                 A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). 
  40.                                                 */
  41.     window.lpszMenuName = NULL; //Menu resource
  42.  
  43.     window.lpszClassName = "Lesson2"; // Name of this class
  44.  
  45.     window.hIconSm = LoadIcon(NULL,"logo.bmp"); // Small icon for this class
  46.  
  47.     // Register this window class with MS-Windows
  48.     if (!RegisterClassEx(&window))
  49.         return 0;
  50.  
  51.  
  52.     //Now to create the window!
  53.  
  54.     // Create the window
  55.  
  56.     hwndMain = CreateWindowEx(0, //Extended window style
  57.                 "Lesson2", // Window class name
  58.                 "Lesson 2 - A simple win32 application", // Window title
  59.                 WS_OVERLAPPEDWINDOW, // Window style
  60.                 CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window
  61.                 CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window
  62.                 HWND_DESKTOP, // HWND of the parent window (can be null also)
  63.                 NULL, // Handle to menu
  64.                 hInstance, // Handle to application instance
  65.                 NULL); // Pointer to window creation data
  66.  
  67.  
  68.     // Check if window creation was successful
  69.     if (!hwndMain)
  70.         return 0;
  71.  
  72.     //Make window visible
  73.  
  74.     ShowWindow(hwndMain,SW_SHOW);
  75.  
  76.     // Process messages coming to this window
  77.  
  78.     while (GetMessage(&msg,NULL,0,0))
  79.     {
  80.         TranslateMessage(&msg);
  81.         DispatchMessage(&msg);
  82.     }
  83.  
  84.  
  85.  
  86.     // return value to the system
  87.  
  88.     return msg.wParam;
  89. }
  90.  
  91. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  92. {
  93.     PAINTSTRUCT paintStruct;
  94.  
  95.  
  96.     HDC hDC;
  97.     char message[] = "KILL ALL HUMANS";
  98.  
  99.  
  100.     switch (msg)
  101.     {
  102.         case WM_DESTROY:
  103.             // User closed the window
  104.             MessageBox(NULL, "Exit","CAPTION",NULL);
  105.             PostQuitMessage(0);
  106.             break;
  107.         case WM_LBUTTONDOWN:
  108.             //User pressed the left mouse button
  109.             MessageBox(hwnd,"BEEP","BEEP",MB_OK | MB_APPLMODAL);
  110.             MessageBeep(MB_OK);
  111.             MessageBox(NULL,"PRINTING TO SCREEN","CAPTION",NULL);
  112.  
  113.             //paint to screen
  114.             hDC = BeginPaint(hwnd,&paintStruct);
  115.  
  116.             /* Set Text color to blue */
  117.             SetTextColor(hDC, COLORREF(0x00FF0000));
  118.  
  119.             /* Display text in the middle of the window */
  120.             TextOut(hDC,150,150,message,sizeof(message)-1);
  121.  
  122.             //Tell OS you are done painting
  123.             EndPaint(hwnd, &paintStruct);
  124.  
  125.             return 0;
  126.             break;
  127.  
  128.         case WM_CHAR:
  129.             //User Pressed a character from the keyboard
  130.             MessageBox(NULL, "You Pressed a Key","CAPTION",NULL);
  131.             break;
  132.     //    case WM_ACTIVATE:
  133.     //        MessageBox(NULL,"THE FORM IS ACTIVE","CAPTION",NULL);
  134.     //        break;
  135.  
  136.  
  137.         default:
  138.             // Call the default window handler
  139.             return DefWindowProc(hwnd,msg,wParam,lParam);
  140.     }
  141.     return 0;
  142. }
  143.  
I searched and searched and I just cand find why it does not work.
Have I got my theory wrong?
Sep 4 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 110
hi,
Did u try Updatewindow() after showwindow?
Xoinki
Sep 4 '07 #2

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1.         case WM_LBUTTONDOWN:
  2.             //User pressed the left mouse button
  3.             MessageBox(hwnd,"BEEP","BEEP",MB_OK | MB_APPLMODAL);
  4.             MessageBeep(MB_OK);
  5.             MessageBox(NULL,"PRINTING TO SCREEN","CAPTION",NULL);
  6.  
  7.             //paint to screen
  8.             hDC = BeginPaint(hwnd,&paintStruct);
  9.  
  10.             /* Set Text color to blue */
  11.             SetTextColor(hDC, COLORREF(0x00FF0000));
  12.  
  13.             /* Display text in the middle of the window */
  14.             TextOut(hDC,150,150,message,sizeof(message)-1);
  15.  
  16.             //Tell OS you are done painting
  17.             EndPaint(hwnd, &paintStruct);
  18.  
  19.             return 0;
  20.             break;
  21.  
BeginPaint and EndPaint are only for use while handling a WM_PAINT message (I suggest you look them up on MSDN).

UpdateWindow forces a the window to be sent a WM_PAINT message forcing a screen update if you have implemented a WM_PAINT handler.

You should either
  1. Call GetDC and Release DC instead of BeginPaint and EndPaint in you WM_LBUTTONDOWN handler
  2. Implement the WM_PAINT handler and move the code that draws to the screen into that handler
Sep 4 '07 #3

100+
P: 114
BeginPaint and EndPaint are only for use while handling a WM_PAINT message (I suggest you look them up on MSDN).

UpdateWindow forces a the window to be sent a WM_PAINT message forcing a screen update if you have implemented a WM_PAINT handler.

You should either
  1. Call GetDC and Release DC instead of BeginPaint and EndPaint in you WM_LBUTTONDOWN handler
  2. Implement the WM_PAINT handler and move the code that draws to the screen into that handler
I see. I shall try that
Sep 5 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.