A Treeview control made by ucgui!

Source: Internet
Author: User
Tags call back

We made a Treeview Control Using ucgui! The Treeview control is not available in my version, so I made one myself.

This is ugly!

My personal mind is relatively simple, so the idea is also relatively simple! The subsequent code is also very simple.

Mainly as follows:

A node is placed in a form. For example, if a child node exists from the root node, a form is created. Its parent form is the form of the parent node, it is nested like a tree. This mainly facilitates the execution of ucgui message processes, such as node expansion and collapse, and the processing of length and width is very convenient.

The effect I have done is like this. Ah, it's too ugly. Programmers still cannot do the interface. (because it is on an embedded device and has no function, I just took a picture, the tab page on the right is also customized, without the built-in uggui ):

The Code is as follows (I didn't compile it separately. If there is any definition macro missing, I will do it by myself). If you are interested, please take a look.

// ============================ Header file ============== ==================/// typedef struct treenode_struct treenode; struct treenode_struct {int nodeid; // node idchar * nodename; // node name}; static treenode maintreenode [] ={{ 1, "device 1" },{ 2, "device 2" },{ 3, "device 3" },{ 4, "Device 4" },{ 5, "Device 5 "},}; wm_hwin createtreeview (int x, int y, int XS, int ys, wm_hwin hparent); // Add a subnode wm_hwin addtreenode (wm_hwin hparent, treenode * nodeinfo, int root );

// ============================== Code file ======== ========================================== //

# Include "createtreeview. H "# include" button. H "# include" text. H "# include" scrollbar. H "# include" string. H "# define windowsbackgroundcolor 0x6a4935 # define nodeheight 20 // height of node collapse # define wm_user_subnodesize wm_user + 102 static void _ cbmain (wm_message * PMSG ); // node callback static void _ cbtreenode (wm_message * PMSG); // callback static void _ cbnodeexpendbutton (wm_message * PMSG) by the expand node button ); // call back the static void _ cbnodebutton (wm_mes Sage * PMSG); // expand the static void expendnode (wm_hwin hwin, int flag); wm_callback * _ cbbtndefault, * _ cbscrollbardefault; wm_hwin createtreeview (int x, int y, int XS, int ys, wm_hwin hparent) {values (); values (fonthztext); values (gui_white, 0); values (windowsbackgroundcolor, 0); wm_hwin hwin = wm_createwindowaschild (X, y, xs, ys, hparent, wm_cf_show, _ Cbmain, 0); Return hwin;} static void _ cbmain (wm_message * PMSG) {Switch (PMSG-> msgid) {Case wm_paint: gui_setbkcolor (0x6a4935 ); gui_clear (); break; Case wm_user_change_scrollbar: {wm_hwin hwin, shwin, svwin; int Height = 0, width = 0; shwin = wm_getscrollbarh (wm_getparent (PMSG-> hwin )); svwin = wm_getscrollbarv (wm_getparent (PMSG-> hwin); hwin = wm_getfirstchild (PMSG-> hwin); While (wm_iswindow (hwin) {If (width <wm_getwinsi Zex (hwin) width = wm_getwinsizex (hwin); height + = wm_getwinsizey (hwin); hwin = equals (hwin);} If (wm_iswindow (shwin) {scrollbar_setnumitems (shwin, width) ;}if (wm_iswindow (svwin) {scrollbar_setnumitems (svwin, height) ;}break ;}default: wm_defaultproc (PMSG );}} // Add a subnode wm_hwin addtreenode (wm_hwin hparent, treenode * nodeinfo, int root) {wm_hwin nodehwin = 0; button_handle bhwin = 0; button_handle btexthwin = 0; b2style; int X, Y, xs, ys; If (wm_iswindow (hparent) {If (root = 1) {wm_hwin hwin = 0, lasthwin = 0; // Add hwin = wm_getfirstchild (hparent) after the last root node; while (wm_iswindow (hwin) {lasthwin = hwin; hwin = wm_getnextsibling (hwin );} if (lasthwin = 0) {x = 5; y = 5;} else {x = 5; y = wm_getwinorgy (lasthwin)-wm_getwinorgy (hparent) + wm_getwinsizey (lasthwin);} xs = wm_getwinsizex (hparent); ys = nodeheight; style = wm_cf_show;} else {X = 20; y = wm_getwindowsizey (hparent); xs = wm_getwinsizex (hparent); ys = nodeheight; style = wm_cf_show;} nodehwin = wm_createwindowaschild (X, Y, xs, ys, hparent, style, _ cbtreenode, 0); // expand the button to hide it. If a subnode is added, bhwin = button_createaschild (, 10, 10, nodehwin, 1, gui_ta_center | gui_ta_vcenter | style); wm_hidewin (bhwin); // node name btexthwin = button_createaschild (20, 0, strlen (nodeinfo-> nodename) * 10, nodeheight, nodehwin, 2, Wm_cf_show | callback | gui_ta_vcenter); wm_setcallback (bhwin, _ callback); _ cbbtndefault = wm_setcallback (btexthwin, _ cbnodebutton); // callback (btexthwin, callback | callback ); button_settext (btexthwin, nodeinfo-> nodename); values (btexthwin, 0); values (bhwin, 0); If (root = 0) {wm_hwin parentbhwin = wm_getfirstchild (hparent ); while (wm_iswindow (P Arentbhwin) {If (wm_getid (parentbhwin) = 1) {button_settext (parentbhwin, "+"); // display the expand button of the parent node, hidden wm_showwin (parentbhwin); break;} parentbhwin = wm_getnextsibling (parentbhwin);} return nodehwin;} // callback static void _ cbtreenode (wm_message * PMSG) {wm_message MSG; wm_hwin nexthwin; int XS, ys; Switch (PMSG-> msgid) {Case wm_user_subnodesize: // change the node height (the message sent when the child node is expanded or collapsed) YS = wm_getysize (PMSG-> hwin) + PMSG-> data. v; WM _ Setysize (PMSG-> hwin, ys); // If the width of the child node is greater than that of the parent node, you need to change the width of the parent node if (wm_getxsize (PMSG-> hwin) <wm_getxsize (PMSG-> hwinsrc) + 20) {xs = wm_getxsize (PMSG-> hwinsrc) + 20; wm_setxsize (PMSG-> hwin, XS );} // change the Y position of the following sibling node nexthwin = wm_getnextsibling (PMSG-> hwin); While (wm_iswindow (nexthwin) {If (wm_getid (nexthwin) = 0) wm_moveto (nexthwin, wm_getwinorgx (nexthwin), wm_getwinorgy (nexthwin) + PMSG-> data. v); nexthwin = wm_getnextsiblin G (nexthwin);} MSG. hwin = wm_getparent (PMSG-> hwin); MSG. msgid = wm_user_subnodesize; MSG. data. V = PMSG-> data. v; // change the parent node wm_sendmessage (MSG. hwin, & MSG); // set the wm_sendmessagenopara (wm_getparent (PMSG-> hwin), break); break; Case wm_paint: {int dy; gui_setbkcolor (windowsbackgroundcolor ); gui_clear ();/* If (! Wm_iswindow (wm_getnextsibling (wm_getparent (PMSG-> hwin) {DY = 10;} else {DY = 0;} gui_setcolor (gui_gray); gui_drawline (4,0, 4, wm_getwinsizey (PMSG-> hwin)-dy); gui_drawline (5, 9, 20, 9); */break;} default: wm_defaultproc (PMSG );}} // call back static void _ cbnodeexpendbutton (wm_message * PMSG) {Switch (PMSG-> msgid) {Case wm_paint: gui_clear (); break; Case wm_pid_state_changed: {wm_pid_state_changed_info * pid_state_changed_info; pid_state_changed_info = (wm_pid_state_changed_info *) PMSG-> data. p; If (pid_state_changed_info-> state> 0) {If (wm_getwinsizey (wm_getparent (PMSG-> hwin) = nodeheight) {button_settext (PMSG-> hwin, "-"); expendnode (wm_getparent (PMSG-> hwin), 1);} else {button_settext (PMSG-> hwin, "+ "); expendnode (wm_getparent (PMSG-> hwin), 0) ;}} break ;}_ cbbtndefault (PMSG) ;}// the node button calls back static void _ cbnodebutton (wm_message * PMSG) {Switch (PMSG-> msgid) {Case wm_paint: gui_setbkcolor (windowsbackgroundcolor); gui_clear (); break; Case wm_policy_parent: {int id = PMSG-> hwinsrc; if (PMSG-> data. V = wm_icationication_released) {;} break;} case wm_pid_state_changed: {Region * pid_state_changed_info; pid_state_changed_info = (Region *) PMSG-> data. p; If (pid_state_changed_info-> state> 0) {// expendnode (wm_getparent (PMSG-> hwin),) ;}}}_ cbbtndefault (PMSG );} static void _ cbschandle (wm_message * PMSG) {Switch (PMSG-> msgid) {Case wm_paint: windowsbackgroundcolor); gui_clear (); break ;}_ cbscrollbardefault (PMSG );} static void expendnode (wm_hwin hparent, int flag) {wm_hwin hwin; wm_message MSG; int I = 0, chheight = 0; hwin = wm_getfirstchild (hparent); While (wm_iswindow (hwin )) {If (wm_getid (hwin) = 0) {I ++; If (flag = 1) {MSG. hwin = hparent; MSG. data. V = wm_getwinsizey (hwin); chheight + = wm_getwinsizey (hwin); MSG. hwinsrc = hwin; MSG. msgid = wm_user_subnodesize; wm_sendmessage (hparent, & MSG); wm_moveto (hwin, wm_getwinorgx (hwin), wm_getwinorgy (hparent) + Digest (hparent)-Digest )); wm_showwin (hwin);} else {MSG. hwin = hparent; MSG. hwinsrc = hwin; MSG. data. V =-wm_getwinsizey (hwin); chheight + =-wm_getwinsizey (hwin); MSG. msgid = wm_user_subnodesize; wm_sendmessage (hparent, & MSG); wm_hidewin (hwin) ;}}hwin = wm_getnextsibling (hwin);} If (flag = 0) {// wm_setxsize (hparent, wm_setysize (hparent, nodeheight );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.