窗体打开时光标变为十字(代码片段工具)截图工具。程序启动(除了光标变化,屏幕上什么都没有),用户点击A点,然后拖动到B点(画一个透明的矩形),松开鼠标,然后保存截图并关闭程序。我绘制透明矩形的方式是调整大小并重新定位具有30%透明度的表单。所以光标永远不会在表格上!为了更改光标,因为它在表单之外,我尝试使用:[DllImport("user32.dll")]staticexternboolSetSystemCursor(IntPtrhcur,uintid);[DllImport("user32.dll")]staticexternIntPtrLoadCursor(IntPtrhInstance,intlpCursorName);私人intCROSS=32515;私有构造uintNORMAL=32512;//然后调用SetSystemCursor(LoadCursor(IntPtr.Zero,CROSS),NORMAL);我对这段代码的问题是它确实是错误的。当窗体关闭时,光标不会恢复正常。我不知道如何正确恢复光标。另外,不可能在从任务管理器关闭表单时恢复光标,是吗?在这种情况下,您建议使用其他什么方法将光标更改为十字?编辑:只是为了澄清,因为我试过问一个类似的问题,之前被标记为这个问题的重复,我删除了它,我想做的是相似的,但有很多不同,如该问题答案中提供的那样,答案中提供的解决方案是制作一个全屏无边框表格,将桌面截图设置为该表格的背景,然后从中裁剪矩形。首先,此解决方案“阻挡”了屏幕,因为在裁剪发生时您只能看到桌面照片,其次,以这种方式处理多显示器设置几乎是不可能的。此外,它还有额外的不必要的工作。尝试将它放在Program.cs文件中staticclassProgram{[DllImport("user32.dll")]staticexternboolSetSystemCursor(IntPtrhcur,uintid);[DllImport("user32.dll")]staticexternIntPtrLoadCursor(IntPtrhInstance,intlpCursorName);[DllImport("user32.dll",CharSet=CharSet.Auto)]privatestaticexternInt32SystemParametersInfo(UInt32uiAction,UInt32uiParam,StringpvParam,UInt32fWinIni);私人静态uintCROSS=32515;私有静态uintNORMAL=32512;[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);SetSystemCursor(LoadCursor(IntPtr.Zero,(int)NORMAL),CROSS);//如果你想要Application.Run(newForm1());,还有其他指向CROSS的指针;SystemParametersInfo(0x0057,0,null,0);这将在应用程序结束或崩溃时恢复正常。因此,就像从Normal更改为Cross一样,您可以将光标更改为您想要的任何箭头它在您停止应用程序(Ctrl+F5)时不起作用,因为这将跳过所有行。但将在发布应用程序后完全有效。制作两张桌子。一个用于拍摄全屏快照,另一个用于裁剪所需区域。选择区域后,将获取的值传递给包含图像的表单并保存。我将举一个例子,你只需要添加两个表单而无需在设计中做任何事情Form_ScreenShotpublicdelegatevoidCROP_PARAMS(Pointpnt,Sizesz);publicpartialclassForm_ScreenShot:Form{Form_TransparentSelectiontranspSelect;图片框pBox;publicForm_ScreenShot(){InitializeComponent();WindowState=FormWindowState.Maximized;FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;pBox=newPictureBox(){Dock=DockStyle.Fill,Cursor=Cursors.Cross};pBox.MouseDown+=pBox_MouseDown;控件。添加(pBox);位图bmp=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);图形grp=Graphics.FromImage(bmp);grp.CopyFromScreen(0,0,0,0,newSize(bmp.Width,bmp.Height),CopyPixelOperation.SourceCopy);pBox.Image=bmp;}voidCropImage(PointstartPoint,Sizesize){Bitmapbmp=newBitmap(size.Width,size.Height);图形grp=Graphics.FromImage(bmp);grp.DrawImage(pBox.Image,newRectangle(0,0,size.Width,size.Height),新矩形角度(起点,大小),GraphicsUnit.Pixel);pBox.Image=bmp;bmp.Save("D:\Check1.png",System.Drawing.Imaging.ImageFormat.Png);}voidpBox_MouseDown(objectsender,MouseEventArgse){transpSelect=newForm_TransparentSelection(e){CropImage=CropImage};transpSelect.ShowDialog();关闭();}}现在半透明选择表格Form_TransparentSelectionpublicpartialclassForm_TransparentSelection:Form{PictureBoxpbSelection;点lastPoint;公共CROP_PARAMSCropImage{得到;放;}publicForm_TransparentSelection(MouseEventArgse){InitializeComponent();WindowState=FormWindowState.Maximized;FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;MouseMove+=Form_TransparentSelection_MouseMove;MouseUp+=Form_TransparentSelection_MouseUp;光标=Cursors.Cross;pbSelection=newPictureBox(){Size=MinimumSize=newSize(5,5),Visible=false,BackColor=Color.LightGreen};控件。添加(pbSelection);lastPoint=新点(eX,eY);铅选择section.Size=pbSelection.MinimumSize;pbSelection.Visible=true;pbSelection.Location=lastPoint;不透明度=.5;TransparencyKey=Color.LightGreen;BackColor=Color.Black;}voidForm_TransparentSelection_MouseUp(objectsender,MouseEventArgse){CropImage(pbS??election.Location,pbSelection.Size);关闭();}voidForm_TransparentSelection_MouseMove(objectsender,MouseEventArgse){pbSelection.Width=eX-lastPoint.X;pbSelection.Height=eY-lastPoint.Y;}}退出应用程序后将光标设置为默认值即可解决问题这可以在Form.Closing事件中实现为:privatevoidForm_Closing(objectsender,System.ComponentModel.CancelEventArgse){//'NORMAL'与代码中的常量相同。SetSystemCursor(LoadCursor(IntPtr.Zero,NORMAL),NORMAL);编辑:通过任务管理器中的“终止任务”选项终止进程将触发Form.Closing事件。无法拦截与“终止进程”选项一起使用的TerminateProcess()调用。以上就是C#学习教程:打开窗体时光标变成十字(代码段工具)。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击有权联系管理员删除。如需转载请注明出处:
