473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TextOut in a Win32 Application

114 New Member
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
3 12637
xoinki
110 New Member
hi,
Did u try Updatewindow() after showwindow?
Xoinki
Sep 4 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
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
Firecore
114 New Member
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

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

Similar topics

5
1815
by: Chi Tang | last post by:
Hi, I need to implement a stand alone gui application which need to call some other regular DLL functions (unmanaged code). Should I use .net with C# or just use Win32 API to write this exe? Is it easy to call any DLL function from .net/C# program? The 2nd question is that on a win32 app, I can use 'UNICODE' to support localization...
1
4019
by: Eitan | last post by:
Hello, I am using Visual Studio.net 2003. I am trying to find how to create Win32 DLL & Win32 Static Library. When going through the wizard it does not show me the option for these two project types. Any Idea? Eitan
8
5051
by: dthom | last post by:
Hi, I have a C# service application - and a Win32 / C++ service application running on my system. I need someway to interact between those two - the Win32 application is pure Win32 - so im not using any components or anything like that. The 2 alternatives i could think of was, messages or creating a TCP/IP socketserver on the C# end,...
8
4753
by: Tony Johansson | last post by:
Hello! I just wonder what the difference is between a native win32-app and MFC. What I know is that you can use Win32 API in both cases. I think an MFC application is connected with a GUI which a win32 is not. You may correct me if I'm wrong on this point. //Tony
3
41768
by: sandycat05 | last post by:
Hello all, I am a new Perl programmer. Below is the beginnings of a code that I am using to manipulate an Excel spreadsheet via Perl using win32::OLE. However, what I'd like to do is the following: instead of either opening a file or creating a new one, I'd like to do BOTH. I was thinking of creating a loop where I could basically say something...
3
1507
by: Gil | last post by:
I need to create a web page in which its content should be controlled by some win32 application. this application may add or remove some gif images from the page and it should be smooth as possible as can. Can someone here direct me how to start that. Thanks in advance Gil
0
1560
by: coss | last post by:
Hi, I`m currently working with Borland C++Builder v6.0 In my program at it's startup some text should be read from file and displayed in the form, on top of the image. I use Canvas->TextOut(), but text is not displayed. I quess some layer covers it, but can't find way how to fix it. But when the program is already loaded and I press button that...
0
1915
by: =?Utf-8?B?Q29saXZpZXI=?= | last post by:
If anyone can help me with this I would really appreciate it: I have an assembly into which I have linked a manifest file as a Win32 resource. This is necessary since I want to use a class in this assembly with registration-free COM interop from a Win32 application. I also want to use this assembly with a .Net application that I want to...
1
4510
by: user1357 | last post by:
Hi all, i am trying to run the following code use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel';
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.