www.pudn.com > matlab7.x.rar > wgtopmax.cpp


/*================================================================= 
 * wgtopmax.cpp ---- makes an MATLAB window always on top 
 *  
 * Use this command-line to compile in MATLAB: 
 * mex wgtopmax.cpp user32.lib 
 * 
 * Input  
 *		name ---- Window's name (A string) 
 *		cmd ---- command (a char) 
 * 
 * Output ---- None 
 * 
 * Usuage: 
 *		wgtopmax('window_name', 't') makes the window on top 
 *		wgtopmax('window_name', 'm') makes the window maximized 
 * 
 * History: 
 *		22 November, 2003 
 *			-Fixed the problem with Chinese characters in window's name 
 *			-Added one more function to maximize the windows. 
 *			-Added one more input argument to take the command to either  
 *			 make the window always on-top, or to maximize the window 
 * 
 *		21 November, 2003 
 *		File created. 
 * 
 * Created by: taohe for simwe forum 
 * 
 * You can use, modify or do anything about this file, provided that  
 * you keep the above information.  
 *=================================================================*/ 
/* $Revision: 1.10 $ */ 
#include  
#include "mex.h" 
 
void mexFunction( int nlhs, mxArray *plhs[],  
		  int nrhs, const mxArray*prhs[] ) 
      
{  
	TCHAR *input_buf, *cmd; 
	int   buflen,status; 
 
    /* Check for proper number of arguments */ 
	if (nrhs != 2) {  
	mexErrMsgTxt("Two input arguments required.");  
    } else if (nlhs > 0) { 
	mexErrMsgTxt("This function has no output.");  
    }  
 
/* The first input must be a string. */ 
  if (mxIsChar(prhs[0]) != 1) 
    mexErrMsgTxt("The first input must be a string."); 
 
/* Input must be a row vector. */ 
  if (mxGetM(prhs[0]) != 1) 
    mexErrMsgTxt("The first input must be a row vector."); 
 
 /* Get the length of the input string. */ 
  buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1*sizeof(TCHAR); 
 
  /* Allocate memory for input and output strings. */ 
  input_buf = (TCHAR *)mxCalloc(2*buflen, sizeof(TCHAR)); 
 
/* The second input must be a string. */ 
  if (mxIsChar(prhs[1]) != 1) 
    mexErrMsgTxt("The second input must be a string."); 
 
/* The second input must be a row vector with one element. */ 
  if (mxGetM(prhs[1]) != 1 || mxGetN(prhs[1]) != 1) 
    mexErrMsgTxt("The second input must be a char."); 
 
  /* Copy the string data from prhs[0] into a C string  
   * input_buf. If the string array contains several rows,  
   * they are copied, one column at a time, into one long  
   * string array. */ 
  status = mxGetString(prhs[0], input_buf, 2*buflen); 
  if (status != 0)  
    mexWarnMsgTxt("Not enough space. String is truncated."); 
 
    /* Do the actual computations in a subroutine */ 
	//mexWarnMsgTxt(input_buf); 
	HWND hWnd; 
	hWnd = FindWindow(NULL, input_buf); 
	if (hWnd != NULL) 
	{ 
		cmd = (TCHAR*) mxGetPr(prhs[1]); 
		switch (*cmd) 
		{ 
		case 'm': 
		case 'M': 
			ShowWindow(hWnd, SW_MAXIMIZE); 
			break; 
		 
		case 't': 
		case 'T': 
			SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 1, 1, SWP_NOMOVE | SWP_NOSIZE | SWP_DRAWFRAME); 
			break; 
		default: 
			mexWarnMsgTxt("Unknown operation!"); 
		} 
	 
	} 
	else 
		mexWarnMsgTxt("Failed to find the specified window."); 
 
	return; 
}