Nano-X program design, #2: "Hello World 」
Jollen
Issued at pm, 2017l 26,200
Preface
Jollen: I have many ideas about "learning. My opinion is...
Learn to compile any program Program program or application
Framework, because it is not a learning method of "writing data", it cannot take learning data as the primary component; the correct method should be "subjective", that is, "hello"
World (I think this has become the annual success !), Then let's look at the examples of different subjects, because the examples of each subject have their own purposes, so we can pass through examples, we will learn from targeted practices.
Secondly, each example must contain the concept of "memory inclusion". In addition to learning practices, we also need to dig out the internal concepts to study them, so as to improve our abilities. Jollen is writing these program scripts, program comments, and
During the course of application framework teaching, I used this kind of thinking to plan the entire project, and it was also my own practice.
In this way and on the premise, the job of "The Elders cannot teach and the students must come by themselves" is:
1. Security (of course !)
2. zookeeper API Manual
3. zookeeper API Manual
4. API query capabilities and skills
5. Check the API manual for Code 1.
6. zookeeper examples
7. Modify the example of logon.
I would like to welcome your comments or disagree with each other.
Nano-X's "Hello World"
Hello. C is our first nano-X program. In this example, we want to learn the basic nano-x programming concepts as follows:
• GC (graphics context)
• Windows)
· Nano-x Programming process
· Event handling
Let's take a look at example: Hello. C.
Below isHello. c
Complete program example:
/*
* Copyright(c) 2003,2004 www.jollen.org
* - Microwindows nano-X API example.
* - hello.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "nano-X.h"
#include "nxcolors.h"
GR_WINDOW_ID wid;
GR_GC_ID gc;
void event_handler (GR_EVENT *event);
int main (void)
{
if (GrOpen() < 0)
{
fprintf (stderr, "GrOpen failed");
return -1;
}
gc = GrNewGC();
GrSetGCForeground (gc, 0xFF0000);
wid = GrNewWindowEx(GR_WM_PROPS_APPFRAME |GR_WM_PROPS_CAPTION |GR_WM_PROPS_CLOSEBOX,"jollen.org",GR_ROOT_WINDOW_ID, 0, 0, 200, 200, 0xFFFFFF);
GrSelectEvents(wid, GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_EXPOSURE);
GrMapWindow(wid);
GrMainLoop(event_handler);
return 0;
}
void event_handler (GR_EVENT *event)
{
switch (event->type)
{
case GR_EVENT_TYPE_EXPOSURE:
GrText(wid, gc, 50, 50, "Hello World", -1, GR_TFASCII);
break;
case GR_EVENT_TYPE_CLOSE_REQ:
GrClose();
default: break;
}
}
Then write down the concept
After learning how to use nano-X to output "Hello World", we can pick out several important ideas for addition:
1. GC (graphics context)
In the nano-x system, we can copy the GC into a uniform distribution. We can analyze the nano-x Data on the GC. CallGrnewgc ()
Function allows you to create a GC. If GC is successfully created
The nano-X server returns a gc id. We will create a GC at the beginning of the program. In addition to the GC, GC resources can also be processed or selected through the selection function for Region processing.
2. Windows)
The nano-x dialog box is called at the beginning of the program.Grnewwindow ()
If the function is successfully created, the window can be obtained.
Id. The border window has the border frame (Border) and the border type. The border window setting can be specified when the border window is created, or modify it through window manager.
Conclusion: Nano-x Programming process
The following describes how to configure the nano-X program:
1. initialize the zookeeper with the server. CallGropen ()
Function. If the return value is less than 0, it indicates that the operation cannot be performed with nano-X server. The program syntax is as follows:
If (gropen () <0 ){
Fprintf (stderr, "gropen failed ");
Return-1;
}
2. Create a new GC:
Gr_gc_id GC;
GC = grnewgc ();
If the row is successfully written, the ID of the GC is returned. The resource type is different fromGr_gc_id
.
3. Set the foreground color of GC:
Grsetgcforeground (GC, 0xff0000 );
Specify the gc id and set its foreground color.
4. CallGrnewdomainwex ()
Create a new function window:
Gr_window_id WID;
WID = grnewjavaswex (gr_wm_props_appframe |
Gr_wm_props_caption |
Gr_wm_props_closebox,
"Jollen.org ",
Gr_root_window_id,
0, 0,200,200, 0 xffffff );
5. Select an event. CallGrselectevents ()
Choose the events we want to handle:
Grselectevents (WID, gr_event_mask_close_req | gr_event_mask_exposure );
ToHello. c
For example, this indicates that we want to handle the problem in the window (Gr_event_mask_close_req
) And upper window display (Gr_event_mask_exposure
) Event.
6. CallGrmapwindow ()
Function data display window, and generateGr_event_mask_exposure
Event:
Grmapwindow (WID );
7. CallGrmainloop ()
The function is sent to the nano-X Distribution circle:
Grmainloop (event_handler );
DistributeGrmainloop ()
Function refers to the function of processing an event.
Handler ). In the dispatch cycle, if the event we specified is generated, the call event handling function is displayed, therefore, we must handle events in event processing records.
Event handling
When an event is generated in the dispatch circle, it calls the event handling function. Below isHello. c
Event handling function:
Void event_handler (gr_event * event)
{
Switch (Event-> type)
{
Case gr_event_type_exposure:
Grtext (WID, GC, 50, 50, "Hello World
",-1, gr_tfascii );
Break;
Case gr_event_type_close_req:
Grclose ();
Default: break;
}
}
In the dispatch circle, an event notification is sent to the event processing function, that is, the event changes in the program. We can determine the incidents produced by this change and make corresponding handling:
- Gr_event_type_exposure
: This event is generated in the dialog box. We call this event after the event is generated.Grtext ()
The function displays text on the dataset.
- Gr_event_type_close_req
: This event occurs when the window is closed. We call after this eventGrclose ()
Function Correlation and nano-x
The Zookeeper of the server.